Mittwoch, 26. Dezember 2012

Corona SDK and Lua: Creating a rotating obstacle with a physics joint

Android app on Google Play



As you might have seen in our game Cookie Billiard, we created some rotating elements that are used as obstacles for the cue or as levers for the cookie. This kind of element can be created by using the standard physics joints that corona sdk offers (http://developer.coronalabs.com/content/game-edition-physics-joints). However, there is no physics joint that is specifically indended for such a purpose. We realised it by using the standard pivot joint that is also used to create ragdoll figures.


physics.newJoint( "pivot", object1, object2, 200,300 )


The pivot joint in general connects two display objects by creating an imaginary line that starts at the center of the first object and ends at the center of the second object (resp. vise versa). The coordinates (here 200,300) specifiy a point where the lines, and the objects that are connected to it, can rotate around.


The movements of the two objects are now controlled by the joint and its break point. They can't rotate around the origin of its connecting line. But now a precularity of corona sdk comes into play: when the two obects lie on top of each other, the last restriction does not hold anymore and they can rotate around the origin of its connecting line.

 So we just created the circle first and made it static so that it can't move:


myCircle = display.newCircle(100, 100, 10 )    
physics.addBody (myCircle, "static", {radius = 10, bounce = 0.5,  friction = 0.65})

Then we created a rectangle that lies on top of the rectangle as illustrated in the picture. Important is that you do not set its physics body to "static" sinse it should be able to move:
       physics.addBody (myRotator, {density = 4, bounce = 0.5,  friction = 0.65})

Eventually, we added a pivot joint that connected the circle and the rectangle:
       physics.newJoint ("pivot", myCircle, myRotator, myCircle.x, myCircle.y)

An important  property here is the desity (in this case set to 4) of the physics body of the rectangle. If it set to low and the cue later on hits the rectangle, it might happen that the rectangle and the circle move slightly away from its origin which looks strange and leads to strange rebounds of the cue. It seems to be a bug of corona sdk that has not been solved yet. I found some other users in different forums also complaining about this problem. 

I hope this post was helpful for you.

Your MOCCA GAMES team.

Android app on Google Play

Keine Kommentare:

Kommentar veröffentlichen