Saturday, June 9, 2012

Articulating a Dragon

I've been working with the Corona SDK this weekend trying to bring life to a water dragon character for one of our new mobile games.

We'd like to articulate the dragon using twenty-two pivot joints in order to allow the player's character to maneuver around the playing area in a convincing physic-sy way.

I ran into problems right away. After I rigged up the component images with joints in all their proper places and ran the app in the simulator, I noticed that the physics joints between the display objects seemed way too elastic, creating an unconvincing gap between each of the pieces of the dragon. Not only that, there was tons of twitching and stuff going on with some parts of the dragon.

After some searching I turned up this forum posting on ansca It is a long thread, but I found some solutions that improved things quite a bit.

The first problem was with the density that I was assigning each physics object. I found that to prevent gaps in joints you must fine-tune the density on objects on both sides of a joint. Specifically, if you have a super-dense object jointed to a series of less dense objects, or if you are hanging a bunch of objects that cumulatively have more density than the object they are jointed to, you may get elastic joints.

The second thing that helped was setting the frames-per-second from 30 to 60. This may cause performance problems for older devices, but it sure helped clear up some of the twitching problems.

The final thing (and the thing that seemed to make the most difference) is explained here. Basically, there is an undocumented method that you can add to your code right after starting the physics:
physics.setContinuous( false )
Apparently this method turns off continuous physics checking. The only potential side effect of this is that collision detection may not be quite as responsive. For example, a high velocity object hitting a one-pixel wall may go right through it. The fix for this is just to make the wall a bit wider (e.g. ten pixel width instead of one).

The dragon works pretty well now. He doesn't jerk around as much, and the joints are much tighter. There is still some joint weirdness I will need to figure out that occurs when you drag the character around too fast, but that should not be a problem since the game will not use continuous touch events (it will be single-tap game play).

Anyway, this proof of concept has been fun, and I have learned a lot about articulating characters and optimizing physics joints.

No comments:

Post a Comment