Ballz.GameSession.Physics.PhysicsControl.LoosenRope C# (CSharp) Method

LoosenRope() public method

public LoosenRope ( Rope rope ) : void
rope Ballz.GameSession.World.Rope
return void
        public void LoosenRope(Rope rope)
        {
            if (rope.PhysicsSegments.Count * Rope.SegmentLength >= Rope.MaxLength)
            {
                // Maximum length reached, rope cannot be made any longer
                return;
            }

            var lastSegment = rope.PhysicsSegments.Last();
            var ropeSegmentVector = Vector2.Normalize(rope.AttachedEntity.Position - lastSegment.Position) * Rope.SegmentLength;
            PhysicsWorld.RemoveJoint(rope.PhysicsEntityJoint);

            //var newSegment = BodyFactory.CreateCircle(PhysicsWorld, Rope.SegmentWidth, Rope.SegmentDensity);// BodyFactory.CreateRectangle(PhysicsWorld, 0.05f, Rope.SegmentLength, 0.5f);
            var newSegment = BodyFactory.CreateRectangle(PhysicsWorld, Rope.Diameter, Rope.SegmentLength, Rope.SegmentDensity);
            newSegment.BodyType = BodyType.Dynamic;
            newSegment.AngularDamping = 0.1f;
            newSegment.LinearDamping = 0.1f;
            newSegment.Friction = 0.5f;
            newSegment.Restitution = 0.2f;
            newSegment.CollisionCategories = Category.Cat3;
            newSegment.Position = lastSegment.Position + ropeSegmentVector;

            rope.PhysicsSegments.Add(newSegment);

            var segmentJoint = JointFactory.CreateRevoluteJoint(PhysicsWorld, lastSegment, newSegment, new Vector2(0, Rope.SegmentLength / 2), new Vector2(0, -Rope.SegmentLength / 2));
            segmentJoint.CollideConnected = false;
            rope.PhysicsSegmentJoints.Add(segmentJoint);

            var ballJoint = JointFactory.CreateRevoluteJoint(PhysicsWorld, rope.PhysicsSegments.Last(), rope.AttachedEntity.PhysicsBody, Vector2.Zero, Vector2.Zero);
            ballJoint.CollideConnected = false;
            rope.PhysicsEntityJoint = ballJoint;
        }