I am quite new to game development and have successfully modelled a 3D terrain, and have a camera that is able to freely move around in the game world, I have however run into a snag that I am unable to figure out.
Currently, to move forward / backwards I have methods as follows:
void Player::moveForward(const float speed)
{
Vector3 pos = getPosition();
float cosYaw = cosf(degreesToRadians(m_yaw));
float sinYaw = sinf(degreesToRadians(m_yaw));
pos.x += float(cosYaw)*speed;
pos.z += float(sinYaw)*speed;
setPosition(pos);
}
Where Vector3 is a simple X, Y, Z struct, and degrees to radians is:
inline float degreesToRadians(const float degrees) {
const float PIOver180 = 3.14159f / 180.0f;
return degrees * PIOver180;
}
To handle moving forward I simply place a positive float into the argument, and to move backwards a negative float.
This allows me to of course move forward and backwards - I can also move upwards by changing the pos.y
of the position however I can't figure out how to side step / strafe left and right.
Flipping the x
and z
results in me being able to side step provided the yaw is 360/0, 270, 180 or 90 however anywhere between them angles causes the camera to forward-sidestep/backward-sidestep or move forward/backwards.
Anyway, this had been bothering me so I attempted to find a solution on paper - this worked:
inline float degreesToStrafe(const float degrees)
{
const float PIOver90 = 3.14159f / 90.0f;
return degrees * PIOver90;
}
However, in game this is exactly the same as swapping the x
and z
results.
Any help would be greatly appreciated; I struggled to find anything on the web regarding this however it's possible I was just searching incorrectly!