JiglibFlash: Using Constraints
In 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…


Hi
Do you know if there’s a way to constrain 2 rigid bodies in a way to make a rigid joint between them? Like a hack to limit all properties of a constrained body, only using its mass influence.
Thanks!
@carlos, Well its a bit hacky but you can construct your shape from primitives, cubes and spheres or your own if you are inclined. Use Jconstraints to join them and make them non elastic, so completely rigid. Try altering the mass of individual objects as well to create different centers of mass. For example if you wanted to make a hammer, you could try constraining 2 cubes, one long one and one smaller one on the top. You’d have to rotate the top one and then “join” them. When I have time, (which seems to be less and less lately) I’ll work up a demo of that. Also, if you have time look at the source for the car demo(s) that are linked to off the google code site for jiglibflash. Good luck and let me know how you’re making out!
Hi
Thanks for the reply! One last question: any hints on removing the elasticity?
hi, really like your experiments using jiglib.
trying to figure out if its possible to use flat planes in the same way as boxes in jiglib…?
figured it’d be possible to apply rigid body physics to flat planes as well as cubes and spheres… is that right?
@carlos, try setting the allowedDistance and timescale attributes of the JConstraint to as small as possible (you’ll have to experiment a little with it). I will try to work on a demo of this (compound shapes) and if I need to add things to the classes I’ll let you know and post some updates.
@julapy, thanks for stopping by, much appreciated. If you look at the walls in my demos so far, those are all flat planes. In jiglib, we call them JPlanes. If you download the latest rev of the code and the first example here, you will see the basics for creating a JPlane which you then map to your 3d engine’s visual representation of a Plane (Plane3D). I’ll try to post a “Getting Started…” tutorial with the basics laid out. I have been hesitating, because we are working so much on the code but I think it could be handy anyway.
Thanks again!
Thank you!!!
Thank you!!!
Thank you!!!
Thank you!!!
=)