schteppe / p2.js

JavaScript 2D physics library
Other
2.64k stars 330 forks source link

Issue creating a rope bridge #218

Closed AndrewRayCode closed 8 years ago

AndrewRayCode commented 8 years ago

I'm trying to simulate planks connected by ropes to make a bridge.

I currently have a row of boxes (planks) connected by springs (ropes). The leftmost and rightmost springs are world anchored.

However as you can see, this bridge doesn't hold the weight of the player at all. This happens even if I use very high stiffness values for the springs, like 10000.

Do you have any suggestions here? Can I do this with purely springs? Or should I switch to a distance constraint? Or some combination of the two? In the ideal simulation, the bridge is "solid" and even heavy objects cannot pass down through it.

Also, I'm confused about how world anchors work, since a body is still required to anchor to a world point. Can the body be arbitrary? Can it be a box with width: 0, height: 0? Does its position matter? Does p2js have an "empty" object or concept I could use as an anchor?

schteppe commented 8 years ago

If you want to use springs you should make sure that the springs can yield a force large enough to support the player. This is probably difficult to do..

In any case you should switch to constraints. Use Distance or RevoluteConstraint. You can use static bodies without shapes as world anchors.

AndrewRayCode commented 8 years ago

Distance constraints are working well for me so far! And they're easy(er) to tweak than springs.

distance-bridge

AndrewRayCode commented 8 years ago

I'm trying to draw ropes between my planks - do constrains expose their absolute coords? I see that springs are drawn in one of the demos but can't figure out if I can do the same with constraints.

schteppe commented 8 years ago

Maybe they should all have getters for their world anchor points. How about:

DistanceConstraint.prototype.getWorldAnchorA = function(result){
    this.bodyA.toWorldFrame(result, this.localAnchorA);
};
DistanceConstraint.prototype.getWorldAnchorB = function(result){
    this.bodyB.toWorldFrame(result, this.localAnchorB);
};