cannon.js
cannon.js copied to clipboard
spring example?
Which example, if any, shows how to use the Spring?
This should be useful... http://schteppe.github.io/cannon.js/demos/spring.html
It's at the very left bottom on the examples page.
Hi there! Following up on this question. I want to add springs inside a for loop. So far I tried this inside the loop:
var spring = new CANNON.Spring(spheresarray[k],spheresarray[j],{
localAnchorA: new CANNON.Vec3(0,0,0),
localAnchorB: new CANNON.Vec3(0,0,0),
restLength : Math.sqrt(distsqr),
stiffness : 500,
damping : 1,
});
world.addEventListener("postStep",function(event){
spring.applyForce();
});
But this seems to be only adding to the world one spring, I think the last one. I guess my loop is just overwriting the spring variable multiple times that's why at the end of the loop I've only added one. But how do I add all of them? Maybe something like creating an array of springs? But I have no idea how..
Thanks for any help!
If you haven't already you'd need push references to the springs into an array to traverse through in your "postStep" handler.
let spring = new CANNON.Spring(spheresarray[k],spheresarray[j],{
localAnchorA: new CANNON.Vec3(0,0,0),
localAnchorB: new CANNON.Vec3(0,0,0),
restLength : Math.sqrt(distsqr),
stiffness : 500,
damping : 1,
});
springsArray.push( spring );
// outside of loop
world.addEventListener("postStep",function(event){
springsArray.forEach( ( spring, i ) => {
spring.applyForce();
}
});