Jumping with Farseer for a XNA Game

Topics: User Forum
Aug 19, 2008 at 5:05 PM
Ok, I am fairly new to Farseer and Physic Engines in general, so any help  or samples would be great.

Basically I am having trouble with a Jumping mechanic for a side scroller game. Here is what I am doing:

  Vector2 force = Vector2.Zero;
  force.X += 50 * -gamePadState.ThumbSticks.Left.X;
   if(gamePadState.IsButtonDown(Buttons.A))
    {
            force.Y -= 50;
    }
  if(force.Y > -200)
    {
        force.Y = 0;
    }

Problem is, my character just keeps going up and up.
Aug 19, 2008 at 8:15 PM
Edited Aug 19, 2008 at 8:18 PM
Ok, so here is my aproach:
When creating player's geometry add this
playerGeom.Collision += new Geom.CollisionEventHandler(onCollide);

And here is onCollide event handler that checks if player is standing on something and changes variable isJumping to false so we can jump again (put this function wherever you want):


        public bool onCollide(Geom g1, Geom g2, ContactList contactList)
        {
            foreach (Contact contact in contactList)
            {
                    if (contact.Position.Y <= g2.Body.Position.Y
                        && contact.Position.Y > g1.Body.Position.Y
                        && contact.Position.X < playerClass.body.Position.X + (playerClass.Width / 2f) - 6f  //playerClass.Width and Height are dimensions of player's body
                        && contact.Position.X > playerClass.body.Position.X - (playerClass.Width / 2f) + 6f)
                    {
                            playerClass.isJumping = false;  //playerClass is fiction ,change to yours
                    }
            }               
            return true;
        }


Now, time for actual jumping:


                    if (keyboardState.IsKeyDown(Keys.Up) && !lastKeyboardState.IsKeyDown(Keys.Up) && playerClass.isJumping == false) //we have to chack that key is pressed only once, no holding
                    {
                            playerClass.body.Position -= new Vector2(0.0f, 2.5f); //teleport player a little higher so he will not be standing on the ground and treated with onCollide function (it can't allow jumping when standing on the ground ;p)
                            playerClass.body.velocity.Y = -500.0f;
                           playerClass .isJumping = true; //change to true
                    }


I'm holding actual physical body of my player in own Player class. Don't get confused ;)
Aug 19, 2008 at 10:04 PM
wow thanks for the quick reply, and I wasn't confused, your Player class looks alot like mine, I'm not a total newb, just when it comes to physcics.

Any way thanks, i'll try it out soon,
Aug 20, 2008 at 8:32 PM
alright I tried it out and works out exactly as how I want it to, thanks.