I already have the collisions done, for when the player is falling.
It looks like this:
/**
* Check if we hit a brick, when falling
* @param worldX
* @param worldY
* @param step
* @return
*/
public double checkBrickTop(double worldX, double worldY, double step) {
if (isInsideBrick(worldX, worldY)) {
double shouldBeY = worldY/10 +1 ;
double diff = shouldBeY - worldY;
return step - diff;
} else {
return step;
}
}
if (player.getYVelocity() <= 0) {
// Falling!
if (player.getYVelocity() <= -1) {
player.setYVelocity(-1);
} else {
player.setYVelocity(player.getYVelocity() - 0.1);
}
// Calculate where the motherf**ker’s gonna land
newY = player.getWorldY() + player.getYVelocity();
if (level.isInsideBrick(player.getWorldX(), newY - (player.getHeight() + 2))) {
double yTrans = level.checkBrickTop(player.getWorldX(),
player.getWorldY() - player.getHeight(),
player.getYVelocity());
player.setWorldY(player.getWorldY() + yTrans);
System.out.println(yTrans + " " + player.getYVelocity());
player.setJumping(false);
} else {
player.setWorldY(newY);
}
}
However, I've hit a brick wall now. I need to do roughly the same thing, but for when I hit a brick, when jumping.
Current code looks like this, but it doesn't work correctly - player gets teleported high above when brick is hit:
/**
* Check if we hit a brick, when rising
* @param worldX
* @param worldY
* @param step
* @return
*/
public double checkBrickBottom(double worldX, double worldY, double step) {
if (isInsideBrick(worldX, worldY)) {
double shouldBeY = worldY/10 +1 ;
double diff = shouldBeY - worldY;
return step - diff;
} else {
return step;
}
}
if (player.getYVelocity() > 0) {
// Rising into the air!
player.setYVelocity(player.getYVelocity() - 0.1);
}
double newY = player.getWorldY() + player.getYVelocity();
if (level.isInsideBrick(player.getWorldX(), newY + 1)) {
double yTrans = level.checkBrickTop(player.getWorldX(),
player.getWorldY(),
player.getYVelocity());
player.setWorldY(player.getWorldY() + yTrans);
} else {
player.setWorldY(newY);
}
It's probably something small, I just don't see it.