I am attempting to create a 3D game using solely LWJGL and Slick-util, and I intend for it to be a first-person game, so I need a way to navigate the map using 3D movement. I have a position Vector3f
and another vector acc
to store acceleration. I have set up the following methods, all bound (in another class) to W, A, S, and D:
public void walkForward()
{
acc.x += walkSpeed * (float) Math.sin(Math.toRadians(yaw)); // Calculations for 3D Movement (please correct me if wrong)
acc.z -= walkSpeed * (float) Math.cos(Math.toRadians(yaw));
}
public void walkBackward()
{
acc.x -= walkSpeed * (float) Math.sin(Math.toRadians(yaw));
acc.z += walkSpeed * (float) Math.cos(Math.toRadians(yaw));
}
public void strafeLeft()
{
acc.x += walkSpeed * (float) Math.sin(Math.toRadians(yaw - 90));
acc.z -= walkSpeed * (float) Math.cos(Math.toRadians(yaw - 90));
}
public void strafeRight()
{
acc.x += walkSpeed * (float) Math.sin(Math.toRadians(yaw + 90));
acc.z -= walkSpeed * (float) Math.cos(Math.toRadians(yaw + 90));
}
Where walkSpeed
is a positive float. Also, in my move()
method, where movement is actually processed, I have the following:
void move()
{
acc.y = 0.0f; // Keep the player's height stable, for testing purposes.
getPosition().x += acc.x; // Add current velocity to the position.
getPosition().y += acc.y;
getPosition().z += acc.z;
if(acc.x > 0.0f) // Gradually bring player's velocity to 0.
acc.x -= 0.01f;
else if(acc.x < 0.0f)
acc.x += 0.01f;
if(acc.y > 0.0f)
acc.y -= 0.01f;
else if(acc.y < 0.0f)
acc.y += 0.01f;
if(acc.z > 0.0f)
acc.z -= 0.01f;
else if(acc.z < 0.0f)
acc.z += 0.01f;
}
Finally, in the render()
method, where transformations are actually made to the game, I have this:
public void render()
{
move();
glRotatef(pitch, 1.0f, 0.0f, 0.0f);
glRotatef(yaw, 0.0f, 1.0f, 0.0f);
glTranslatef(-position.x, -position.y, -position.z);
}
Expected result: Ordinary 3D movement, proper directional motion, etc.
Actual result: Player moves in general direction, speed changes based on both yaw and pitch, releasing all keys (debugging confirms that NO input is being received to the walk()
methods causes jittering and strange movement in random directions, whose speed changes based on both yaw and pitch, holding both W + A or W + D causes a massive increase in horizontal speed, etc.
I have a feeling this could be due to missing a pop or push in the matrix, or forgetting to init the identity somewhere. Any help would be appreciated. Thanks in advance!