moving and jumping in side-scrolling game

May 18, 2008 at 9:56 AM
I'm programming a side-scrolling game with XNA and Farseer

For moving the master forward and backward,

if(Keyboard.GetState().IsKeyDown(Keys.Left)) master.body.applyforce(new vector(-100,0));
if(Keyboard.GetState().IsKeyDown(Keys.Right)) master.body.applyforce(new vector(100,0));

It works, but it moves faster and faster. Is there a solution to set a speed limit ?

And for moving upward, if I use the method above

if(Keyboard.GetState().IsKeyDown(Keys.Up)) master.body.applyforce(new vector(0,100));

The master will be just a rocket. ( I think you can imagine about that )

So is there a method to detect whether master is on ground ( or on something ) then it can jump, or master is on air and we don't applyforce to it.

Thanks for answering
Coordinator
May 18, 2008 at 11:07 PM

You could limit the speed by increasing the bodies drag force, that way once the velocity gets large, the drag force will also get large and resist acceleration.

You could also set a max speed and whenever you go above it, adjust your velocity vector.

May 19, 2008 at 5:12 AM
Thanks, I've solved horizontal but vertical

I think I need to know when master occur
collision, which edge is touched

I mean, I use a
rectangle as master in physics world, it has 4 edges. So I just have to keep the rectangle without rotation ( body.rotation=0; in update method ) and concentrate on the foot side edge is collided or not. If foot edge did, master can jump; if it have not, then jump button will not work.

Is what I think is worked ? Please tell me if it did and how to detect the foot is collided.

Thank you again
May 19, 2008 at 6:40 AM
Edited May 19, 2008 at 6:50 AM
I had a side scroller project some time ago and spent a lot of time playing around with the control physics. Here is how I did the physics for the character control.

0) (I thought of this last minute and didn't want to renumber my list lol) Also I made the geometry for the character a circle, not a square. This works tons better than the square when you have angled platforms, because the friction will be the same no matter what angle the platform is at.
1) As crashlander said, I put a limit on the speed, however I split it into horizontal and vertical speed limits. This is easier to calculate and I wanted my vertical velocity limit to be higher than the horizontal limit so you could still fall from a tall height and get that "falling" feel. Modify the velocity just before you do a physics simulator update, but after you apply any forces/impulses.
2) I started by just drawing the character at the same upright rotation regardless of the body's rotation, but the angular momentum of a circular body negates the friction you want when the character is "standing" on a platform to make your character slide to a natural stop. I reset the rotation and angular velocity to zero twice. Once before all my forces/impulses are applied, and once after, but before the physics simulator is updated. This effectively "locks" the body's angular rotation/velocity.
3) The "jumping" controls were a little tricker. For jumping, I apply a relatively high vertical impulse force on the character's body. The tricky part is detecting when the character is standing on a platform. For this I used a timer and a collision handler. In the collision handler, I enumerated through all of the collision points and calculated the angle of the point to the body. If the angle was in a certain range underneith the body (adjustable depending on how steep of a platform you want your character to be able to jump off of), then the timer is set. While the timer is active, I allowed the character to jump. Otherwise, ther character can't jump. You can adjust the timer value.
4) With the velocity limits, horizontal movement is easier. Just apply a relatively high horizontal force while the left or right buttons are pressed. If you don't want to allow the player to move left or right while in the air, you can check the timer value from 3) and only allow horizontal movement when the player is NOT standing on a platform.
5) I didn't like the way the controls felt with linear drag, either while "walking" on a platform or in the air. It's probably ok in the air but you definately want to turn it off while standing. Just use the timer value to set the linear drag. You should probably turn it off while walking on the platform though. I think it makes the character feel too sluggish.

One idea I had but never tried was regarding friction and walls. I let the player move left and right in midair (it's just more fun that way for the kind of game I was working on), but this caused a problem with walls because the friction would cause you to slowly slide down the wall, which my classmates were more than happy to take advantage of to cheat during my demo :P I'm not sure what the best way to solve this would be, since you would have to adjust the friction of the platform and the player on the fly. Hmm...

That's all I can remember. Let me know how it goes!
-JeroMiya