Archive

Archive for February, 2009

JiglibFlash: Using Constraints

February 10th, 2009 richard mattka 6 comments

demo42In physics engines constraints are basically the means to bind objects together. Remember making crafts in grade school? Imagine you have 2 of those old Styrofoam balls and you jam each one on to the end of a stick. Well in jiglibflash the balls are called JSpheres and the stick is the JConstraintPoint. In the demo you can see how this looks. This is the basic way to create more complex shapes using basic shapes in the engine. The way to create the constaint is using the JConstraintPoint object:

JConstraintPoint(body0:RigidBody, body0Pos:JNumber3D, body1:RigidBody, body1Pos:JNumber3D, allowedDistance:Number = 1, timescale:Number = 1)

You can experiment with allowedDistance and timescale to get various results, which to me equate to various degrees of elasticity or rigidity.

So, after you create some objects to join (i created a little array of JSpheres called ballBody), you need to join them together. Here’s an example:

var pos1:JNumber3D;
var pos2:JNumber3D;
chain = new Array();
for (i = 1; i < ballBody.length; i++ )
{
trace(i);
pos1 = JNumber3D.multiply(JNumber3D.UP, -ballBody[i - 1].BoundingSphere);
pos2 = JNumber3D.multiply(JNumber3D.UP, ballBody[i].BoundingSphere);
chain[i] = new JConstraintPoint(ballBody[i - 1], pos1, ballBody[i], pos2, 1, 0.2);
PhysicsSystem.getInstance().AddConstraint(chain[i]);
}

In the above code I loop through my objects (the ballBody array) and add constraints between them. So in effect I “chain” them together. The result is a little simple snake body.

You can get creative and come up with more complex shapes and configurations, but this is the basic concept.

Hope that helps. More to come…

Categories: jiglibflash, papervision3d Tags: