In my recent asteroids clone I've encountered a lot of problems with the velocity vector of the ship and the actual rotation angle of the ship.
What I'm currently doing is when the user presses a rotate key (left or right arrow if it has not been re-assigned) I simply change the Z rotation of the ship without altering the velocity vector. The velocity vector is only altered when the user presses the thrust key (up arrow). This makes for a really cool floating type effect where you can actually be heading in one direction but have your ship pointed in another firing on whatever you need to fire on.
Big problem: I cannot clamp the velocity vector to reasonable values using this method. In other words it's possible to speed the ship up to some insane rates - to the point of not being able to distinguish what is going on in the game with your ship.
Code:void GetKeys(void) { Keyboard.Update(); if (Keyboard.KeyDown(AST_LEFT)) { Ship.Rotate(LEFT); Ship.ZRotation.x=cos(Ship.GetAngle()); Ship.ZRotation.y=sin(Ship.GetAngle()); } if (Keyboard.KeyDown(AST_RIGHT)) { Ship.Rotate(RIGHT); Ship.ZRotation.x=cos(Ship.GetAngle()); Ship.ZRotation.y=sin(Ship.GetAngle()); } if (Keyboard.KeyDown(AST_THRUST)) { Ship.VelVector+=(Ship.ZRotation); }
Solutions I've tried:
Keep the velocity vector normalized and multiply the normalized vector by a scalar speed amount.
Problem: Since the speed value is a scalar, you cannot simply increase it every time the user presses the thrust key. This results in the velocity vector overshooting the actual rotation vector - the ship goes nuts.
Clamp the velocity vector components to certain ranges
This works but since you are clamping based on range instead of x/y relation the final velocity vector might not represent the actual rotation vector. For instance clamping x and y to .50 while having a rotation vector of 135 would not work.
Clamp based on the length of the velocity vector
This works until you max the length - then there is no way to lessen the length because the thrust key is effectively locked out, thus the length stays constant.
I'm open to any ideas you might have.