I recently developed a bit of code in my Player
class that makes the player jump at a height based off the amount of time that the jump button is held down. However, sometimes landing on the ground causes the camera to shake a little. How can I fix this?
Here is my Player class code:
package com.catastrophe.entity;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.geom.Rectangle2D;
import com.catastrophe.graphics.Animation;
import com.catastrophe.graphics.AnimationLibrary;
import com.catastrophe.graphics.Camera;
import com.catastrophe.save.PlayerSave;
import com.catastrophe.world.Level;
import com.catastrophe.world.Tile;
public class Player extends Entity {
public static final int FURBEE = 0;
public static final int MAC = 1;
public static final int PIXIE = 2;
public float speed, health;
public PlayerSave ps;
public boolean left, right, jump;
public Animation leftAnimation, rightAnimation;
public float speedX, speedY;
public int jumpHeight;
public int jumpTimer;
public float gravity;
public int player;
private boolean grounded;
private int coyoteTime;
private int inputBuffer;
public Animation current;
private boolean firstJumpFrame;
public Player(int player, float x, float y, int width, int height, float health, float speed, int jumpHeight, float gravity, PlayerSave ps) {
super(x, y, width, height, health);
this.speed = speed;
this.height = height;
this.ps = ps;
speedX = 0;
speedY = 0;
jumpTimer = 0;
this.player = player;
this.jumpHeight = jumpHeight;
this.gravity = gravity;
this.grounded = false;
this.inputBuffer = 0;
switch(player) {
case FURBEE:
leftAnimation = AnimationLibrary.getAnimation(AnimationLibrary.FURBEE_BACKWARD, 10);
rightAnimation = AnimationLibrary.getAnimation(AnimationLibrary.FURBEE_FORWARD, 10);
break;
case MAC:
leftAnimation = AnimationLibrary.getAnimation(AnimationLibrary.MAC_BACKWARD, 10);
rightAnimation = AnimationLibrary.getAnimation(AnimationLibrary.MAC_FORWARD, 10);
break;
}
leftAnimation.setDimension(width,height);
rightAnimation.setDimension(width,height);
current = leftAnimation;
current.setDimension(width,height);
current.setPosition(x, y);
}
public void tick(Level lvl) {
current.unpause();
if (coyoteTime > 0) {
coyoteTime--;
}
if (inputBuffer > 0) {
inputBuffer--;
}
boolean canJump = (grounded || coyoteTime > 0) && jumpTimer == 0;
if (jump) {
inputBuffer = 10;
firstJumpFrame = true;
}else {
jumpTimer = 0;
}
if (inputBuffer > 0 && canJump) {
jumpTimer = jumpHeight;
speedY = -gravity;
if(firstJumpFrame) {
this.y -= 10;
firstJumpFrame = false;
}
grounded = false;
coyoteTime = 0;
inputBuffer = 0;
}
if (jumpTimer > 0) {
jumpTimer--;
} else {
speedY = gravity;
}
if (left) {
speedX = -speed;
current = leftAnimation;
} else if (right) {
current = rightAnimation;
speedX = speed;
} else {
speedX = 0;
current.imageIndex = 0;
current.pause();
}
x += speedX;
Rectangle2D.Double bounds = new Rectangle2D.Double(x, y, 120, 60);
for (Tile tile : lvl.tiles) {
if (bounds.intersects(tile.x, tile.y, 60, 60) && tile.collision && !tile.semisolid) {
if (speedX > 0) {
x = tile.x - 120;
} else if (speedX < 0) {
x = tile.x + 60;
}
speedX = 0;
}
}
boolean wasGrounded = grounded;
grounded = false;
y += speedY;
bounds = new Rectangle2D.Double(x, y, 120, 60);
for (Tile tile : lvl.tiles) {
if (bounds.intersects(tile.x, tile.y, 60, 60) && tile.collision) {
if (speedY > 0) {
y = tile.y - 60;
grounded = true;
speedY = 0;
} else if (speedY < 0 && !tile.semisolid) {
y = tile.y + 60;
speedY = 0;
}
}
}
if (!wasGrounded && grounded) {
coyoteTime = 10;
}
current.tick();
}
public void render(Graphics2D g, Camera camera) {
leftAnimation.setPosition(x, y);
rightAnimation.setPosition(x, y);
current.setPosition(x - camera.x, y - camera.y);
current.render(g);
}
public void switchPlayer(int player) {
this.player = player;
switch(player) {
case FURBEE:
leftAnimation = AnimationLibrary.getAnimation(AnimationLibrary.FURBEE_BACKWARD, 10);
rightAnimation = AnimationLibrary.getAnimation(AnimationLibrary.FURBEE_FORWARD, 10);
break;
case MAC:
leftAnimation = AnimationLibrary.getAnimation(AnimationLibrary.MAC_BACKWARD, 10);
rightAnimation = AnimationLibrary.getAnimation(AnimationLibrary.MAC_FORWARD, 10);
break;
}
leftAnimation.setDimension(width,height);
rightAnimation.setDimension(width,height);
current = rightAnimation;
}
}
If you need more of my code, let me know.
I have tried applying an initial boost to the player's height before they actually jump, when they press the jump button.