Collision detection - player gets stuck in platform when jumping
- by Sun
So I'm having some problems with my collision detection with my platformer. Take the image below as an example.
When I'm running right I am unable to go through the platform, but when I hold my right key and jump, I end up going through the object as shown in the image, below is the code im using:
if(shapePlatform.intersects(player.getCollisionShape())){
Vector2f vectorSide = new Vector2f(shapePlatform.getCenter()[0] - player.getCollisionShape().getCenter()[0],
shapePlatform.getCenter()[1] - player.getCollisionShape().getCenter()[1]);
player.setVerticleSpeed(0f);
player.setJumping(false);
if(vectorSide.x > 0 && !(vectorSide.y > 0)){
player.getPosition().set(player.getPosition().x-3, player.getPosition().y);
}else if(vectorSide.y > 0){
player.getPosition().set(player.getPosition().x, player.getPosition().y);
}else if(vectorSide.x < 0 && !(vectorSide.y > 0)){
player.getPosition().set(player.getPosition().x+3, player.getPosition().y);
}
}
I'm basically getting the difference between the centre of the player and the centre of the colliding platform to determine which side the player is colliding with. When my player jumps and walks right on the platform he goes right through. The same can also be observed when I jump on the actual platform, should I be resetting the players y in this situation?