Bezier Curves in Corona ** SOURCE CODE

17 replies [Last post]
carlos m. icaza
User offline. Last seen 2 years 29 weeks ago. Offline
Alumni
Joined: 22 Jun 2009

Finally !

Bezier segment manipulation using Corona SDK. This is a sample of how to manipulate a cubic bezier segment. Touch the screen to draw the four points (anchor1,handle1,handle2,anchor2) then manipulate the curve.

I cleaned up the code as much as I could. It is not optimized. Feel free to make changes, amplifications and updates and share with the rest of us.

You can download the code at Bezier Code

I tried it on iOS and Android devices.

Let me know what you think.

You can also find the code on the Code Exchange

Enjoy.

Carlos

Replies

carlos m. icaza
User offline. Last seen 2 years 29 weeks ago. Offline
Alumni
Joined: 22 Jun 2009

Fixed a performance issue. Download the latest.

Carlos

betterpriceapps
User offline. Last seen 1 year 34 weeks ago. Offline
Joined: 19 Oct 2010

hi im using the latest version of corona sdk, i have downloaded the code but im getting a blank black screen when i try to open,

IronMan
User offline. Last seen 1 week 5 days ago. Offline
Joined: 5 Jul 2010

of course you get a blank screen :D just tap the screen and play the red dot :D

andyhullinger
User offline. Last seen 5 weeks 4 days ago. Offline
Joined: 23 Feb 2010

This is excellent! (code and timing)
I've got a character in an animation flying a kite (with accelerometer) now I've got the kiteString too.
Now, if we can just talk Jonathan Beebe into making an OpenSource "Cut the Rope" clone :^)

Andy

IronMan
User offline. Last seen 1 week 5 days ago. Offline
Joined: 5 Jul 2010

well, that sounds great :D

carlos m. icaza
User offline. Last seen 2 years 29 weeks ago. Offline
Alumni
Joined: 22 Jun 2009

Faster version uploaded !

c.

amigoni
User offline. Last seen 1 year 39 weeks ago. Offline
Joined: 12 Aug 2010

OK. Sorry but this is pretty mind blowing to me. Are you telling me that I can somehow manipulate your code to create curves with body and have say a ball slide down that awesome bezier curve?

carlos m. icaza
User offline. Last seen 2 years 29 weeks ago. Offline
Alumni
Joined: 22 Jun 2009

Yes

Have you seen a video where I have a tank following the curve?

http://www.youtube.com/watch?v=722su8kVYW4&feature=related

c

horacebury
User offline. Last seen 51 min 27 sec ago. Offline
Joined: 17 Aug 2010

Hi Carlos,

I'm really impressed by your bezier code, but I'd like to know how you got the tank to smoothly follow the curve. Obviously, you're not using the physics engine, but I'm having a little trouble getting the same effect.

Thanks,

Matt.

carlos m. icaza
User offline. Last seen 2 years 29 weeks ago. Offline
Alumni
Joined: 22 Jun 2009

Pseudo code would be something like this

Set the tolerance to an acceptable level
Get the points to the curve
Walk the points and as you walk them get the angle between the points
move the object along the points and rotate it given the angle

C

horacebury
User offline. Last seen 51 min 27 sec ago. Offline
Joined: 17 Aug 2010

Yep, I got that much, however the flip between, say, 5 degrees and 355 degrees causes the tank to spin a full circle on the spot. I was wondering how you got round this? I did myself, but was wondering about other approaches...

Matt

carlos m. icaza
User offline. Last seen 2 years 29 weeks ago. Offline
Alumni
Joined: 22 Jun 2009

local angle = math.atan2(y-x)
angle = angle * 180 / PI (3.1415196)

object:rotate (angle-prevAngle)

prevAngle = angle;

Carlos

horacebury
User offline. Last seen 51 min 27 sec ago. Offline
Joined: 17 Aug 2010

Thanks Carlos...

My bad - I think I assumed certain things. The problem I referred to comes from transition.to (which I used for smooth animation from one angle to the next) tweening between 355 and 5 degrees through the larger range around the circle's 360, not directly through the 360|0 border...

That, of course, is to simply check the difference and add/subtract 360 where appropriate.

m

carlos m. icaza
User offline. Last seen 2 years 29 weeks ago. Offline
Alumni
Joined: 22 Jun 2009

xcellent

post video of such animation when you have it available.. would b great to see it

carlos

horacebury
User offline. Last seen 51 min 27 sec ago. Offline
Joined: 17 Aug 2010

Not wishing to go off topic, but can you recommend something to take videos of the simulator screen?

matt

carlos m. icaza
User offline. Last seen 2 years 29 weeks ago. Offline
Alumni
Joined: 22 Jun 2009

thats offtopic so no i won't go there..

;-)

I use screenflow http://www.telestream.net/screen-flow/overview.htm

there is a thread somewhere in the forum for other alternatives..... but i like screenflow a lot but that is my preference ;-)

c.

coderebelbase
User offline. Last seen 1 year 25 weeks ago. Offline
Joined: 1 Feb 2010

Why isn't this encapsulated into a useful module?

Viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.