I have been working on a simple game inspired by the old classic "Space War" a 2D top-down battle game where two ships attack each other (multi-player simulated on one keyborad.)
It is coming along well except the movement is not ideal. The goal is this: a ship drifts in the direction and speed in which it was moving unless the player applies acceleration (i.e. Newton's First Law) causing a change in heading and velocity.
I am trying to use vector-type math so(not true "vector data types" but same values anyway) so that if force is applied in direct opposition to the drift, the drift will slow down. I think that my code is annotated clearly:
void ShipClass::accelerateShip1()
{
float fVelX, fVelY, fAccelX, fAccelY, fNewX, fNewY, fNewFacing, fNewVelocity;
//calculate current movement vector (x/y velocity) from fAngleOfMovement, fVelocity
fVelX = dbCOS( dbWrapValue(ship1.fAngleOfMovement) ) * ship1.fVelocity;
fVelY = dbSIN( dbWrapValue(ship1.fAngleOfMovement) ) * ship1.fVelocity;
//calculate acceleration vector given facing and acceleration
fAccelX = dbCOS( dbWrapValue(ship1.fFacing) ) * ship1.fAcceleration;
fAccelY = dbSIN( dbWrapValue(ship1.fFacing) ) * ship1.fAcceleration;
//combine vectors for summation of force
fNewX = fVelX + fAccelX;
fNewY = fVelY + fAccelY;
//convert vector back to angle/velocity
fNewFacing = dbATANFULL( fNewX, fNewY );
fNewVelocity = dbSQRT( (fNewX*fNewX) + (fNewY*fNewY) );
//save new values
ship1.fFacing = fNewFacing;
ship1.fVelocity = fNewVelocity;
}
The results are unpredictable. The ship does not mantain its heading or velocity correctly.
Can someone please tell me why this does not work?
Thanks!