So, I have a problem with collision response. I save the last good, not colliding position, and then, when the player collides with a wall, I set the position to the previous position. But when I run it, lastGoodPos is ALWAYS set to position. Even when there is collision.. any idea?
public void update()
{
float forward = 0f;
float strafe = 0f;
float vertical = 0f;
if(Keyboard.isKeyDown(Keyboard.KEY_W))
{
forward += 1f;
}
else if(Keyboard.isKeyDown(Keyboard.KEY_S))
{
forward -= 1f;
}
else if(Keyboard.isKeyDown(Keyboard.KEY_A))
{
strafe -= 1f;
}
else if(Keyboard.isKeyDown(Keyboard.KEY_D))
{
strafe += 1f;
}
else if(Keyboard.isKeyDown(Keyboard.KEY_Q) && freeCam)
{
vertical -= 1f;
}
else if(Keyboard.isKeyDown(Keyboard.KEY_E) && freeCam)
{
vertical += 1f;
}
int dx = Mouse.getDX();
int dy = Mouse.getDY();
yrot += dx * 0.5f;
xrot -= dy * 0.5f;
if(xrot > 90)
xrot = 90;
else if(xrot < -90)
xrot = -90;
GL11.glRotatef(xrot, 1, 0, 0);
GL11.glRotatef(yrot, 0, 1, 0);
forward *= Game.graphics.getDelta();
strafe *= Game.graphics.getDelta();
Vector3f movement = new Vector3f();
if(strafe == 0f || forward != 0)
{
movement.x += SPEED * forward * Math.cos(Math.toRadians(yrot + 90));
if(freeCam)
movement.y += SPEED * vertical;
else
movement.y = -0.5f;
movement.z += SPEED * forward * Math.sin(Math.toRadians(yrot + 90));
}
else
{
movement.x += SPEED * strafe * Math.cos(Math.toRadians(yrot + 180));
if(freeCam)
movement.y += SPEED * vertical;
else
movement.y = -0.5f;
movement.z += SPEED * strafe * Math.sin(Math.toRadians(yrot + 180));
}
position.x -= movement.x;
position.z -= movement.z;
bb.setVecMin(new Vector3f(position.x - BBSIZE, 0f, position.z - BBSIZE));
bb.setVecMax(new Vector3f(position.x + BBSIZE, 1f, position.z + BBSIZE));
boolean c = false;
for(BoundingBox b : Game.level.bbs)
{
if(BoundingBox.isCollided(bb, b))
{
c = true;
break;
}
}
if(c)
{
System.out.println("Collided!");
position = lastGoodPos;
}
else
lastGoodPos = position;
GL11.glTranslatef(-position.x, -0.5f, -position.z);
}