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);
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);
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));
- 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!