public void AddRope(Rope rope)
{
float ropeLength = (rope.AttachedEntity.Position - rope.AttachedPosition).Length();
int segmentCount = (int)Round(1 + (ropeLength / Rope.SegmentLength));
if (segmentCount < 2)
segmentCount = 2;
var ropeSegmentDir = Vector2.Normalize(rope.AttachedEntity.Position - rope.AttachedPosition);
// TODO: fix the RotationFromDirection function!
var ropeRotation = ropeSegmentDir.RotationFromDirection();
rope.PhysicsSegments.Clear();
for (int i = 0; i < segmentCount; i++)
{
var ropeSegmentLength = i > 0 ? Rope.SegmentLength : Rope.Diameter;
var ropeSegmentVector = ropeSegmentDir * ropeSegmentLength;
var segmentStart = rope.AttachedPosition + ropeSegmentVector * i;
var segmentCenter = rope.AttachedPosition + ropeSegmentVector * (i + 0.5f);
var boxWidth = Rope.Diameter;
var boxHeight = i > 0 ? Rope.SegmentLength : Rope.Diameter;
//var segment = BodyFactory.CreateCircle(PhysicsWorld, Rope.JointRadius, Rope.SegmentDensity);// BodyFactory.CreateRectangle(PhysicsWorld, 0.05f, Rope.SegmentLength, 0.5f);
var segment = BodyFactory.CreateRectangle(PhysicsWorld, boxWidth, boxHeight, Rope.SegmentDensity);
segment.BodyType = BodyType.Dynamic;
segment.Friction = 0.5f;
segment.Restitution = 0.2f;
segment.AngularDamping = 0.2f;//1.1f;
segment.LinearDamping = 0.2f;// 1.1f;
segment.CollisionCategories = Category.Cat3;
segment.SetTransform(segmentCenter, -ropeRotation - 0.5f * (float)Math.PI);
rope.PhysicsSegments.Add(segment);
if (i > 0)
{
var segmentJoint = JointFactory.CreateRevoluteJoint(PhysicsWorld, rope.PhysicsSegments[i - 1], segment, new Vector2(0, i == 1 ? Rope.Diameter / 2 : Rope.SegmentLength / 2), new Vector2(0, -Rope.SegmentLength / 2));
segmentJoint.CollideConnected = false;
rope.PhysicsSegmentJoints.Add(segmentJoint);
}
else
{
segment.BodyType = BodyType.Static;
}
}
//var ballAnchor = rope.AttachedEntity.Position + new Vector2(0, rope.AttachedEntity.Radius + 1f);
var ballJoint = JointFactory.CreateRevoluteJoint(PhysicsWorld, rope.PhysicsSegments.Last(), rope.AttachedEntity.PhysicsBody, new Vector2(0, Rope.SegmentLength / 2), Vector2.Zero);
ballJoint.CollideConnected = false;
rope.PhysicsEntityJoint = ballJoint;
rope.AttachedEntity.PhysicsBody.FixedRotation = false;
//rope.AttachedEntity.PhysicsBody.Mass = 2f;
}