Newbie: OnCollision and Movement

Topics: Developer Forum, Project Management Forum, User Forum
Feb 26, 2014 at 3:01 PM
I am creating a 2D platformer using farseer physics engine, I am having the hardest of times trying to figure out how to use OnCollision, even though I saw this, I am still trying to grasp the concept. This is what I am working with. I want to know when the player is hitting ground so I can have a bool that is called isJumping. When the player is jumping, isJumping is true so that the player does not keep jumping.
(I also want to have a ground bool that will say, "hay your on the ground".)

The problem with this code is that I will get a null reference. I am also only detecting the floor were I am also using red rectangles as well. How do I detect all the objects not just the floor physics object?
public bool OnCollision(Fixture fixtureA, Fixture fixtureB, FarseerPhysics.Dynamics.Contacts.Contact contact)
        {
            if (contact.IsTouching())
            {
                if (fixtureA.Body.GetType() == Person.GetType() & fixtureB.Body.GetType() == game1.floor.GetType())
                {
                    ground = true;
                    isJumping = false;
                }
            }
            return false;
        }
I am also having an issue with the sliding of the player when I let go of the left or right arrow key. The player will just keep going, slowly decelerating. I have gotten it so that the player will slow down more dramatically but its acts like it hit a wall I want a quick stop with about a .1 to .25 delay from the speed the player was going to stopped.

Code:
public void SlowDown()
        {
            float xForce;
            float pushForce;

            Vector2 left;
            Vector2 right;

            KeyboardState keyboardState = Keyboard.GetState();

            xForce = Person.Body.LinearVelocity.X;

            if (Person.Body.LinearVelocity.X > 0 && !keyboardState.IsKeyDown(Keys.Right))
            {
                if (Person.Body.LinearVelocity.X > 1)
                    pushForce = xForce + 1;

                else
                    pushForce = 0;

                right = new Vector2(-pushForce, 0);

                Person.Body.ApplyForce(ref right);
            }
            else if (Person.Body.LinearVelocity.X < 0 && !keyboardState.IsKeyDown(Keys.Left))
            {
                if (Person.Body.LinearVelocity.X < -1)
                    pushForce = xForce - 1;

                else
                    pushForce = 0;

                left = new Vector2(-pushForce, 0);

                Person.Body.ApplyForce(ref left);
            }
            else
            {
            }
        }
Apr 10, 2014 at 6:11 PM
Edited Apr 10, 2014 at 6:11 PM
To detect if I'm on the ground with my PlatformController, I use a thin shape under my character and linked to it, with IsSensor = true.
Then on the event OnCollision, I add to a fixture list the collided fixture, and remove it OnSeparation event.

So basically, if your fixtures list items count is > 0, your character is touching the ground.

And then IsJumping = !IsGrounded ;)

Hope it helps