Collision of rectangles

  • Thread starter r4nd0m
  • Start date
  • #1
96
1

Main Question or Discussion Point

I wonder if somebody could help me with this problem I'm solving for my c language class (but it's more a mathematical problem I think).
So we have 2 different rectangles (ABCD and EFGH) in an ortogonal plane, their sides are paralel to the axes of the ortogonal system. Each of this rectangles is given a velocity vector. Now the task is to determine where the first collision will take place (where these two rectangles (their sides or corners) will meet the first time) if it will take place.
Any suggestions?
Can this problem be solved via mathematicl formulas?
thanks a lot
 

Answers and Replies

  • #2
AKG
Science Advisor
Homework Helper
2,565
4
A couple ideas here to help you, not much because I'm at work right now. For one, motion is relative, so if you have a velocity vectors v1 and v2, then just treat the second rectangle as motionless and treat the first one as though it were moving at velocity v1 - v2. The two rectangles will intersect if and only if:

a) the vertical distance between the centers of the rectangles is less than or equal to V = 0.5(the vertical height of rectangle 1 + the vertical height of rectangle 2)

AND

b) the horizontal distance between the centers of the rectangles is less than or equal to H = 0.5(the horizontal width of recantle 1 + the horizontal width of rectangle 2)

It should be quite easy to make a program that checks if these conditions are ever met. The plot of horizontal distance between centers over time will look like an absolute value graph (V shape) and same for vertical distance. You will be able to solve for 0, 1, or 2 points in time where the horizontal distance between centers is exactly equal to H. If there are 0 point in time, then no intersection. If there is one, and it occurs at time t, then plug in t in your equation to find the vertical distance, and see if the vertical distance between centers is less than equal to V. If so, then they meet at t, otherwise they don't. If you find two points of time, t and t' (assume t < t'), then do a similar calculation for vertical distance so you find either 0, 1, or 2 points in time when the vertical distance is exaclty equal to V. If 0, no intersection. If 1, and it occurs at time t'', check if t < t'' < t'. If so, then they intersect only once at t'', otherwise never. Finally, say you find two times, t'' and t'''. You want to find the smallest t value that is in both intervals [t,t'] and [t'',t''']. You can figure out how to do that.
 

Related Threads on Collision of rectangles

Replies
3
Views
3K
Replies
5
Views
6K
  • Last Post
Replies
2
Views
3K
  • Last Post
Replies
1
Views
7K
  • Last Post
Replies
4
Views
13K
Replies
14
Views
7K
  • Last Post
Replies
5
Views
11K
Replies
3
Views
2K
  • Last Post
Replies
4
Views
920
  • Last Post
Replies
1
Views
1K
Top