Advertisement

Collision detection on a RECT

Started by October 25, 2000 12:04 PM
6 comments, last by DJ_House 24 years, 2 months ago
Let''s say I have a RECT in the middle of the screen. It is a RECT (rect) structure using: SetRect(▭, 15, 15, 30, 30); If I have an object coming at it, how can I determine which side that object strikes my RECT on? It''s not as easy as: if(object hits rect.right) because if the object moves at 7 pixels per frame or whatever, it can actually be inside of the object. Any suggestions on how to determine which side is hit?
I see you like Win32 functions - so we''ll stick with that

How about using PtInRect() for point-rect collision, or IntersectRect() for rect-rect collision.

- After you know collision is taking place, you can calculate (for every edge) the distance to the object. Shortest distance = closest of course.
- Or you can use the direction of the object and after collision move back to find the intersected edge. A line-line intersection algo might be usefull here.

Advertisement
check this out:
http://workspot.net/~kondor/tutors/dos/colldet.html
Me again...what I use is four points on the top, left, right, and bottom of the incoming object. Depending on the direction of the ball and the point that hits...you determine the direction.

..........1
.......__|__
...../..........\
2_|.............|_3
...|.............|
.....\______/
...........|
...........4

Example: The ball is going up, and point 1 hits the
rectangle, the ball *SHOULD* go down, right.

Not to hard, is it?

Basically, you make sure the collision isn't impossible (e.g. the ball is going left and the left side hits something) then you just reverse the direction depending on which points hit the rectangle. If 1 or 4 hit, you reverse the y direction, if 2 or 3 hit, you reverse the x direction.

Hope that helps.

Any questions...go ahead and post again...or you could e-mail me.

MSkinn99

Whats done is done (Until you hit Undo)

Edited by - MSkinn99 on October 25, 2000 10:27:03 PM
-MSkinn99What's done is done (Until you hit Undo) :)Two wrongs don't make a right; but 3 lefts do.You'd be paranoid too if everyone was out to get you.
Yet another game programming web page...
Thanks for the help guys. The only problem I had, was the end position of my ball, was sometimes inside of the brick. Here''s the final solution I came up with...

I calculated the line intersection of my ball and all the lines on the RECT. I could then easily find out which area of the RECT was hit. Because even if the ball is inside my RECT, one part of the line from the original position and new position *HAD* to cross the RECT.

Either way, it works perfect.

Thanks for the suggestions guys.
Actually I scrapped my previous idea. It sucked. I''m boosting the frames per second up to 60, and having the ball increment in smaller amounts. I know this isn''t the way to do it, but I can''t find any full code implementing a time loop. And for whatever reason, I just can''t figure it out. I''m going to try your suggestions MSKinn
Advertisement
I am honored...I think. This is the first (I think) time anyone has actually listened to me and used my ideas. Thank you DJ!


Ok, I''m done now.

-MSkinn99

What's done is done (Until you hit Undo) :)
-MSkinn99What's done is done (Until you hit Undo) :)Two wrongs don't make a right; but 3 lefts do.You'd be paranoid too if everyone was out to get you.
Yet another game programming web page...
I have a small tutorial on my website. It''s "Physics I - Gravity". If you drop the gravity acceleration and go from 3D to 2D, you''re almost there.

This topic is closed to new replies.

Advertisement