Hanging Rope with swinging

Discuss any questions about BEPUphysics or problems encountered.
Post Reply
schulzor2004
Posts: 29
Joined: Mon Jan 12, 2009 2:43 pm

Hanging Rope with swinging

Post by schulzor2004 »

Hi,

I try to make a hanging rope with DistanceConstraint (or DistanceRangeConstraint) with 3 Entities bound together. This works finde. Than I have a guy that hangs at the end of the rope (also attached with DistanceConstraint). By pulling the left trigger to left or right (on the XBox Controller) an impulse is added to the guy, so that it swings:

Here is the code for the 3 entities that build the rope:

Code: Select all

//nBall is the first entity (with no movement), connect nBall with nBall1 (the entities are all Spheres)
DistanceConstraint drc1 = new DistanceConstraint(nBall.PhysicObject.Entity, nBall1.PhysicObject.Entity,
  nBall.Position, nBall1.Position, 0, 0.5f, 10000);

//connect nBall1 with nBall2
DistanceConstraint drc2 = new DistanceConstraint(nBall1.PhysicObject.Entity,
  nBall2.PhysicObject.Entity, nBall1.Position, nBall2.Position, 0f, 0.5f, 10000);

//connect nBall2 with foot (a cube at the end of the rope)
DistanceConstraint drc3 = new DistanceConstraint(nBall2.PhysicObject.Entity,
  foot.PhysicObject.Entity, nBall2.Position, foot.Position, 0f, 0.5f, 10000);

//build a constraint to a ground contraint, so that the rope is swinging out and comes to a silence)
GrabConstraint gc = new GrabConstraint(
  foot.PhysicObject.Entity, foot.Position, jGround.Position, 2f, 0.8f, 0.6f);

foot.PhysicObject.Entity.nonCollidableEntities.Add(guy.PhysicObject.Entity);

Scene.currentScene.Space.add(drc1);
Scene.currentScene.Space.add(drc2);
Scene.currentScene.Space.add(drc3);
Scene.currentScene.Space.add(gc);
And here the guy is added to the rope

Code: Select all

Node foot = Scene.currentScene.FindNode("Foot");

//connect the Foot of the rope with the guy
ropeJoint = new DistanceConstraint(physicObject.Entity,
  foot.PhysicObject.Entity, foot.PhysicObject.Position+v1, foot.Position, .0f, .1f, 1000000);

Scene.currentScene.Space.add(ropeJoint);
Guy has Mass=100 the rope entities have 1

With following code the guy gets some impulse to swing:

Code: Select all

//apply impulse on the guy
float pow = leftStick.X * 5.0f;
Vector3 impPos = new Vector3(physicObject.Position.X, physicObject.Position.Y - 5, physicObject.Position.Z);
physicObject.Entity.applyImpulse(impPos, new Vector3(pow, 0, 0));
This works relatively good but now some questions, I could not solve:

- How do I make the rope swing only left and right (X-Direction) NOT back and front (Z-Direction), it get's there sometimes, so is there a possibility to prevent this?
- The more impulse I add to the guy (by swinging left and right) the swing gets very high, is there a possibility to restrict the swing angle of the different DistanceConstraints?
- Is the technique I used correct, or is there a better way to achieve the rope-swing effect?

Thanks for answering!
Norbo
Site Admin
Posts: 4929
Joined: Tue Jul 04, 2006 4:45 am

Re: Hanging Rope with swinging

Post by Norbo »

The easiest way to constrain the joint in the way you want is to fake it. Applying a corrective force based on the distance of the entity from the desired Z plane would work. Similarly, checking the angle of entities to the starting connection of the joint would give you a way to push things down if they go too far up. There's no built in way to constrain the distance constraint's allowed angle at the moment (more constraints/limits/motors are planned for a future version).

The way you outlined will work fine. You might notice some stability issues with your mass ratios; if you see any, try increasing the mass of the rope entities a bit. Another option would be to use a single DistanceRangeConstraint. This won't give you any help on the graphics side, but allows your 'rope' to have a range of allowed distances so that the entity can flop around a bit more and eliminates the need for the intermediate entities.
Post Reply