FPS Sensitive Jumping

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:

  1. My jump height is correct
  2. My movement speed is correct
  3. 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];
	}
}

So yeah… A few things which will lead in a more solid direction. Things get tricky with dynamics / gravity and other related calculations as you have seen.

The quick hack is this.

The next thing to do is this.

Also there are performance problems with your move method. Never create new objects if at all possible in any game loop. A further optimization is to not poll the input device in the move method of your game / entity class! I’ll see if I can get a mostly correct example with the quick hack and these optimizations above mostly worked out in some pseudo-code modifications to your posted class in a follow up post.

Is the gravity speed to high or low? And I assume you’re talking about the value of velocity.y?

Thank you for the replies guys.

I am currently on vacation and will look back into this when I get back!

However to answer your question:
The gravity appears to be lower when my fps drops however the height of my jump is correct. And it is only the velocity.y that feels this effect.
When my fps is at 60 the gravity speed is fine