[Src] Universal rectangular collision detection

Discussion in 'Programming General' started by Blupig, Mar 17, 2012.

[Src] Universal rectangular collision detection
  1. Unread #1 - Mar 17, 2012 at 3:19 PM
  2. Blupig
    Joined:
    Nov 23, 2006
    Posts:
    7,145
    Referrals:
    16
    Sythe Gold:
    1,609
    Discord Unique ID:
    178533992981594112
    Valentine's Singing Competition Winner Member of the Month Winner MushyMuncher Gohan has AIDS Extreme Homosex World War 3 I'm LAAAAAAAME
    Off Topic Participant

    Blupig BEEF TOILET
    $5 USD Donor

    [Src] Universal rectangular collision detection

    Made a cool little class and a couple functions that deal with collision detection. The header file is commented to tell you what each function does.

    Bounds.h
    Code:
    #include <iostream>
    
    #ifndef BOUNDS_H
    #define BOUNDS_H
    
    struct Point // Point struct, some of the functions use or return it
    {
    	float x, y; // x and y coords
    };
    
    class Bounds
    {
    
    public: // public members
    
    	Bounds() // if Bounds is called as "Bounds b1;" then its centre point will be (1,1), width and height will be 2
    	{
    		centre.x = 1.0; // centrepoint X
    		centre.y = 1.0; // centrepoint Y
    		w = 2; // width
    		h = 2; // height
    	}
    
    	Bounds(float _x, float _y) // allows to set centrepoint, but uses default width and height (2 px)
    	{
    		centre.x = _x;
    		centre.y = _y;
    		w = 2;
    		h = 2;
    	}
    
    	Bounds(float _x, float _y, float _w, float _h) // allows to set everything (Bounds b1(50, 50, 5, 5); -- Makes a bounds with centrepoint (50,50), width of 5 and height of 5)
    	{
    		centre.x = _x;
    		centre.y = _y;
    		w = _w;
    		h = _h;
    	}
    
    	void setCentre(float _x, float _y) // dynamically set the centrepoint (use this to move the bounds around)
    	{
    		centre.x = _x;
    		centre.y = _y;
    	}
    
    	Point getCentre() { return centre; } // Return centrepoint
    
    	float getX() { return centre.x; } // Grab centrepoint X
    	float getY() { return centre.y; } // Grab centrepoint Y
    
    	void setW(float _w) { w = _w; } // Set width
    	void setH(float _h) { h = _h; } // Set height
    
    	float getW() { return w; } // Grab width
    	float getH() { return h; } // Grab height
    
    	bool contains(Point p); // Determines if a Point type is inside a Bounds
    	bool contains(float _x, float _y); // Determines if a point is inside a Bounds (manual form)
    
    	Point getA(); // Grab corner A of the bounds
    	Point getB(); // Corner B
    	Point getC(); // Corner C
    	Point getD(); // Corner D
    	// Corners are in order as so:
    	// A---B
    	// |   |
    	// C---D
    
    private: // private members
    	Point centre; // Centrepoint (private)
    	float w, h; // width and height respectively
    
    };
    
    #endif
    Bounds.cpp
    Code:
    #include "Bounds.h"
    
    Point Bounds::getA()
    {
    	Point ret;
    	ret.x = this->getX() - (this->getW()/2);
    	ret.y = this->getY() + (this->getH()/2);
    	return ret;
    }
    
    Point Bounds::getB()
    {
    	Point ret;
    	ret.x = this->getA().x + this->getW();
    	ret.y = this->getA().y;
    	return ret;
    }
    
    Point Bounds::getC()
    {
    	Point ret;
    	ret.x = this->getA().x;
    	ret.y = this->getY() - (this->getH()/2);
    	return ret;
    }
    
    Point Bounds::getD()
    {
    	Point ret;
    	ret.x = this->getC().x + this->getW();
    	ret.y = this->getC().y;
    	return ret;
    }
    
    bool Bounds::contains(float _x, float _y)
    {
    	if (_x < this->getC().x || _x > this->getD().x)
    		return false;
    
    	if (_y < this->getC().y || _y > this->getA().y)
    		return false;
    	
    	return true;
    }
    
    bool Bounds::contains(Point p)
    {
    	return this->contains(p.x, p.y);
    }
    Two functions you'll need:
    Code:
    bool hasCollided(Bounds b1, Bounds b2)
    {
    
    	if (b1.contains(b2.getA()))
    		return true;
    
    	if (b1.contains(b2.getB()))
    		return true;
    
    	if (b1.contains(b2.getC()))
    		return true;
    
    	if (b1.contains(b2.getD()))
    		return true;
    
    	if (b2.contains(b1.getA()))
    		return true;
    
    	if (b2.contains(b1.getB()))
    		return true;
    
    	if (b2.contains(b1.getC()))
    		return true;
    
    	if (b2.contains(b1.getD()))
    		return true;
    
    	return false;
    
    }
    
    bool willCollide(Bounds b1, Bounds b2, float buffer)
    {
    	if (hasCollided(b1,b2) == true)
    		return false;
    
    	b1.setW(b1.getW() + (buffer * 2));
    	b1.setH(b1.getH() + (buffer * 2));
    	b2.setW(b2.getW() + (buffer * 2));
    	b2.setH(b2.getH() + (buffer * 2));
    
    	return hasCollided(b1, b2);
    
    }
    hasCollided returns true if a collision between two Bounds objects is occuring (present collision).

    willCollide returns true if a collision between two Bounds objects is about to occur, depending on the buffer. The buffer is a frame that is built around each Bounds (buffer is in px). Again, returns true if the two buffer frames are overlapping.


    What do you use this for?
    Whatever you want. I'm currently using it for a 2D game I'm developing.
     
  3. Unread #2 - Mar 19, 2012 at 1:06 AM
  4. The Fat Controller
    Joined:
    Aug 16, 2007
    Posts:
    1,003
    Referrals:
    0
    Sythe Gold:
    1

    The Fat Controller Guru

    [Src] Universal rectangular collision detection

    Nice. You might want to add some extra checks to avoid tunneling, assuming it's still a potential problem with your buffers factored in.

    What sort of game is it? :)
     
  5. Unread #3 - Mar 19, 2012 at 9:19 AM
  6. Blupig
    Joined:
    Nov 23, 2006
    Posts:
    7,145
    Referrals:
    16
    Sythe Gold:
    1,609
    Discord Unique ID:
    178533992981594112
    Valentine's Singing Competition Winner Member of the Month Winner MushyMuncher Gohan has AIDS Extreme Homosex World War 3 I'm LAAAAAAAME
    Off Topic Participant

    Blupig BEEF TOILET
    $5 USD Donor

    [Src] Universal rectangular collision detection

    Not sure if tunnelling affects it, I have yet to hear from my project leader :eek:
    And it's a 2D top-down type deal, sort of like Zelda. It's pretty cool...I'm just not a fan of the art assets we've got done, our art team is pretty mediocre D:
     
  7. Unread #4 - Mar 20, 2012 at 2:00 AM
  8. McMac04
    Joined:
    Jul 2, 2008
    Posts:
    279
    Referrals:
    0
    Sythe Gold:
    0

    McMac04 Forum Addict

    [Src] Universal rectangular collision detection

    Oh man, overloaded functions!
     
  9. Unread #5 - May 6, 2012 at 12:35 PM
  10. feyd
    Joined:
    Jun 10, 2009
    Posts:
    31
    Referrals:
    0
    Sythe Gold:
    0

    feyd Member

    [Src] Universal rectangular collision detection

    This is why I use radial detection for everthing =)
     
< Question about IRC | 370+ Java E-books[Organized][Google doc's][No download required] >

Users viewing this thread
1 guest


 
 
Adblock breaks this site