Search code examples
collision-detectionprocessingcollision

How would I create rectangle collisions in processing?


I am working on a platformer and I need some very basic collisions that I can't seem to come up with myself. It always seems to find a way to glitch through and fly off the screen, or get stuck to the ceiling.

Here's what I have so far:

    if (player.y-8+player.height>loc.y && player.y<loc.y+height-8) {
      if (player.x+player.width>loc.x && player.x<loc.x+8) {
        player.xvelocity=0;
        player.x = loc.x-player.width-1;
      }
      if (player.x+8>loc.x && player.x<loc.x+width) {
        player.xvelocity=0;
        player.x = loc.x+dim+1;
      }
    }



    if (player.x+player.width>loc.x && player.x<loc.x+width) {
      if (player.y+player.height>loc.y && player.y<loc.y+8) {
        player.yvelocity=0; 
        player.y = loc.y-player.height;
        ground = true;
      }
      if (player.y+8>loc.y && player.y<loc.y+height) {
        player.yvelocity=0; 
        player.y = loc.y+dim;
      }
    }

The width of the player is 15 and the height is 25, the width of the square is 16 and the height is 16.

I have tried looking elsewhere on the internet for an answer to this but all I get is collision detection (checking if two objects are intersecting) which I already know how to do. Thanks.

Also I am working on the collisions from the square/rectangle.


Solution

  • Ah, I found a solution to the problem. For people also having difficulty with collision detection and handling, check out this http://www.hobbygamedev.com/adv/2d-platformer-advanced-collision-detection/ it helped me.