I have a vector from controller input, and a float value representing current rotation. I need to use the controller input to determine the angle of rotation required and then decide which way to turn the player. If the turn is too large then a small increment will be made in the direction of the target rotation., otherwise it will just be set to the target.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
D3DXVECTOR2 down(0,-1); // 2d vector pointing straight down
D3DXVECTOR2 unitDir(UnitVector(D3DXVECTOR2(entityDirection.x, entityDirection.z)));
float angle = acos(DotProduct(down, unitDir)); // difference in angle between 0 and entity direction
if (entityDirection.x < 0)
rotationTarget = angle;
else
rotationTarget = (PI * 2 - angle);
if (abs(entityRotation.y- rotationTarget) <= MAX_ROTATION || abs(entityRotation.y - rotationTarget) > PI * 2 - MAX_ROTATION) // if difference in angles is less than max rotation speed
entityRotation.y = rotationTarget;
else
{
if ( someCondition ) // PROBLEM - I don't know what to put here, the condition should say something equivalent to 'if a right turn to the target rotation is shorter than a left turn to the larget rotation'
entityRotation.y += MAX_ROTATION;
else
entityRotation.y -= MAX_ROTATION;
}
The problem is commented, I can't think of how to tell whether a right turn or a left turn is appropriate, it took plenty of reading just to come up with this code, my maths is not all that great.