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

ShortenRope() public method

public ShortenRope ( Rope rope ) : void
rope Ballz.GameSession.World.Rope
return void
        public void ShortenRope(Rope rope)
        {
            if (rope.PhysicsSegments.Count <= 2)
            {
                // Rope cannot be any shorter as 2 segments is minimum
                return;
            }

            // Only allow shortening rope if all joints are restful
            for (int i = 0; i < rope.PhysicsSegmentJoints.Count; ++i)
            {
                var joint = rope.PhysicsSegmentJoints[i];
                if (Vector2.Distance(joint.WorldAnchorA, joint.WorldAnchorB) > 0.3f * Rope.SegmentLength)
                    return;
            }

            PhysicsWorld.RemoveJoint(rope.PhysicsEntityJoint);
            PhysicsWorld.RemoveJoint(rope.PhysicsSegmentJoints.Last());
            rope.PhysicsSegmentJoints.Remove(rope.PhysicsSegmentJoints.Last());


            RemoveBody(rope.PhysicsSegments.Last());
            rope.PhysicsSegments.Remove(rope.PhysicsSegments.Last());

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

        }