Collision detection - player gets stuck in platform when jumping

Posted by Sun on Game Development See other posts from Game Development or by Sun
Published on 2012-06-26T21:34:14Z Indexed on 2012/06/27 3:24 UTC
Read the original article Hit count: 232

Filed under:
|
|

So I'm having some problems with my collision detection with my platformer. Take the image below as an example.

enter image description here

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?

© Game Development or respective owner

Related posts about java

Related posts about collision-detection