Skip to main content
GameDev.net gamedev.net
🔒 Locked 🎮 Unity

Better character movement in a 2D sidescroller

Started by Essel May 27, 2009 at 6:40 PM 2 replies 1.5k views
Original Post
Essel
Essel
So I'm making a 2D side scrolling action/platformer with a few friends as a sort of side project, and I've ran into the problem of creating nice character movement. You see, what I want is a character that flows pretty well, with his speed increasing gradually as he keeps moving in one direction, when they jump, it goes up pretty fast, then gets slower and slower as they get higher, until they are forced back down. What I have currently is a boring when you press the left/right the character moves in that direction at that set speed. Currently my movement system works this way: Player Presses Right -> Game checks input and adds velocity -> Game then draws the updated player at the end of the game loop, then repeat. I'll include my code for reference, if you want. But mind you it's a bit messy right now. The code in question would be in CBaseMovingEnt::Move(), CBaseMovingEnt::Update(), CPlayer::HandleInput(). Code: http://www.artisbigshirts.com/sdl_game.rar Any help would be greatly appreciated, as a I just can't seem to wrap my head around this concept.
kru
kru
When the player initially engages movement, set some initial velocity. Then multiply the current velocity by some factor to get a smooth curve of acceleration.


For example, in your button handler:

hero.moveVelocity = some_static_velocity;


and in your game loop:

hero.moveVelocity += hero.moveVelocity * 0.3;
hero.position += hero.moveVelocity;

Of course you'd have to bound it for maximum speed and all that stuff. Getting a smooth movement is a matter of playing around with the initial value and the multiplier. Suit it to your tastes.

My opinion, though, is that instant movement is a good thing in a platformer. Listen to some of the comments about Knytt, within the first minute of this design review: http://blog.wolfire.com/2008/12/knytt-stories-design-tour/

Tight, responsive controls make the game. Loose, slow controls would have reduced the fun of the game.
Essel
Essel
Thanks for the help man, I understand what you mean about the movement, but I'd still need that sort of thing for player jumping.
pulpfist
pulpfist
What I'm thinking is that your key presses should manipulate acceleration, not velocity. The velocity is just a calculation like x_velocity = x_velocity + x_acceleration. (Remember that acceleration can be a negative number also) Or something like that.

You can use the same principal for jumping.
You got one "acceleration" that pulls the character down all the time. Commony known as gravity. This value would typically be fixed as in real life.
When you have that working, all you need to do to jump is to add a certain amount of acceleration in the up-direction (negative y), to make a jump.

The gravitation will constantly "eat" on the y_acceleration, until it reaches zero, turns around, becomes positive, which will start moving the character down again.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.