Sunday, November 11, 2012

Game-off: Day 8

Chains and pulleys.

I had a feeling adding support for chains would not be too difficult. However, it took just over 4 hours to get it all working. The difficult part with this one was positioning the chain segments properly. When they are placed incorrectly, the physics simulation corrects them by applying forces, which causes horrific jitter.

(Clicky for make big)

As part of the positioning, the rotation of each segment is also important. For this, I just used Chipmunk's excellent vector methods. The first step is getting the difference between the vectors, normalizing them to 0..1, and finally converting the resulting vector to an angle. For the position, I just use Chipmunk's built-in vector linear interpolation function. It's excellent!

There's one known bug that the segments nearest to each point on the chain are initialized with an incorrect rotation, and the positions are non-optimal. It creates a little bit of jitter, but it's not completely broken! It could be better.

The pulleys are just circles which are anchored to the default static body with pivot joints. Friction causes them rotate as the chain slides over.

The other obvious change for tonight is that most objects (but not all) are initialized with a random color. This was part of an API update for the object constructors.


evilo said...

Very nice, I can't wait to see the final result, I think I have a fair idea of where you want to go, but as you said it earlier, it will be better with some real sprites :) You should as well consider adding some more "touch device friendly" meta tags into your html, like the one I put in the whack-a-mole example :

as without them is difficult to use the touche event without actually moving/scaling the page :)

Unknown said...

Hm! I already have the meta tags on index.html:

Is it not working?