Re: Physics Patch-a-Day Thread – Join the fun!!!

Forums > Jitter > Physics Patch-a-Day Thread – Join the fun!!!
Sep 18 2012 | 11:34 pm

hello fizz-fans!

for today’s patch-a-day, we’ve got a fun demonstration of constraints between rigid-bodies, and enabling motors on those constraints.

the patch creates a structure of 5 rigid-bodies, held together with hinge-constraints. i’m calling it a racer.

the racer has 1 main body shape and 4 "limbs". each limb is constrained to the main body with a hinge constraint, and each hinge constraint is motorized, allowing the racer to travel across the ground. the limb objects are each stored in their own bpather, found on the right side of the patch.

the ground (contained inside the "ground" sub-patcher) is made up of some static bodies (bodies with @mass 0). jit.phys.multiple is used to give some random obstacles to the ground.

the camera view can be controlled (via by dragging in the window with the mouse, and pressing the w,a,s,d,q,z keys.

when you first start the patch, the racer’s main-body has @kinematic mode enabled, causing it to remain in place (rather than fall to the ground from gravity). this allows you to adjust properties of the body, limbs, and hinges, without the racer bouncing around in the world. once you’ve adjusted those properties, you can disable kinematic mode to see your racer in action. the patch comes with two presets for different body, limb, and hinge properties. the first preset sets up the limbs more as legs, and the second preset sets them up as wheels.

you can now propel the racer forward (or backward) by enabling the limb-hinge-motors (motorvelocity and motorstrength). what tends to happen at this point is the racer tips over, so we want to add another constraint to the main-body, using the jit.phys.6dof, to prevent this. the 6dof constraint is adjusted so that there are no limitations on linear movement (the lowerlimit_lin values are greater than the upperlimit_lin), allowing the body to move anywhere in the world. the only thing we constrain is the angular orientation of the body, thereby preventing the tip-over. as a side-effect, we can now adjust the racer’s direction by adjusting the world-space y-rotation of this "up" constraint. the patch allows you to use the up and down arrow keys to adjust the motorvelocity and left and right arrow keys to adjust the constraint y-rotation, thereby driving the racer.

as a last bonus, a javascript file can create some opponent racers, using the same basic structure in the main patch, with some randomness applied to certain properties. this demonstrates how physics and gl objects can be managed via javascript.

if you are feeling mirthful, comment out the lines of code that add the "up" constraint, and watch hilarity ensue as the little guys struggle to cross the terrain.

happy racing!

Subscribe to the Cycling ’74 Weekly Newsletter

Let us tell you about notable Max projects, obscure facts, and creative media artists of all kinds.

* indicates required