[Src] Universal rectangular collision detection
#1
03-17-2012, 08:19 PM
be gentle

 Join Date: Nov 2006 Location: Canada Referrals: 15 Posts: 5,745

[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.
#2
03-19-2012, 06:06 AM
Guru

 Join Date: Aug 2007 Posts: 1,002

Re: [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?
#3
03-19-2012, 02:19 PM
be gentle

 Join Date: Nov 2006 Location: Canada Referrals: 15 Posts: 5,745

Re: [Src] Universal rectangular collision detection

Quote:
 Originally Posted by The Fat Controller 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?
Not sure if tunnelling affects it, I have yet to hear from my project leader
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:
__________________
#4
03-20-2012, 07:00 AM
Forum Addict

 Join Date: Jul 2008 Posts: 276

Re: [Src] Universal rectangular collision detection

Oh man, overloaded functions!
__________________
Quote:
 Originally Posted by Jave` Vouch for McMac04, i sold him a pin for sara plate, He went first.
Quote:
 Originally Posted by AMS I vouch for McMac04. He went first while i was selling him a pin. He is very curious about his gp and loves it alot. Thats wat i found out while trading him.
Successful Trades:7
Times I have been Scammed:3
Last Trade: Sold 'F2P Tank' for 33M [3/20/2012]
#5
05-06-2012, 05:35 PM
Member

 Join Date: Jun 2009 Posts: 31

Re: [Src] Universal rectangular collision detection

This is why I use radial detection for everthing =)

 Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 Thread Tools Display Modes Linear Mode

 Posting Rules You may not post new threads You may not post replies You may not post attachments You may not edit your posts vB code is On Smilies are On [IMG] code is On HTML code is OffTrackbacks are Off Pingbacks are Off Refbacks are Off

All times are GMT +1. The time now is 09:38 PM.

Powered by vBulletin®
Copyright ©2000 - 2015, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.6.1