• Log in or sign up for an account here: https://www.sythe.org/login

[Src] Universal rectangular collision detection

Blupig

BEEF TOILET

$5 USD Donor
Joined
Nov 23, 2006
Sythe Gold
1,609
Messages
7,145
Age
33
Location
Canada
Valentine's Singing Competition Winner
Sythe Awards 2015 Winner
Sythe Awards 2016 Winner
Sythe Awards 2020 Winner
Member of the Month Winner
MushyMuncher
Gohan has AIDS
Extreme Homosex
World War 3
I'm LAAAAAAAME
Discord Verified
Off Topic Participant
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.
 
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? :)
 
Valentine's Singing Competition Winner
Sythe Awards 2015 Winner
Sythe Awards 2016 Winner
Sythe Awards 2020 Winner
Member of the Month Winner
MushyMuncher
Gohan has AIDS
Extreme Homosex
World War 3
I'm LAAAAAAAME
Discord Verified
Off Topic Participant
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 :o
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:
 
Oh man, overloaded functions!
 
This is why I use radial detection for everthing =)
 
Runescape Gamblingrunescape gambling
Back
Top