I am working on a platformer game for the final year project in HS. However... The collision detection system I use is basically about checking wether a specific part of the character intersects with another block or not.
The collision seems to be working fine except that it causes some bugs such as player king of getting stuck or slowing down when hitting from the sides or gets thrown up when both upper and < or > keys are used.
My question is; How can I improve the collision code in order to avoid such glitches and have sort of 'slippery' collisions?
here is a preview of how the bounds look
and here is a preview of what it actually looks like in game
(click for better quality)
The code i use for collision detection:
(tempObject are blocks that the player is intersecting with)
if(getBoundsTop().intersects(tempObject.getBoundsBottom())){
y = tempObject.getY() + height;
velY = 0;
System.out.println("Top collision");
}
if(getBoundsBottom().intersects(tempObject.getBoundsTop())){
y = tempObject.getY() - height;
velY = 0;
falling = false;
jumping = false;
//isOnBlock = true;
} else {
falling = true;
//isOnBlock = false;
}
if(getBoundsRight().intersects(tempObject.getBoundsLeft())){
x = tempObject.getX() - this.width;
}
if(getBoundsLeft().intersects(tempObject.getBoundsRight())){
x = tempObject.getX() + this.width;
}
and here is are the bound methods:
public Rectangle getBounds() { return new Rectangle( (int)x, (int)y, (int)width, (int)height ); }
public Rectangle getBoundsTop() {
return new Rectangle(
(int)x,
(int)y,
(int)(width-(width*0.01f)),
(int)(height/2)
);
}
public Rectangle getBoundsBottom() {
return new Rectangle(
(int)x,
(int)(y+(height/2)),
(int)(width-(width*0.01f)),
(int)(height/2)
);
}
public Rectangle getBoundsLeft() {
return new Rectangle(
(int)x,
(int)y,
(int)((int)width*0.15f),
(int)height
);
}
public Rectangle getBoundsRight() {
return new Rectangle(
(int)
((int)x+(width-(width*0.15f))),
(int)y,
(int) ((int)width*0.15f),
(int)height
);
}