#### Archived

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

# 2D rectangular clipping - oh the agony.

This topic is 6966 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Once again, I need the assistance of this forum, and once again, I feel like an idiot. Intellectual capicity aside, I have been twisting my head around the concept of collision detection for the past two days, and I can''t seem to find what''s wrong in my algo'', so I''m asking some relatively fresh minds. Width = Width of this bitmap Height = Height of this bitmap Objects is an array of pointers to bitmaps. x is the amount to move on the x axis, and y is the amount to move along the y axis. This is in a loop in my Move function, which moves an object x in the x axis, and y in the y axis. index starts at zero and is inremented. I know the following will be hard to read due to lack of formatting, but please bear with me. // don''t waste time checking zero if(x) { if(x > 0) { // moving right if(Box.right + x >= Objects[index]->Box.left && Box.right + x < Objects[index]->Box.right && Box.top - y >= (Objects[index]->Box.top - Height) && (Box.bottom + y <= Objects[index]->Box.bottom + Height)) x = 0; } else // moving left if (Box.left - x <= Objects[index]->Box.right && Box.left - x > Objects[index]->Box.left && Box.top - y >= (Objects[index]->Box.top - Height) && (Box.bottom + y <= Objects[index]->Box.bottom + Height)) x = 0; } // end if if(y) { if(y > 0) { // moving down if (Box.bottom + y >= Objects[index]->Box.top && Box.bottom + y < Objects[index]->Box.bottom && Box.left - x >= (Objects[index]->Box.left - Width) && (Box.right + x <= Objects[index]->Box.right + Width)) y = 0; } else // moving up if (Box.top - y <= Objects[index]->Box.bottom && Box.top - y > Objects[index]->Box.top && Box.left - x >= (Objects[index]->Box.left - Width) && (Box.right + x <= Objects[index]->Box.right + Width)) y = 0; } // end if The problems are these: A: A bitmap will appear to get ''stuck'' inside another bitmap, and only be able to move out (the opposite direction on the axis he entered with). B: He will get stuck too far in the bitmap. My observations are these: A: The object appears to go too far into the bitmap before clipping. My reasonings are these: A: The object is getting stuck because it''s going too far in. B: I suspect it''s going too far in because of the code in the if statement that checks if it is within the correct boundaries in the other axis to register a collision. *** If this seems like gibberish, I apoligise for the wasted bandwith, and I''d better stop typing before me fingers cramp up into a ball and whack me repeatedly on the head. Thanks for your help. The_Minister

##### Share on other sites
To test for collision:

RECT rc1;
RECT rc2;
Rect dummy; // you might be able to pass NULL here, can''t remember

if (IntersectRect(&dummy,&rc1,&rc2)) {
collision!
} else {
no collision!
}

##### Share on other sites
*loads shotgun with buckshot and goes out back, mumbling something about better times, sob*

The_Minister

##### Share on other sites
Still, what''s wrong with the way I''m doing it?

The_Minister

##### Share on other sites
Hmm it must be too high for you people, too bad.

I guess it''s back to the drawing board then...

The_Minister

##### Share on other sites
if you think things are getting stuck you need a collision range and a collided with variable

##### Share on other sites
sorry, reverse pshychology doesn''t work.

##### Share on other sites
I love the communication in this forum.

FACT Things are getting stuck
FACT I have a huge algorithm to clip
FACT I have already THOUGHT about detecting collisions, that''s like ''Here''s your burger sir, would you like anything to eat?''.

The_Minister

##### Share on other sites
Hey minister!! We need you at the congregation right NOW!! Please pack your bags and be there by MidKniGhT!

Geez...
OldManDave.

##### Share on other sites
It seems to me that you are trying to see if two rectangular bitmaps are colliding. This is the simpiliest form of collision. What you can do is get the all the sides of each rectangle, and check to see if anysides intersect with anyother sides of the other recangle. If you want to know what''s wrong with your code, its just a litte messy and needs a little restructuring to cut down on code.

Each bitmap should just hold its current x and x velocity, its current y and y velocity, and add the x velocity to x and the y velocity to y to move each frame.

Thats it, so fancy loops. You''ll need to restructure, from what i see.

• 17
• 19
• 23
• 10
• 21