collision detection

Started by
8 comments, last by wyrd 19 years, 2 months ago
I have a 10x10 and a 55x55 box, I just cant get the collision detection to work. how would I do this? if you have any code that would help sitting around it would be nice if you could post it. please dont give me gamedevs tut on this.
Advertisement
The simplest way is to take each point in the smaller box and see if it is within the other box. If any one is, you have a collision.
if(bulletx <= enemyx+55 && bullety - 55 > enemyy)
the bullet is 10x10 and the tank is 55x55

here is the screen
X ->
----------------------------------
|..............................................|Y
||.............................................||
|..............................................|V
|..............................................|
|..............................................|
|..............................................|
|..............................................|
|..............................................|
|..............................................|
|..............................................|
|..............................................|
|..............................................|
| .............................................|
------------------------------------

it should work, y doesnt it?
You could do what I did and let Windows handle it for you.
I use the function called 'IntersectRect'. Research it on the MSDN.
Projects:> Thacmus - CMS (PHP 5, MySQL)Paused:> dgi> MegaMan X Crossfire
I dont like windows specific programming, I like my programs to be cross-platform.
Quote:Original post by game mercenary
if(bulletx <= enemyx+55 && bullety - 55 > enemyy)
the bullet is 10x10 and the tank is 55x55

here is the screen
X ->
----------------------------------
|..............................................|Y
||.............................................||
|..............................................|V
|..............................................|
|..............................................|
|..............................................|
|..............................................|
|..............................................|
|..............................................|
|..............................................|
|..............................................|
|..............................................|
| .............................................|
------------------------------------

it should work, y doesnt it?

Do this for each point in the bullet:
if bulletpointx >= rectx && bulletpointx <= rectx+rectwidth &&   bulletpointy >= recty && bulletpointy <= recty+rectheight:      //this point is colliding      collisionFlag = True
Quote:Original post by game mercenary
I dont like windows specific programming, I like my programs to be cross-platform.


Well, I guess you could use somthing like this:

bool point_in_rect(const rect& r, int x, int y){  return (x >= r.left && x < rect.right && y >= rect.top && y < rect.bottom);}bool rect_intersect_rect(const rect& r1, const rect& r2){  rect smaller, bigger;  if (r1.area() >= r2.area()) {    bigger = r1;    smaller = r2;  } else {    bigger = r2;    smaller = r1;  }  if (point_in_rect(bigger, smaller.top, smaller.left) ||      point_in_rect(bigger, smaller.top, smaller.right) ||      point_in_rect(bigger, smaller.bottom, smaller.left) ||      point_in_rect(bigger, smaller.bottom, smaller.right)) {    return true;  }  return false;}


Quote:Original post by game mercenary
if you have any code that would help sitting around it would be nice if you could post it. please dont give me gamedevs tut on this.


Give one some bread, and you'll nourish him one day. Teach one how to make bread and you'll nourish him until then end of his life.

Or something like that.

Regards,
thanks, but I figured it out..... some people pull out their sketh pad and doodle during school, due to the fact I homeschool on my laptop, I just open dev-c++. 1/3 through english I got it.
You know, sometimes for this kind of thing it's better to "pull out your sketchpad and doodle"... in this case, draw the rectangles in intersecting and non-intersecting cases, label the coordinates, stare and think... [smile]
Here's another method:

        BOOL RectIntersect(const RECT& r1, const RECT& r2)	{		if (r1.right < r2.left ||			r1.left > r2.right ||			r1.bottom < r2.top ||			r1.top > r2.bottom) 		{			return FALSE;		}		return TRUE;	}


It simply compares the opposite sides of each rectangle. ie; If the right side of one rectangle is outside the left side of the other, then they obviously aren't intersecting. It does this for each side of a rectangle. Simple and efficient.

This topic is closed to new replies.

Advertisement