Tuesday, January 6, 2009

stickman physaxe

So I was reading about physaxe 1.2 and thought it would be fun to have some more stickman action leveraging it.

The demo I throw together is mostly from code ripped off from the original post and little bit of stuff to make the "springs":

Move the mouse around to move gravity and click anywere to add some junk:



Usage is something like this:

var a = body( 50, 40 );
var b = body( 50, 140 );
var j = new DistanceJoint( a, b );

world.addBody( a );
world.addBody( b );
world.addJoint( j );


It is not integrated into the physaxe code right, cuz I had to loop over my joints when I stepped the world.

The interesting piece of the "DistanceJoint" look like this:

public override function preStep( invDt : Float ) {
var currentDistance = dist( b1, b2 );
if ( 0 != currentDistance ) {

var diff = ( currentDistance - this.distance ) / currentDistance;
var xdiff = ( b2.x - b1.x ) * diff;
var ydiff = ( b2.y - b1.y ) * diff;

b1.v.x += xdiff * 0.5;
b2.v.x -= xdiff * 0.5;

b1.v.y += ydiff * 0.5;
b2.v.y -= ydiff * 0.5;
}
}


It doesn't take into account the relative mass of the bodies but that should be pretty straight forward.

It should be enough for stickmen, cloth, strings, etc.

If you are interested, you can find the code here.

No comments:

Post a Comment