I am working a 3D project.
My movement is scaled by a delta factor so that it is not affected by fps drops, however…
When my fps drops:
- My jump height is correct
- My movement speed is correct
- My gravity’s speed is not
Any suggestions as to why this is occurring? (Code below…)
Sorry, I can’t figure out how to put the code in a spoiler tag.
public class Player extends MovableGameObject {
private boolean inAir = false;
private final float GRAVITY = -0.015f;
private final float JUMP_POWER = 0.22f;
private final float WALK_SPEED = 3.5f;
private final float RUN_MODIFIER = 3f;
private final float CROUCH_MODIFIER = 2.5f;
private final Vector3f[] collisionPositions;
private Vector3f hitBox = new Vector3f(0.4f, 1.9f, 0.4f);
private final int collisionPointCount = 8;
private int direction;
public Player(TexturedModel model, Vector3f position, float rotX,
float rotY, float rotZ, float scale) {
super(model, position, rotX, rotY, rotZ, scale);
calculateDirection();
}
public void move(ChunkEngine chunkEngine) {
Vector2f direction = new Vector2f((float)Math.cos(Math.toRadians(rotY)), (float)Math.sin(Math.toRadians(rotY)));
boolean W = Keyboard.isKeyDown(Keyboard.KEY_W);
boolean A = Keyboard.isKeyDown(Keyboard.KEY_A);
boolean S = Keyboard.isKeyDown(Keyboard.KEY_S);
boolean D = Keyboard.isKeyDown(Keyboard.KEY_D);
int runFlag = isRunning ? 1 : 0;
int crouchFlag = Keyboard.isKeyDown(Keyboard.KEY_LSHIFT) ? 1 : 0;
if (isRunning)
System.out.println("Running!");
velocity.x = 0;
velocity.z = 0;
float speedModifier = (WALK_SPEED + RUN_MODIFIER * runFlag - CROUCH_MODIFIER * crouchFlag);
if (W) {
velocity.z += -direction.x * speedModifier * DisplayManager.getDelta();
velocity.x += direction.y * speedModifier * DisplayManager.getDelta();
}
if (S) {
velocity.z += direction.x * speedModifier * DisplayManager.getDelta();
velocity.x += -direction.y * speedModifier * DisplayManager.getDelta();
}
if (D) {
velocity.x += direction.x * speedModifier * DisplayManager.getDelta();
velocity.z += direction.y * speedModifier * DisplayManager.getDelta();
}
if (A) {
velocity.x += -direction.x * speedModifier * DisplayManager.getDelta();
velocity.z += -direction.y * speedModifier * DisplayManager.getDelta();
}
if (Keyboard.isKeyDown(Keyboard.KEY_SPACE) && !inAir) {
inAir = true;
velocity.y = JUMP_POWER;
}
if (!W && !S) {
velocity.z *= 0.9f;
}
if (!D && !A) {
velocity.x *= 0.9f;
}
if (velocity.x > 0) {
if (chunkEngine.getBlockAt(position.x + velocity.x + hitBox.x, position.y, position.z + hitBox.z).getBlockID() != 0
|| chunkEngine.getBlockAt(position.x + velocity.x + hitBox.x, position.y, position.z - hitBox.z).getBlockID() != 0)
position.x -= velocity.x;
}
if (velocity.x < 0) {
if (chunkEngine.getBlockAt(position.x + velocity.x - hitBox.x, position.y, position.z + hitBox.z).getBlockID() != 0
|| chunkEngine.getBlockAt(position.x + velocity.x - hitBox.x, position.y, position.z - hitBox.z).getBlockID() != 0)
position.x -= velocity.x;
}
if (velocity.z > 0) {
if (chunkEngine.getBlockAt(position.x + hitBox.x, position.y, position.z + velocity.z + hitBox.z).getBlockID() != 0
|| chunkEngine.getBlockAt(position.x - hitBox.x, position.y, position.z + velocity.z + hitBox.z).getBlockID() != 0)
position.z -= velocity.z;
}
if (velocity.z < 0) {
if (chunkEngine.getBlockAt(position.x + hitBox.x, position.y, position.z + velocity.z - hitBox.z).getBlockID() != 0
|| chunkEngine.getBlockAt(position.x - hitBox.x, position.y, position.z + velocity.z - hitBox.z).getBlockID() != 0)
position.z -= velocity.z;
}
if (inAir) velocity.y += GRAVITY;
else {
velocity.y = 0;
}
if (chunkEngine.getBlockAt(position.x, position.y-0.5f, position.z).getBlockID() == 0) {
inAir = true;
}
if (velocity.y <= 0) {
if (chunkEngine.getBlockAt(position.x + hitBox.x, position.y + velocity.y, position.z + hitBox.z).getBlockID() != 0
|| chunkEngine.getBlockAt(position.x - hitBox.x, position.y + velocity.y, position.z + hitBox.z).getBlockID() != 0
|| chunkEngine.getBlockAt(position.x - hitBox.x, position.y + velocity.y, position.z - hitBox.z).getBlockID() != 0
|| chunkEngine.getBlockAt(position.x + hitBox.x, position.y + velocity.y, position.z - hitBox.z).getBlockID() != 0) {
position.y -= velocity.y;
inAir = false;
}
}
// Apply velocities to position
super.increasePosition();
rotX -= Mouse.getDY() * 0.1f;
rotY += Mouse.getDX() * 0.1f;
// Post-Movement corrections
y[0] = (int)position.getY();
if (rotX > 90) rotX = 90f;
calculateDirection();
}
public int getX() {
return x[0];
}
public int getY() {
return y[0];
}
public int getZ() {
return z[0];
}
}