# moving characters correctly

This topic is 3156 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi, At the moment I'm thinking about how objects should move in my game. I'm writing a 2D game, where the camera scrolls with the player (I think these games are called side scrolling games :D) Anyways: The problem is, that I don't know how to write a "system" that moves everything. If the player presses for example [right-arrow] I add velocity to the players velocity. If [left-arrow] is pressed, I subtract velocity. But now I have something like acceleration (because its added every frame). If I don't add velocity, but say "if(rightArrow) {velocity = 100;} else{velocity = 0;}" the problem is, that the system is not very flexible, because later I want areas in my game where the player is (for example) pushed in one direction (or just something like gravity) and then I would just subtract velocity. But thats not possible if I just set the velocity to one fixed mark. etc. I have tried a lot, but nothing works "correctly". So my question is: How is it done? I think I'm not the first one that needs movement in his game, but Google wasn't that helpful :) thank you.

##### Share on other sites
Rather than setting a fixed value when the right-arrow is pressed, start the player with velocity = 0 at the beginning of each frame and add or subtract from that velocity, depending on events.
velocity = 0;if( right_arrow ) velocity += 100;if( being_pushed ) velocity -= 125;

##### Share on other sites
My first Suggestion would be to learn about steering.
That is a system with behaviors like arrive, seek , flee and bunch of other.

at each game cycle steering calculates a force being applied to the character.

You turn on a behavior

Steering->SetTarger(SOME PLACE);
Seering->SeekOn();

force=Steering->calulate()

acceleration=force/characterMass;

velocity+=acceleration

characterPosition+=velocity.

I know this might look a little complicated.

You also need to rotate your character to where it is moving.

SeekFunction is something like this

Vector2 Seek(){
DesiredVelocity=(target-controllingPlayerPos)*MAXSPEEDCHARACTERCANHAVE
return DesiredVelocity - controllingPlayerPos->getCurrentVelocity()

}

I suggest you get this
http://www.amazon.com/Programming-Game-Example-Mat-Buckland/dp/1556220782

IT has a sweet section on this.

##### Share on other sites
It depends on how your physics are supposed to work. Which in turn depends on what kind of game it is - i.e., how the player is getting around. Is the player driving a vehicle? Walking/running and jumping? Flying a space ship?

You have to have something to model, and then model it. After that it becomes a question of actually understanding the physics involved.

But in general, you do want to track acceleration, velocity and position. The update loop adds acceleration (multiplied by time elapsed) to velocity, and then adds velocity (multiplied by time elapsed) to position. (To some extent, you can handwave around the multiplication if you have a fixed timestep, because you can pick the units for each value and don't really have to state them. But this is not really a good idea in the general case; you lose flexibility this way, and you might decide later that fixing the timestep wasn't such a good idea, for example.)

The real modelling comes in when you (a) determine the player's control over those values, and (b) calculate acceleration for each frame.

For a platformer, the player's control might set the X velocity to walk, and set an initial Y velocity at the moment of a jump, for example (but don't re-set it if the key is held). Meanwhile, Y acceleration would be a constant, and there would normally not be any other forces (except for collision rejection by the ground).

For a driving game, the physics could be very complicated, but the player would control a steering-wheel position and a throttle pressure. The pressure would factor into the amount of acceleration (as would the existing velocity, because of drag; not to mention the current gear, ground conditions...), and the wheel position would rotate the acceleration, relative to the current velocity.

For a space game, you might have a thruster on either side of the ship; the average of the thruster forces gives you current acceleration (there is negligible drag or friction in space) and the difference would apply a rotational force.

But the basic process is the same:

User state contains position, velocity, acceleration and {simulation-specific variables representing the user's control}.

When a key is pressed, set the simulation-specific variables.

In the update loop, use the simulation-specific variables to modify acceleration and/or velocity (usually just acceleration, except for something like walking).

Then do the non-specific part: update velocity due to acceleration (since acceleration is the change in velocity over time, and time has elapsed), and position due to velocity (similarly).

##### Share on other sites
thanks for your replies. I think I know how I will do it :)

1. 1
Rutin
30
2. 2
3. 3
4. 4
5. 5

• 13
• 13
• 11
• 10
• 14
• ### Forum Statistics

• Total Topics
632961
• Total Posts
3009494
• ### Who's Online (See full list)

There are no registered users currently online

×