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

Author Topic: Simple Collision Detection - BoundingBoxTest  (Read 2616 times)

0 Members and 1 Guest are viewing this topic.

bglaze

  • Newbie
  • *
  • Posts: 46
    • View Profile
Simple Collision Detection - BoundingBoxTest
« on: October 15, 2011, 12:56:52 pm »
I am using the simple collision detection modules found here:
http://www.sfml-dev.org/wiki/en/sources/simple_collision_detection

I am using this with a snake game to test whether or not my snake eats his food.

The BoundingBoxTest seems to create a box that is nearly twice the size of my sprite image. My snake will eat his food if I am merely passing beside the food on the grid. Is there any way to fix this, such as shrinking the size of the bounding box in this code?

I don't need pixel perfect detection, but I don't want to eat my food by merely passing next to my food either.

Thanks so much!!

Haikarainen

  • Guest
Simple Collision Detection - BoundingBoxTest
« Reply #1 on: October 27, 2011, 08:11:41 am »
Learn how to do it yourself! Boundingbox is very basic and is recommended to learn if you want to progress your skills and get a deeper understanding on wtf you are doing while coding.

You wanna check 4 things to see if 2 boxes intersect. Lets call the boxes A and B.

Check if A is to the right of B
Code: [Select]
if(A.xpos > B.xpos)

Also check if A is to the left of B's right side(B's position + B's size)
Code: [Select]
if(A.xpos < B.xpos+B.xsize)

Then do the same 2 checks for the Y-axis, since we're dealing with 2 dimensions. What we end up with is:
Code: [Select]
if(A.xpos > B.xpos && A.xpos < B.xpos+B.xsize && A.ypos > B.ypos && A.ypos < B.ypos+B.ysize   ){
   // A intersects B, wich means they collide !
}else{
   // They do not collide
}


SFML have a function to check this though, and I recommend you use it. Actually there is 2 functions; One checks if A is fully inside B (If B contains A), the other one checks if they overlap(intersects/collide).

You have them documented here; http://sfml-dev.org/documentation/2.0/classsf_1_1Rect.php#a7343feda13d5e005182dc4fd893a6f4b


EDIT; Actually, I'm very tired and the code i provided is wrong, I didn't take into account A's size in any check. Go figure ;)

bglaze

  • Newbie
  • *
  • Posts: 46
    • View Profile
Simple Collision Detection - BoundingBoxTest
« Reply #2 on: October 30, 2011, 02:59:10 am »
Haikarainen,

Thank you for your help, man. You are right, I do want to learn how to do this on my own, and from your advice, it should be very easy. Thank you so much!

And I didn't know SFML had a way to check this. I thought I read somewhere that collision wasn't something they wanted to implement. However, I must've been wrong, and I will now check out the Rect function.

Thanks again, so much!

bglaze

  • Newbie
  • *
  • Posts: 46
    • View Profile
Simple Collision Detection - BoundingBoxTest
« Reply #3 on: October 30, 2011, 03:54:45 am »
Quote from: "Haikarainen"
EDIT: Actually, I'm very tired and the code i provided is wrong, I didn't take into account A's size in any check. Go figure ;)


Well, I am close. Here is what I have so far. However, it does no detect if the bottom left corner or the top right corner have collided with the other sprite.

Code: [Select]

bool Freighter::Collides(sf::Sprite* sprite)
{
    if(this->GetPosition().x > sprite->GetPosition().x
       and this->GetPosition().x < sprite->GetPosition().x + sprite->GetSize().x
       and this->GetPosition().y > sprite->GetPosition().y
       and this->GetPosition().y < sprite->GetPosition().y + sprite->GetSize().y){
           return true;

    }else if(this->GetPosition().x + this->GetSize().x < sprite->GetPosition().x + sprite->GetSize().x
             and this->GetPosition().x + this->GetSize().x > sprite->GetPosition().x
             and this->GetPosition().y + this->GetSize().y < sprite->GetPosition().y + sprite->GetSize().y
             and this->GetPosition().y + this->GetSize().y > sprite->GetPosition().y){
                 return true;
    }else{
        return false;
    }
}


sf::Rect::Intersects() looks promising, however, I can't figure out how to get my sprites' Rects, and also how to use this function.

Does anyone have experience with this that might be able to enlighten me on a good way to give each of my sprites a Rect, and then use those rects to check for intersecting using
sf::Rect::Intersects()?

 

anything