Basic player character [Solved]

Topics: User Forum
Dec 15, 2010 at 11:31 AM
Edited Dec 15, 2010 at 11:33 AM
I'm trying to implement the player character with the 'box-wheel setup'. But the FixedAngleJoint
that is used to make the box stay on top of the wheel is pinned to the world's Vector.zero coordinate
 (as can be seen in the FixedAngleJoint's WorldAnchorB property, it's always a Vector2.Zero). So after
 moving away from the World's zero-position the upper boxes is pulled onto the ground. I'm missing this little detail but I have no clue what it could be...


Fixture playerFix; RevoluteJoint wheelMotorRevJoint; private void CreatePlayer() { playerFix = FixtureFactory.CreateRectangle(Engine.World, 5, 5, 1); playerFix.CollisionGroup = 1; playerFix.Body.Position = new Vector2(0f, 0f); playerFix.Body.BodyType = BodyType.Dynamic; playerFix.Body.SleepingAllowed = false; Fixture wheelFix = FixtureFactory.CreateCircle(Engine.World, 2.5f, 1.0f); wheelFix.CollisionGroup = 1; wheelFix.Body.Position = playerFix.Body.Position + new Vector2(0.0f, -5f); wheelFix.Body.BodyType = BodyType.Dynamic; wheelFix.Body.SleepingAllowed = false; wheelFix.Friction = 30.0f; FixedAngleJoint playerFAJ = JointFactory.CreateFixedAngleJoint(Engine.World, playerFix.Body); playerFAJ.BodyB = wheelFix.Body; //playerFAJ.TargetAngle = 0f; // Default == 0 anyway //playerFAJ.BodyB = wheelFix.Body; //RevoluteJoint playerRJ = JointFactory.CreateRevoluteJoint(playerFix.Body, wheelFix.Body, playerFix.Body.LocalCenter + new Vector2(0, -3f)); //JointFactory. wheelMotorRevJoint = JointFactory.CreateRevoluteJoint(playerFix.Body, wheelFix.Body, new Vector2(0, -0.5f)); wheelMotorRevJoint.MaxMotorTorque = 100.0f; wheelMotorRevJoint.MotorEnabled = true; Engine.World.AddJoint(wheelMotorRevJoint); }

 

 

Dec 15, 2010 at 1:34 PM

This may be the function you are looking for:

AngleJoint CreateAngleJoint(World world, Body bodyA, Body bodyB)

Dec 15, 2010 at 2:29 PM

Thanks for your reply. Butt hat didn't work either. With a regular AngleJoint the box is 'melted together' with the wheel. So when the wheel turns the body will hit the ground after the wheel turned for 45 degrees. If I would me it impossible for the box to rotate than that will mean that the wheel won't be able to rotate either since they are now 'melted together'.

 

            AngleJoint playerAJ = new AngleJoint(playerFix.Body, wheelFix.Body);
            Engine.World.AddJoint(playerAJ);

Developer
Dec 15, 2010 at 3:49 PM

Not quite sure, but shouldn't your setup look something like this:

Fixture playerFix;
        RevoluteJoint wheelMotorRevJoint;
        private void CreatePlayer()
        {
            playerFix = FixtureFactory.CreateRectangle(Engine.World, 5, 5, 1);
            playerFix.CollisionGroup = 1;
            playerFix.Body.Position = new Vector2(0f, 0f);
            playerFix.Body.BodyType = BodyType.Dynamic;
            playerFix.Body.SleepingAllowed = false;

            Fixture wheelFix = FixtureFactory.CreateCircle(Engine.World, 2.5f, 1.0f);
            wheelFix.CollisionGroup = 1;
            wheelFix.Body.Position = playerFix.Body.Position + new Vector2(0.0f, -2.5f);
            wheelFix.Body.BodyType = BodyType.Dynamic;
            wheelFix.Body.SleepingAllowed = false;
            wheelFix.Friction = 30.0f;

            FixedAngleJoint playerFAJ = JointFactory.CreateFixedAngleJoint(Engine.World, playerFix.Body);
            playerFAJ.BodyB = wheelFix.Body;
            //playerFAJ.TargetAngle = 0f; // Default == 0 anyway
            //playerFAJ.BodyB = wheelFix.Body;
            //RevoluteJoint playerRJ = JointFactory.CreateRevoluteJoint(playerFix.Body, wheelFix.Body, playerFix.Body.LocalCenter + new Vector2(0, -3f));
            //JointFactory.

            wheelMotorRevJoint = JointFactory.CreateRevoluteJoint(playerFix.Body, wheelFix.Body, new Vector2.Zero);
	    // or wheelMotorRevJoint = JointFactory.CreateRevoluteJoint(wheelFix.Body, playerFix.Body, new Vector2(0f, -2.5f));
            wheelMotorRevJoint.MaxMotorTorque = 100.0f;
            wheelMotorRevJoint.MotorEnabled = true;
            Engine.World.AddJoint(wheelMotorRevJoint);
        }
Dec 15, 2010 at 4:44 PM

Ah yes now it works :). Thank you.

Just a question tough: When the debugView is drawn there is a line from the center of the box to the World's zero position. The line is always there. Won't that cause any problems?

I also suppose that the LinearDragCoefficient is replaced by something else because the player needs to move immediately instead of slowly moving faster.

Developer
Dec 16, 2010 at 2:12 AM
Edited Dec 16, 2010 at 2:13 AM
napoleonite wrote:

Just a question tough: When the debugView is drawn there is a line from the center of the box to the World's zero position. The line is always there. Won't that cause any problems?

I think thats just debugView related. Non-fixed joints have two anchors as they always connect two bodies. Fixed joints are derived from the same base class and just don't use the second anchor, thus it is always Vector2.Zero. So it is just a visualization issue. Makes some sense on the other hand, as fixed joints are somewhat anchored within the world ;)

Coordinator
Dec 16, 2010 at 2:34 AM

That "issue" is fixed in Farseer Physics Engine 3.2