[SOLVED] Collision detection, when hitting head

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.

Your code is filled with accidental code tags, making it really hard to read it. Try to fix it… xD

You’re most likely calculating checkBrickTop() wrongly. Instead of trying to find the error in the position and then subtracting it from the position, why not just snap (=set) the player position to the top of the brick? It should be much less error prone.

Did that now, thanks :slight_smile:

I believe you problem is in the checkBrickBottom(). You are subtracting the difference, which will put your player higher on the screen. You want your player lower. Try adding the diff value.

I just changed it to calculate the players location on top of the brick, and set it to that location ::slight_smile:
Thanks for the reply though,that was probably my problem.