Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Best way to detect Mario style collision?  (Read 1185 times)

0 Members and 1 Guest are viewing this topic.

Canvas

  • Full Member
  • ***
  • Posts: 107
    • View Profile
Best way to detect Mario style collision?
« on: July 07, 2014, 10:31:07 pm »
Hello there people,

At the moment I'm developing a very simple Mario clone for fun, but I have hit a wall ( you may say :D ). At the moment I'm trying to figure out the best way to detect collision on the ground or a wall, at the moment I'm just checking a rectangle is colliding with another rectangle. Here is the basic code I use to check for collisions

bool CollisionService::PointInRect(int pnt_x, int pnt_y, int rect_x, int rect_y, int rect_w, int rect_h)
{
        if ((pnt_x >= rect_x) && (pnt_x <= rect_x + rect_w - 1))
        {
                if ((pnt_y >= rect_y) && (pnt_y <= rect_y + rect_h - 1))
                {
                        return true;
                }
        }
        return false;
}

bool CollisionService::RectToRectCollision(int rect1_x, int rect1_y, int rect1_w, int rect1_h, int rect2_x, int rect2_y, int rect2_w, int rect2_h)
{
        // top-left cornerif ( pointInRect(rect1_x, rect1_y, rect2_x, rect2_y, rect2_w, rect2_h) ){return true;}
        // top-right corner
        if (PointInRect(rect1_x + rect1_w - 1, rect1_y, rect2_x, rect2_y, rect2_w, rect2_h)){ return true; }
        // bottom-right corner
        if (PointInRect(rect1_x + rect1_w - 1, rect1_y + rect1_h - 1, rect2_x, rect2_y, rect2_w, rect2_h)){ return true; }
        // bottom-left corner
        if (PointInRect(rect1_x, rect1_y + rect1_h - 1, rect2_x, rect2_y, rect2_w, rect2_h)){ return true; }
        // Check to see if rectangle 2 is hit any part of rectanlge 1
        // top-left corner
        if (PointInRect(rect2_x, rect2_y, rect1_x, rect1_y, rect1_w, rect1_h)){ return true; }
        // top-right corner
        if (PointInRect(rect2_x + rect2_w - 1, rect2_y, rect1_x, rect1_y, rect1_w, rect1_h)){ return true; }
        // bottom-right corner
        if (PointInRect(rect2_x + rect2_w - 1, rect2_y + rect2_h - 1, rect1_x, rect1_y, rect1_w, rect1_h)){ return true; }
        // bottom-left corner
        if (PointInRect(rect2_x, rect2_y + rect2_h - 1, rect1_x, rect1_y, rect1_w, rect1_h)){ return true; }
        // If there is no collision
        return false;
}

Now what I am thinking is, I have two rectangles, one which is the characters height but has 2 pixels less width so this can be checked against falling and another rectangle which has 2 less height so it can be checked against walking left and right into a block. Check my first image to see what I mean by the two rectangle solution. If anyone has a better way of checking for collisions or over all a way better solution please let me know as I do feel my solution isn't a great one.

Strelok

  • Full Member
  • ***
  • Posts: 139
    • View Profile
    • GitHub
« Last Edit: July 08, 2014, 12:32:12 am by Strelok »

 

anything