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.