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

Author Topic: Trying to understand collision - do you mask a sprite with a rectangle?  (Read 1938 times)

0 Members and 1 Guest are viewing this topic.

HailBee

  • Newbie
  • *
  • Posts: 25
    • View Profile
    • Email
Hey all, my first post here, am studying SFML 2.0 at university but as I dont want to annoy my tutor with every question I hope you don't mind helping me out ;)

I have position = velocity * DeltaTime working wonderfully and objects like the player within their own class so the next step is figuring this out =)

As I understand it would you draw a rectangle over your sprite to check if it collides with another rectangle? Or does the IntRect of the sprite(the box you cut out) suffice for the rectangle needed to detect collision??

My tutor has provided this for an example but im still a little confused:

struct Rectangle
{
        float top;
        float left;
        float bottom;
        float right;
};
 
// Assumes y runs 'downward'.
bool Intersects(const Rectangle& r1, const Rectangle& r2)
{
        if (r1.bottom < r2.top) return false;
        if (r1.top > r2.bottom) return false;
        if (r1.left > r2.right) return false;
        if (r1.right < r2.left) return false;
 
        // Rectangles must be intersecting!
        return true;
}
« Last Edit: June 15, 2013, 04:13:46 am by HailBee »

The Hatchet

  • Full Member
  • ***
  • Posts: 135
    • View Profile
    • Email
Unless you want to specify a rectangle with different dimensions than the sprites own intRect, the sprites is all you need.  Infact the intRect class has an Intersects and Contains method built in.

And your professors example is literally the simplest example of 2 rectangles colliding you could ever write so if it seems a little confusing maybe you want to brush up on those spacial logistics skills.

HailBee

  • Newbie
  • *
  • Posts: 25
    • View Profile
    • Email
Unless you want to specify a rectangle with different dimensions than the sprites own intRect, the sprites is all you need.  Infact the intRect class has an Intersects and Contains method built in.

And your professors example is literally the simplest example of 2 rectangles colliding you could ever write so if it seems a little confusing maybe you want to brush up on those spacial logistics skills.

Thanks! Ill look at working in the Sprites intRect in that case :). Not sure what you mean about studying spacial logistics but guessing you mean further study of geometry.

if (r1.bottom < r2.top) return false;

Tells me if rectangle 1's bottom 'point' is less than rectangle 2's top point then rectangle 2 has overlapped rectangle 1 and (shoudlnt) so return false?

The Hatchet

  • Full Member
  • ***
  • Posts: 135
    • View Profile
    • Email
Upon second look I guess his is a little confusing.  He uses an exclusion way instead of inclusion and some people think the other way.

The easiest way to understand his method is to take some paper and draw pairs of rectangles, some overlapping, some not and label each pair with an R1 and R2 and just run through the IF statements.  If at any point you return false you are not overlapping.

HailBee

  • Newbie
  • *
  • Posts: 25
    • View Profile
    • Email
Instead of working out the implementation of his structure I've been reading https://github.com/eXpl0it3r/Examples/blob/master/SFML/SimpleAABB.cpp

and come up with:

//Practice Collision Detection
                if(theLevel->GetFloor().getGlobalBounds().intersects(aPlayer->GetSprite().getGlobalBounds()))
                {
                        //DO STUFF
                }

I think im on the right track to getting it working now =)