schteppe / cannon.js

A lightweight 3D physics engine written in JavaScript.
http://schteppe.github.com/cannon.js
MIT License
4.67k stars 709 forks source link

More rigid hinge constraint #369

Open bilkentCrap opened 6 years ago

bilkentCrap commented 6 years ago

Thank you really very much for this amazing project.

Everything works perfectly but after checking source, stackoverflow, github issues, demos I couldn't figure out how to prevent hinge constraint from bending.

You can see behavior at: https://bilkentcrap.github.io/cannon.js/demos/hinge.html

In demo gy is set to 100 so it bends the hinge constraint. I am trying to prevent it from bending.

Source: https://github.com/bilkentCrap/cannon.js/blob/gh-pages/demos/hinge.html

serkankazak commented 6 years ago

Demo:

serkankazak commented 6 years ago
var demo = new CANNON.Demo();
demo.addScene("test", function() {

    var world = setupWorld(demo);
    world.gravity.set(0, 100, -10);
    demo.setGlobalSpookParams(1e7, 20, 1/60);

    var material = new CANNON.Material();
    material.friction = 0.4;
    var body = new CANNON.Body({ mass: 0, material: material });
    var bo1 = new CANNON.Body({ mass: 0.2, material: material });        
    var bo2 = new CANNON.Body({ mass: 0.2, material: material });
    body.addShape(new CANNON.Box(new CANNON.Vec3(0.25, 0.25, 0.5)));
    bo1.addShape(new CANNON.Box(new CANNON.Vec3(0.25, 0.25, 1.5)));
    bo2.addShape(new CANNON.Box(new CANNON.Vec3(0.25, 0.25, 1.5)));
    body.position.set(-4 - 0.25, -2 - 0.25, 1.5 * 4 + 0.8 * 2 + 0.5);
    bo1.position.set(-4, -2, 1.5 * 3 + 0.8);
    bo2.position.set(-4, -2, 1.5);

    var l1c1 = new CANNON.HingeConstraint(bo1, bo2, { pivotA: new CANNON.Vec3(0, -0.25, -1.5 - 0.8 / 2), axisA: new CANNON.Vec3(0, 1, 0), pivotB: new CANNON.Vec3(0, -0.25, 1.5 + 0.8 / 2), axisB: new CANNON.Vec3(0, 1, 0) });
    var l1bc = new CANNON.HingeConstraint(body, bo1, { pivotA: new CANNON.Vec3(0, 0, -0.5 - 0.8 / 2), axisA: new CANNON.Vec3(0, 1, 0), pivotB: new CANNON.Vec3(0, -0.25, 1.5 + 0.8 / 2), axisB: new CANNON.Vec3(0, 1, 0) });

    world.add(body);
    world.add(bo1);
    world.add(bo2);
    demo.addVisual(body);
    demo.addVisual(bo1);
    demo.addVisual(bo2);
    world.addConstraint(l1c1);
    world.addConstraint(l1bc);

});
function setupWorld(demo) {
    var world = demo.getWorld();
    world.broadphase = new CANNON.NaiveBroadphase();
    world.solver.iterations = 50;
    world.quatNormalizeFast = false;
    world.quatNormalizeSkip = 0;
    return world;
};
demo.start();