My player is a rectangle so is the block and i cant get it to collide properly i get my player thrown of the game camera and I don’t know what am i doing, my code:
in Block :
public void tick() {
if (solid) {
if (this.intersects(getPlayer())) {
intersecting();
}else{
getPlayer().down = true;
getPlayer().up = true;
getPlayer().left = true;
getPlayer().right = true;
}
}
}
public void intersecting() {
if (getPlayer().x > x) {
getPlayer().left = false;
}if (getPlayer().x < x) {
getPlayer().right = false;
}
if (getPlayer().y > y) {
getPlayer().up = false;
}if (getPlayer().y < y) {
getPlayer().down = false;
}
getPlayer().intersecting(this.intersection(getPlayer()));
}
in Player:
public void intersecting(Rectangle r) {
boolean leftorright = false;
boolean upordown = false;
if (r.width < r.height) {
leftorright = true;
} else {
upordown = true;
}
if (leftorright) {
if (x < r.x) {
x -= r.width / 2;
getCamera().xOff += r.width / 2;
} else {
x += r.width / 2;
getCamera().xOff -= r.width / 2;
}
}
if (upordown) {
if (y < r.y) {
y -= r.height;
getCamera().yOff += r.height;
} else {
y += r.height;
getCamera().yOff -= r.height;
}
}
}
This code is just working when colliding with just 1 block when 2 blocks come in contact BOOOOOM player is no more in the center of the screen :\ help?!