Ship Movement

This is a discussion on Ship Movement within the Game Programming forums, part of the General Programming Boards category; Hi everyone!
I am making a shooter game, where the player control a ship. The user moves accelerating with a ...

Ship Movement

Hi everyone!

I am making a shooter game, where the player control a ship. The user moves accelerating with a keyboard key, but moves towards the mouse cursor. I had to use sin and cos functions to discover the ship horizontal and vertical speeds. This works very fine, however, I want to add another functionality. The player must be able to move to left and right (the classical side step). I have discovered a good algorithm for this kind of movement, but it has some problems that I dont know how to solve efficiently. My question, finally, is: does anyone know a good algorithm for this kind of movement (where the user can move ANY direction)?

Are you talking about strafing? Also, I'm unsure if you are talking about 2D or 3D but I will discuss the technique for both.

2D: You should at least have a forward vector for your ship at all times. This way when he spins around you rotate the forward vector and you know which way he is facing. Then presumably to move you simply do something like:

//playerPos and forward are both 2D vectors and k is a real valued scalar (forward should be normalized at all times)
playerPos += forward * k;

To strafe you would simply calculate the vector that cuts the ship in half the other way. You would rotate your forward vector by 90 degrees to obtain this vector. Then you would do the same some or update with this new vector for strafing.

3D: This would be pretty much the same except you probably already have a view system for your ship (uvn) or (right up forward). So you simply use the u vector for this calculation.

"...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers

Hi again!

Thanks for all the help until now!
Sorry, I forgot to tell that my game is 2D. Ok, I will post the piece of code that works for forward movement. In the code, x, y are the mouse coordinates. clWidth and clHeight is the sprite dimension, not very important. The number 4 is the maximum speed (will be a class attribute later).

Now, I know that to do strafe, the Y speed must be equal to X speed, and X equal to Y, the problem is the sign of the variable, remembering that a negative value in Y, the object moves up, and negative in X the object moves left.

You can represent all directions in 2D with 3 basis vectors. Position, X, and Y.

struct vector2D
{
float x;
float y;
};

Second, you can compute the vector between two objects by first subtracting the vectors to produce a third vector and then normalize that vector. The normalization is simply the square root of the dot product of the vector. Or you can store each movement vector as a pre-normalized vector and then rotate the pre-normalized vector. However with this approach you will still have to normalize the resulting vector if you do anything with it.