#### Archived

This topic is now archived and is closed to further replies.

# Movement control - What is best?

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

## Recommended Posts

What is the best way to move a player around a 2D plane? Should I have 4 velocities (left, right, up, down), just x-axis and y-axis velocities, or something completely different? If I want to move an object up or left (decreasing x or y) should the necessary velocities be negative, or should they be positive and subtracted from the object's x or y? Thanks in advance! [edited by - SteaIth on April 13, 2004 10:21:08 PM]

##### Share on other sites
If I were you I would store velocity as just an x and y axis velocity (two floats), or better yet a 2D vector! Which ever you prefer.

Remember, though -x = left, +x = right, -y = up, +y=down (those y''s are especially confusing coming from typical math or 3D programming)

##### Share on other sites
go with the 2 variable solution. At least for simple games (pacmanish). Use negatives for decreasing x, y.
ex. Xpos += directionx;
Ypos += directiony;
Then if you want constant movement do this every cycle. If you only want it to move on user input just do it everytime he presses the proper keys

##### Share on other sites
Usually it's just x, y. How you define which way is positive and which negative is entirely up to you, no one way is better that the other. The only real reason to use one above the other is that it may be advantagous to do it the way your graphics system has defined x and y coords.

There is also radial velocity (r') and angular velocity (ω ), but that's mostly mathematical are rarely used in games.

[edited by - Doc on April 13, 2004 10:52:41 PM]

##### Share on other sites
just one struct

struct Vector{    float x;    float y;};Vector Vel;  // player velocityVector Pos;  // player position

moving down, y is positive (remember, it''s in screen space), moving up, y is negative.

moving left, x is negative
moving right, x is positive.

although I''d recommend you use a right handed coordinate system (y increases as you move up).

so it will be like

Player.Pos.x += Player.Vel.x;
Player.Pos.y += Player.Vel.y;

Vel being either positive or negative along x and y.

then later on, if you want to move on with vector maths (addition, multiplications, substraction, dot products, divisions, cross products...), it will be straight forward.

things like

struct Vector{    float x;    float y;    Vector& operator += (const Vector& A)       { x += A.x; y += A.y; return (*this); }    Vector  operator +  (const Vector& A) const { Vector Temp(*this); return (Temp += A); }};

allows you to do

Player.Pos = Player.Pos + Player.Vel;

or

Player.Pos += Player.Vel;

##### Share on other sites
How would I make different forces (gravity, on-collision events, etc) move the object? Would I alter the object''s velocity, or its position? Should each force that effects the object''s position have its own velocity, or should everything add up into 1 velocity which is then added to the object''s position?

Again, help appreciated!

##### Share on other sites
What happens is all the various force''s should be summed to get their effect on the object. Say you run into a wall (which stops your movment), bounce off at a velocity of (-4, 1) and gravity is pulling you down at a constant acceleration of (0, -9.81) you could say:

Vector GravityVec(0.0f, -9.81f);
PlayerVelocity = Vector(-4.0f, 1);
PlayerVelocity += GravityVec;

or whatever

Good luck.

##### Share on other sites
if u consider forces, then u wouldnt want to affect the player position directly. try summing up all the forces acting on the object to get the resultant velocity vector. apply that vector to the player position.

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

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

• Total Topics
632967
• Total Posts
3009556
• ### Who's Online (See full list)

There are no registered users currently online

×