I am using the Farseer Engine port of Box2D, but I believe this question pertains to Box2D in general. I am creating a rope connecting one body to the world. I've adjusted the densities of the circles to prevent odd behavior from light objects supporting heavy ones, but my circles still wont collide with any other objects in the world. I want my rope to behave... well, rope-like - such that if the object swings around the end of a platform it will wrap around instead of go right through it.
I am using a series of circles spaced evenly, adding a rope joint to connect the previous link to the current one, then ending the rope with a FixedDistanceJoint to the world.
Please ignore how hackish the code is.
I am using a series of circles spaced evenly, adding a rope joint to connect the previous link to the current one, then ending the rope with a FixedDistanceJoint to the world.
Please ignore how hackish the code is.
Code:
Vector2 startPos = CollisionComponent.convertToWorld(_playerPhysics.ScreenPosition) - new Vector2(0f, 0.5f);
Vector2 endPos = grapplePoint;
Body prevLink = _topBody;
//Get the direction as a unit vector
var direction = Vector2.Normalize(endPos - startPos);
const float maxLinkDist = 0.50f;
var distance = Vector2.Distance(startPos, endPos);
Vector2 linkEnd = endPos;
for (float ropeDist = 0f; distance - ropeDist > maxLinkDist; )
{
float linkDist = maxLinkDist;
linkEnd = startPos + (direction * linkDist);
var link = BodyFactory.CreateCircle(_physicsSystem.World, 0.1f, 50f);
link.Position = linkEnd;
link.BodyType = BodyType.Dynamic;
link.FixedRotation = false;
if (prevLink != null)
{
//Create a distance joint between the two links
//JointFactory.CreateAngleJoint(_physicsSystem.World, prevLink, link);
//JointFactory.CreateDistanceJoint(_physicsSystem.World, prevLink, link, startPos, startPos);
var joint = new RopeJoint(prevLink, link, prevLink.GetLocalPoint(ref startPos), link.GetLocalPoint(ref linkEnd));
joint.MaxLength = maxLinkDist;
joint.CollideConnected = true;
_physicsSystem.World.AddJoint(joint);
}
ropeDist += linkDist;
prevLink = link;
startPos = linkEnd;
}
//Create the final joint to attach to the ceiling
JointFactory.CreateFixedDistanceJoint(_physicsSystem.World, prevLink, prevLink.GetLocalPoint(ref linkEnd), endPos);
Vector2 endPos = grapplePoint;
Body prevLink = _topBody;
//Get the direction as a unit vector
var direction = Vector2.Normalize(endPos - startPos);
const float maxLinkDist = 0.50f;
var distance = Vector2.Distance(startPos, endPos);
Vector2 linkEnd = endPos;
for (float ropeDist = 0f; distance - ropeDist > maxLinkDist; )
{
float linkDist = maxLinkDist;
linkEnd = startPos + (direction * linkDist);
var link = BodyFactory.CreateCircle(_physicsSystem.World, 0.1f, 50f);
link.Position = linkEnd;
link.BodyType = BodyType.Dynamic;
link.FixedRotation = false;
if (prevLink != null)
{
//Create a distance joint between the two links
//JointFactory.CreateAngleJoint(_physicsSystem.World, prevLink, link);
//JointFactory.CreateDistanceJoint(_physicsSystem.World, prevLink, link, startPos, startPos);
var joint = new RopeJoint(prevLink, link, prevLink.GetLocalPoint(ref startPos), link.GetLocalPoint(ref linkEnd));
joint.MaxLength = maxLinkDist;
joint.CollideConnected = true;
_physicsSystem.World.AddJoint(joint);
}
ropeDist += linkDist;
prevLink = link;
startPos = linkEnd;
}
//Create the final joint to attach to the ceiling
JointFactory.CreateFixedDistanceJoint(_physicsSystem.World, prevLink, prevLink.GetLocalPoint(ref linkEnd), endPos);