Search code examples
xnagame-physicsmonogamephysics-enginefarseer

Gravity issue using Farseer Physics


Having an issue with farseer physics im pretty sure the issue is just with this line of code because of the y value. So when i try to move my character when falling he moves left and right normally but his fall slows.

body.LinearVelocity = new Vector2(1,0)

Is there a way to only change the x value of this? Or is there a way to prevent sliding and set a cap on the speed of applyforce() or applylinearimpulse()?


Solution

  • By setting the linear velocity to 1,0 you give the character a horizontal speed of 1 and a vertical speed of 0. So you effectively stop it from falling.

    The following code will do what you would expect as it preserves the vertical speed.

    body.LinearVelocity = new Vector2(1, body.LinearVelocity.y);

    In some (most) cases it is better to apply a force or impulse to the character using body.Apply... this applies a force for a single frame and Farseer will automatically compute the correct velocity. Note that adding the same force or impulse every frame will make the movement speed-up.