Calculating the First Collision Point of Two Moving Rectangles in C Language

  • Context: Undergrad 
  • Thread starter Thread starter r4nd0m
  • Start date Start date
  • Tags Tags
    Collision
Click For Summary
SUMMARY

The discussion focuses on calculating the first collision point of two moving rectangles (ABCD and EFGH) in the C programming language. The solution involves treating one rectangle as motionless and adjusting the velocity vectors accordingly. The rectangles will intersect if the vertical distance between their centers is less than or equal to half the sum of their heights, and the horizontal distance is less than or equal to half the sum of their widths. A program can be developed to check these conditions over time to determine the exact collision point.

PREREQUISITES
  • Understanding of C programming language
  • Knowledge of vector mathematics
  • Familiarity with collision detection algorithms
  • Basic concepts of geometry related to rectangles
NEXT STEPS
  • Implement a C program to calculate the intersection of two rectangles based on their velocity vectors
  • Research mathematical formulations for relative motion in 2D space
  • Explore collision detection techniques in game development
  • Learn about the use of absolute value functions in programming for distance calculations
USEFUL FOR

Students in computer science, game developers, and anyone interested in physics simulations or collision detection algorithms in programming.

r4nd0m
Messages
96
Reaction score
1
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 parallel 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
 
Mathematics news on Phys.org
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 exactly 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.
 

Similar threads

  • · Replies 32 ·
2
Replies
32
Views
3K
  • · Replies 2 ·
Replies
2
Views
3K
Replies
14
Views
3K
Replies
2
Views
3K
Replies
34
Views
3K
  • · Replies 3 ·
Replies
3
Views
16K
  • · Replies 3 ·
Replies
3
Views
5K
  • · Replies 8 ·
Replies
8
Views
3K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 6 ·
Replies
6
Views
3K