Sword Slicing Game

Jul 19, 2011 at 7:27 PM
Edited Jul 19, 2011 at 7:29 PM

Hey. I'm trying to emulate this game:


However, I am having some difficulty getting the "spring effect" between the crosshair and the sword. This is what I have:

                World world = new World(new Vector2(0, 0));
                Body swordBody = BodyFactory.CreateBody(world);
                Body crosshairBody = BodyFactory.CreateBody(world);
                swordBody.BodyType = BodyType.Dynamic;
                mouseBody.BodyType = BodyType.Dynamic;
                Fixture swordFixture = FixtureFactory.AttachRectangle(1, 1, 1, Vector2.Zero, swordBody);
                Fixture crosshairFixture = FixtureFactory.AttachCircle(1, 0, mouseBody);
                //The important joints:
                DistanceJoint mouseSwordJoint = JointFactory.CreateDistanceJoint(world, swordBody, mouseBody, swordOrigin, mouseOrigin);
                    mouseSwordJoint.Length = 10f;
                    mouseSwordJoint.DampingRatio = 1f;
                    mouseSwordJoint.Frequency = 10f;
                FixedAngleJoint swordJoint = JointFactory.CreateFixedAngleJoint(world, swordBody);

           //In Update:
                MousePos.X = input.getMouseX();
            MousePos.Y = input.getMouseY();
            swordBody.Position = MousePos;

swordJoint.TargetAngle = (float)Math.Atan2(swordBody.Position.Y - mouseBody.Position.Y, swordBody.Position.X - mouseBody.Position.X);


I am using Farseer 3.3.1.
Unfortunetly, my "crosshair" follows the sword extremely slowly and is prone to "orbiting" the sword.

Any ideas on how to emulate this great game better?
Jul 19, 2011 at 7:49 PM
Edited Jul 19, 2011 at 7:53 PM

Here is the "fix" I have come up with:


//in Update:          
  if (Vector2.Distance(crosshairBody.Position, swordBody.Position) > 20)
                Vector2 swordToCrossHair = Vector2.Subtract(crossHairBody.Position, swordBody.Position);
                crosHairBody.Position = (swordBody.Position + 20 * swordToMouse);

So basically I'm not even using the "DistanceJoint" between the sword and the crosshair anymore.

Now the only annoyance is the wrap that happens between -Pi and +Pi on the swords angle. Basically the sword moves smoothly until its facing horizontally left then it swings all the way around to the other side to get to the other side of the -X axis. Ive fixed this before using cross products, however, I'm wondering why I'm even using Farseer physics if it seems unable to do a proper spring, or proper target angle seeking.

swordJoint.TargetAngle = (float)Math.Atan2(swordBody.Position.Y - mouseBody.Position.Y, swordBody.Position.X - mouseBody.Position.X);