Archived

This topic is now archived and is closed to further replies.

kolpo

rectangular collision

Recommended Posts

If I have to rectangulars how can I then know or they collish? so(a and b are rectangulars that can be of any size): bool isCol(int aX, int aY,int aWidth,int aHeight, int bX, int bY, int bWidth, int bHeight) { ? }

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Kolpo:

This is a simple logic/math problem. Just think about it. If one area is anywhere inside the other area, thewn they collide. Now, think mathematicaaly, how can you solve that?

if ((LeftSideRect1 is rightof RightSideRect2 AND
RightSideRect1 is leftof LeftSideRect2) AND ...

You need to solve these kind of problems yourself.

Share this post


Link to post
Share on other sites
BOOL IntersectRect(
LPRECT lprcDst, // pointer to structure for intersection
CONST RECT *lprcSrc1, // pointer to structure with first rectangle
CONST RECT *lprcSrc2 // pointer to structure with second rectangle
);

Share this post


Link to post
Share on other sites
oh found out that it is a very easy problem. I was wrong with trying to find out when they collish. It is a lot more easy to find out when they do NOT collish:

bool isCol(int aX, int aY,int aWidth,int aHeight, int bX, int bY, int bWidth, int bHeight)
{
if ((aX+aWidth) bX+bWidth) return false;
if ((aY+aHeight) bY+bHeight) return false;
return true;
}

Buster, why do you use pointers and the LPRECT lprcDst variabele?

Share this post


Link to post
Share on other sites
Once you have that licked....

When you check that your objects are on the screen (which is also a collision check!) build a stack of the collision positions (one for bullets, one for objects) and then after the main draw method process the stacks against each other!

Thats what I have done in Nebuloids

:-)

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Could you please explain to me how finding the x value of the right side of the first rect, then multiplying that by the x value of the left side of the second rect, and then adding the width of the second rect and finding that it is a non-zero value would tell you that those two rects intercet at ANY point? This just seems totaly insane to me, like it was writen by someone that has no idea what he is doing. It''s probly just that I can''t see how this works out. An explenation would be great, as I am totaly confused here. It shouldn''t work...

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
sorry teh code didn''t post correct(it didn''t post parts of it)


bool isCol(int aX, int aY,int aWidth,int aHeight, int bX, int bY, int bWidth, int bHeight)
{
if ((aX+aWidth)< bX || aX > bX+bWidth) return false;
if ((aY+aHeight)< bY || aY>bY+bHeight) return false;
return true;
}

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
sorry the code didn''t post correct(it didn''t post parts of it)


bool isCol(int aX, int aY,int aWidth,int aHeight, int bX, int bY, int bWidth, int bHeight)
{
if ((aX+aWidth)< bX || aX > (bX+bWidth)) return false;
if ((aY+aHeight)< bY || aY > (bY+bHeight)) return false;
return true;
}

Share this post


Link to post
Share on other sites
All these rect collision functions look different even though they do the same thing so i post yet another one that does the same thing.

  

inline bool intersecting(const Rect& r1, const Rect& r2)
{
return (r1.left <= r2.right) &&
(r1.top <= r2.bottom) &&
(r1.right >= r2.left) &&
(r1.bottom >= r2.top);
}

Share this post


Link to post
Share on other sites