Slick2D + LWJGL collision system
- by Connor W
So I've been learning java for a while and have explored slick and lwjgl before but went away from using Slick for a while. But I've recently gone back to using it (as I'm making a platformer and Tiled will be really helpful). But here's where my problems begin: collision.
I have a player polygon and I check to see if it's colliding with my tiled map with this method:
public static boolean playerCollisionWith() {
for(int i = 0; i < Blockmap.entities.size(); i++) {
Block entity1 = (Block) Blockmap.entities.get(i);
if(playerPoly.intersects(entity1.poly)) {
return true;
}
}
return false;
}
This would work normally but I'm using a different method for movement. Instead of just adding a speed variable to the player's x axis. I move like this:
if(Keyboard.isKeyDown(Keyboard.KEY_RIGHT)) {
speedX = Math.min(5, speedX + 1);
moving = true;
playerPoly.setX(x);
if(playerCollisionWith()) {
speedX = -5;
playerPoly.setX(x);
}
}
That Math.min call is what is messing me up =. I can't just call speedX = -5, because when I do the player "bounces" when the right mouse button is down and it's colliding. Bounces as in flashes back and forth REALLY quickly. But I don't really know how I would make it so that collisions on the y axis would work either, whether the player is jumping or not. So if I could get some help with how to fix this problem that would be great.
Thank you for the help!