Skip to content

Commit

Permalink
added .multiplier to Equation, added tear demo #229
Browse files Browse the repository at this point in the history
  • Loading branch information
schteppe committed Sep 17, 2015
1 parent 86e4a03 commit 679102a
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 0 deletions.
96 changes: 96 additions & 0 deletions demos/tear.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
<!DOCTYPE html>
<html>
<head>
<title>cannon.js - constraints demo</title>
<meta charset="utf-8">
<link rel="stylesheet" href="css/style.css" type="text/css"/>
<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
</head>
<body>
<script src="../build/cannon.js"></script>
<script src="../build/cannon.demo.js"></script>
<script src="../libs/dat.gui.js"></script>
<script src="../libs/Three.js"></script>
<script src="../libs/TrackballControls.js"></script>
<script src="../libs/Detector.js"></script>
<script src="../libs/Stats.js"></script>
<script src="../libs/smoothie.js"></script>
<script>

var demo = new CANNON.Demo();

// Sphere chain
demo.addScene("Sphere chain",function(){
var size = 0.45;
var dist = size*2+0.12;
var world = setupWorld(demo);
//world.solver.setSpookParams(1e20,3);
var sphereShape = new CANNON.Sphere(size);
var mass = 1;
var lastBody = null;
var N = 15;
world.solver.iterations = N; // To be able to propagate force throw the chain of N spheres, we need at least N solver iterations.
var constraints = [];
for(var i=0; i<N; i++){
// Create a new body
var spherebody = new CANNON.Body({ mass: i===0 ? 0 : mass });
spherebody.addShape(sphereShape);
spherebody.position.set(0,0,(N-i)*dist - 9);
world.addBody(spherebody);
demo.addVisual(spherebody);

// Connect this body to the last one added
var c;
if(lastBody!==null){
world.addConstraint(c = new CANNON.DistanceConstraint(spherebody,lastBody,dist));
constraints.push(c);
}

// Keep track of the lastly added body
lastBody = spherebody;
}

world.addEventListener('postStep', function(){
for(var i=constraints.length-1; i>=0; i--){
// The multiplier is proportional to how much force that is added to the bodies by the constraint.
// If this exceeds a limit we remove the constraint.
var m = Math.abs(constraints[i].equations[0].multiplier);
if(m > 1000){
world.removeConstraint(constraints[i]);
}
}
});

// Throw a body on the chain to break it!
var spherebody = new CANNON.Body({ mass: mass*2 });
spherebody.addShape(sphereShape);
spherebody.position.set(20,0,3);
spherebody.velocity.x = -30;
world.addBody(spherebody);
demo.addVisual(spherebody);
});

function setupWorld(demo){
// Create world
var world = demo.getWorld();
world.gravity.set(0,0,-10);
world.broadphase = new CANNON.NaiveBroadphase();

/*
// ground plane
var groundShape = new CANNON.Plane();
var groundBody = new CANNON.Body({ mass: 0 });
groundBody.addShape(groundShape);
groundBody.position.set(0,0,-15);
world.addBody(groundBody);
demo.addVisual(groundBody);
*/

return world;
};

demo.start();

</script>
</body>
</html>
7 changes: 7 additions & 0 deletions src/equations/Equation.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,13 @@ function Equation(bi,bj,minForce,maxForce){
*/
this.enabled = true;

/**
* A number, proportional to the force added to the bodies.
* @property {number} multiplier
* @readonly
*/
this.multiplier = 0;

// Set typical spook params
this.setSpookParams(1e7,4,1/60);
}
Expand Down
7 changes: 7 additions & 0 deletions src/solver/GSSolver.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,13 @@ GSSolver.prototype.solve = function(dt,world){
b.wlambda.vmul(b.angularFactor, b.wlambda);
w.vadd(b.wlambda, w);
}

// Set the .multiplier property of each equation
var l = equations.length;
var invDt = 1 / h;
while(l--){
equations[l].multiplier = lambda[l] * invDt;
}
}

return iter;
Expand Down

0 comments on commit 679102a

Please sign in to comment.