hannenz
hannenz

Reputation: 513

"Glueing together" two bodies in cannon.js

This must be really simple, but I can't seem to get, how one could fix together two bodies in cannon.js. I have experimented with different types of constraints no (first I thought, LockConstraint should be exactly what I need, being desribend as "removing all degrees of freedom").

What I want to achieve is to "stick together" several bodies so that they rotate/behave together if one of them moves/rotates, e.g. like a hammer to its shaft, but absolutely "fix". (The constraints behave a little like if the bodies were connected with rubber bands, even if applying a very high "maxForce" option)

How would I do that? Is body.addShape() the right direction..?!

Thanks a lot for any hints.

Upvotes: 1

Views: 1607

Answers (1)

schteppe
schteppe

Reputation: 2084

If you want two shapes to always have the same relative transform, you should put them in the same Body.

This example code creates a body with two boxes in it, at X=2 and X=-2 locally in the Body, relative to the center of mass.

var body = new CANNON.Body({ mass: 1 });
var shapeA = new CANNON.Box(new CANNON.Vec3(1,1,1));
body.addShape(shapeA, new CANNON.Vec3(2,0,0), new CANNON.Quaternion());
var shapeB = new CANNON.Box(new CANNON.Vec3(1,1,1));
body.addShape(shapeB, new CANNON.Vec3(-2,0,0), new CANNON.Quaternion());

Also see the compound demo: http://schteppe.github.io/cannon.js/demos/compound.html

Upvotes: 2

Related Questions