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).
Code:
// Critical region
WaitForSingleObject(clSemVel, INFINITE);
{
// Update ship angle
int base = x - clX-(clWidth/2); // base of triangle
int height = y - clY-(clHeight/2); // height of triangle
hipotenusa = sqrt(pow(base, 2) + pow(altura,2));
clAngleY = height/hipotenusa;
clTopYSpeed = clAngleY * 4;
clAngleX = base/hipotenusa;
clTopXSpeed = clAngleX * 4;
}
// End of critical region
ReleaseSemaphore(clSemVel, 1, NULL);
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.
Code:
// Critical region
WaitForSingleObject(clSemVel, INFINITE);
{
// User has pressed strafe
int sign_y = 0;
int sign_x = 0;
if( strafe == LEFT ){
sign_y = -1; //for example
sign_x = 1; //for example too
}
if( strafe == RIGHT ){
sign_y = 1; //another example
sign_x = -1; //yes... example
}
cpTopYSpeed = sign_y * clTopXSpeed;
cpTopXSpeed = sign_x * clTopYSpeed;
}
// End of critical region
ReleaseSemaphore(clSemVel, 1, NULL);
The problem is that the sign calculation is too complex for my understanding. If anyone have a better idea ou the algoritm itself, I am grateful.