display.newArc()

It would be very nice to have a factory function to draw an arc. Something like that:

display.newArc(x, y, width, height, start, stop)

x = x coordinate of the arc ellipse
y = y coordinate of the arc ellipse
width = width of the arc ellipse
height = height of the arc ellipse
start = angle to start the arc ellipse
stop = angle to stop the arc ellipse

And also:

display.newEllipse(x, y, width, height)

display.newTriangle (x1, y1, x2, y2, x3, y3)

display.newQuadr (x1, y1, x2, y2, x3, y3, x3, y3)

As 2D is the corebusiness of Corona, these new functions would be legitimate...

I just posted regarding an arc, then noticed this. I am happy to see others have interest in the same type of feature.

You can actually append functions to display yourself if you really want.

Just copy this to the top of your main.lua or whatever:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
        function display.newArc(x,y,w,h,s,e)
                local xc,yc,xt,yt,cos,sin = x+w/2,y+h/2,0,0,math.cos,math.sin
                s,e = s or 0, e or 360
                s,e = math.rad(s),math.rad(e)
                w,h = w/2,h/2
                local l = display.newLine(0,0,0,0)
                for t=s,e,0.02 do l:append(xc + w*cos(t), yc - h*sin(t)) end
                return l
        end
        function display.newEllipse(x,y,w,h)
                return display.newArc(x,y,w,h)
        end
        function display.newTriangle(x,y,x2,y2,x3,y3)
                local l = display.newLine(x,y,x2,y2)
                l:append(x3,y3,x,y)
                return l
        end
        function display.newQuad(x,y,x2,y2,x3,y3,x4,y4)
                local l = display.newLine(x,y,x2,y2)
                l:append(x3,y3,x4,y4,x,y)
                return l
        end

Hey sweet! Thanks for the code Yobonja!

No problem, Here's an alternative Ellipse function that returns a filled in ellipse:

1
2
3
4
5
6
7
8
9
10
        function display.newEllipse(x,y,w,h)
                local circle = display.newCircle(0,0,w/2)
                local ellipse = display.newGroup()
                ellipse:insert(circle)
                ellipse:setReferencePoint(display.CenterReferencePoint)
                ellipse.x, ellipse.y  = w/2, h/2
                ellipse.yScale = h/w
                circle.x, circle.y = x, y+h
                return circle
        end

@Yobonja : Thanks for sharing! +++

Thanks for the wonderful tip Angelo!

Do you know an easy way to create a physics object for an arc as well? I attempted to use a polygon with multiple small faces, but it cause a lot of physics glitches (object sticking in object, etc)

I've also considered doing a circular sensor, and making my own check on the quadrant of the circle that the object is colliding with based on the event location, but it doesn't seem like that would be very optimal.

I would think that a bunch of small rects jointed together would work, but I don't know. Do you need a quarter circle or an arc? If you just need a wedge, I think you can manage it with a custom shape. but you can't do a concave shape, so for an arc I'd joint several rects together.

I also think the circular sensor might work depending on your situation, if you're doing a view cone it should be fine, but I'm not sure how well it would work if you're trying to pile cheese wedges. One way to find out though right?

-Angelo

The problem with connecting the multiple small rects is that occasionally an object will get stuck between them. I tested this with a simple "pinball gun" system. When the ball was sent up and hit the inner curved edge, it would get occasionally caught in the edge between two rect. I even attempted to force overlap in each rect so there was no space between, but it still occurred.

Is the isBullet property of the ball set to true?(it should be) I have some experience with a pinball like setup ~ we make curves out of smaller elements in some of our levels, and it seems to work fine, even at high speeds.

It all depends on what you're trying to do, If the arcs ar not going to move, like they are just attached to the background, they don't even need to be jointed together.

Also, you could modify the Arc function to make the arcs out of rects instead of a line, just be sure to adjust the granularity so you don't make too many rects.

-Angelo

views:2014 update:2011/9/18 10:09:28
corona forums © 2003-2011