Would this simple bounding box collision detection work?

Started by
5 comments, last by Durfy 16 years, 9 months ago
I know it's just console but i want to know if this would return true like it should? I dont have a c++ compiler on me... compiled on that one site and no errors but does it work? Thanks, durfy

#include<iostream>
int x = 0;
int y = 0;
int width = 10;
int height = 10;

int x1 = 5;
int y1 = 5;
int width1 = 5;
int height1 = 5;

bool collision() {
 if (y + height < y1) {
   return false;
 }

  if (y > y1 + height1) {
    return false;
  }
  if (x + width < x1) {
    return false;
  }
  if (x > x1+width) {
    return false;
  }

  return true;
}

int main() {
  std::cout<<collision();
 return 0;
}


[Edited by - Durfy on July 24, 2007 7:30:47 PM]
Advertisement
Hey Durfy.

It compiles fine, and it does output true, however I would look over the third test you perform. It just doesn't sit quite right with me. See if you can spot it yourself? =)
if ( youreHappyAndYouKnowIt ) clapYourHands;
lol ur right...
if (x + width < width1) {
return false;
}


should be
if (x + width < x1) {
return false;
}




Thanks... Does it do like i want it?
Thanks,
durfy
Why don't you test it yourself as soon as you get back to a computer with a compiler or IDE installed?

Anyway, yeah, it looks like it'll work fine. However, I would rewrite the function to accept the two boxes as arguments, instead of using global values. And for that reason, you may want to write a struct or class for your box, rather than passing around four separate floats for every box. That way, you can reuse the function for every box-box collision check you need to do.
Create-ivity - a game development blog Mouseover for more information.
what about like this?
It compiles and works fine(if this is the proper solution i will make the variables public and use set/getters)
#include <windows.h>class CFObject {public:	int x;	int y;	int width;	int height;	CFObject(int newx, int newy, int newwidth, int newheight) {		x = newx;		y = newy;		width = newwidth;		height = newheight;	}	bool didcollide(CFObject cobject) {		if ((y + height < cobject.y) || 	       (y > cobject.y + cobject.height) ||		   (x + width < cobject.x)||		   (x > cobject.x + cobject.width)) {			   return false;		}		return true;	}};CFObject obj(10,10,50,50);CFObject obj1(65,0,10,10);int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hpInstance, PSTR cmdLine, int iCMD) {	if (obj.didcollide(obj1)) {		MessageBox(NULL, "COLLISION!", "COLLIDED!", MB_OK);	}	else {		MessageBox(NULL, "NO COLLISION!", "NOTHING!", MB_OK);	}	return 0;}

Thanks a lot,
durfy
Some notes on your code:

#include <windows.h>// CFObject is a very undescriptive name for what essentially is a rectangle,// so I've renamed the class:class Rectangle{public:	// This looks fine, but for more precise movement, you may want to use	// floats or doubles. Personally, I stick to floats.	int x;	int y;	int width;	int height;	// I've used an initialization list here: this initializes the member	// variables with the given values, rather than initializing them to	// a default value and assigning them the given value later.	Rectangle(int newx, int newy, int newwidth, int newheight)	:	x(newx), y(newy), width(newwidth), height(newheight)	{	}	// I'm passing a constant reference to a Rectangle here, instead of	// a Rectangle itself: this means that there's no Rectangle being copied,	// but a reference to an existing Rectangle is passed into this function	// instead. The first 'const' means that that Rectangle may not be	// modified. The second 'const' means that this function won't modify	// this Rectangle itself - so it can be called on const Rectangle objects,	// because it guarantees that no changes will be made - only a check is	// done, after all.	bool CollidesWith(const Rectangle& cobject) const	{		// I've rewritten this part slightly: basically you're checking		// a boolean statement, and depending on the result, you return		// true or false. But the statement you're checking already does		// that, so why not return that immediatly?		return !((y + height < cobject.y) || 			(y > cobject.y + cobject.height) ||			(x + width < cobject.x)||			(x > cobject.x + cobject.width));	}};Rectangle obj(10,10,50,50);Rectangle obj1(65,0,10,10);int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hpInstance, PSTR cmdLine, int iCMD) {	if (obj.CollidesWith(obj1)) {		MessageBox(NULL, "COLLISION!", "COLLIDED!", MB_OK);	}	else {		MessageBox(NULL, "NO COLLISION!", "NOTHING!", MB_OK);	}	return 0;}


As for getters and setters, you may want to read this article. In this case, I don't think you'll need specific checks inside your setters, so I'd leave those variabeles just public. Unless you somehow need to constraint these variabeles (now, or later), setters and getters won't gain you much.

And if outside code accesses these variabeles so frequently, then analyze their access. If code continually changes the x and y variabeles, then write a SetPosition(int x, int y) function, and/or a Move(int x, int y) function. That's what the outside code intended to do, after all.
Create-ivity - a game development blog Mouseover for more information.
Thanks a lot dude... i see a lot of valid points there... as for the initializer thing thats awesome i've always written everything out :-) ... Thanks so much for taking the time to respond. Rating++.


Edit also couldn't i just use a rect? Like from the windows api or w/e RECT
Thanks,
-durfy

This topic is closed to new replies.

Advertisement