PDA

View Full Version : not opengl, but c++ (collision)



method5
03-29-2002, 12:24 PM
trying to get simple collision detection working... basically want to see if one box is on the of the other...

-----
|..__|__
|_|_|...|
...|___|
(dont mind the dots, heh)

the top corners of these are..

box1x
box1y

and

box2x
box2y

and they are sized 32x32


ive been trying something and just cant get it to work and wanted to see if someone could help...

for(int i = box1x; i < box1x+32; i++) {
for(int j = box1y; j < box1y+32; j++) {

if(i >= box2x && i <= box2x+32 && j >= box2y && j <= box2y+32) { cout<< "there is a collision"; }

}
}

doesnt seem to be working...

what i wanted that ot do is do through every pixel of the one and see if its in the other.. any ideas?


[This message has been edited by method5 (edited 03-29-2002).]

method5
03-29-2002, 12:57 PM
my example expanded..



#include <iostream.h>

struct point2d {
int x;
int y;
};

struct rect {
point2d p1;
point2d p2;
};

void main(void) {
rect temp;
int size = 32;
temp.p1.x = 10;
temp.p1.y = 10;
temp.p2.x = 40;
temp.p2.y = 40;

for(int i = temp.p1.y; i < temp.p1.y+32+1; i++) {
for(int j = temp.p1.x; j < temp.p1.x+32+1; j++) {
if(i >= temp.p2.y &amp;&amp; i <= temp.p2.y+32) {
if(j >= temp.p2.x &amp;&amp; j <= temp.p2.x+32) {
cout<< "there is a collision"<<endl;
}
}
}
}
}

that should not print anything.. but it does..

chxfryer
03-31-2002, 10:04 PM
check out http://www.gametutorials.com
it has tuts in the opengl section on collision detection. Might help out

gumby
04-01-2002, 09:46 AM
Because they do overlap.
Box 1 upper left = 10,10
Box 2 upper left = 40,40
Box 1 width = height = 32
Box 1 lower right = 42, 42
Box 2 lower right = 72,72

So box 1 lower right corner is
inside box 2.