Line Vs Rectangle Collision Detection


A_SN

Active Member
Joined
Jun 8, 2006
Messages
899
Here is my problem, I have a 3D pong game that we will simplify as this : each pad is represented by a rectangle. This rectangle is parallel to every axis, its Z axis position is constant, and it's otherwise defined by four values that we could call x1, y1, x2, y2 that make up the rectangle.

The ball is defined as a singular point, evolving in all 3 axis. What I want to find out is when the line defined by the position of the ball at time t and t+1 crosses the rectangle, and where on the rectangle.

I tried googling some for collision detection for quite a while but most of the time I fall on obscure stuff that is obviously not about my particular problem.
 
A_SN said:
Here is my problem, I have a 3D pong game that we will simplify as this : each pad is represented by a rectangle. This rectangle is parallel to every axis, its Z axis position is constant, and it's otherwise defined by four values that we could call x1, y1, x2, y2 that make up the rectangle.

The ball is defined as a singular point, evolving in all 3 axis. What I want to find out is when the line defined by the position of the ball at time t and t+1 crosses the rectangle, and where on the rectangle.

I tried googling some for collision detection for quite a while but most of the time I fall on obscure stuff that is obviously not about my particular problem.
Did a google search for intersection between a line (your ball) and a plane (your paddle) and found this:

http://www.geocities.com/SiliconValley/2151/math3d.html

about 2/3rds down.

If i understand your game, you can check to see if the end points of the line exceed the plane that the paddle is fixed to, and if so, test to see where on this plane the line intersects, and check this against where the paddle is.

Edit: Is the game this sort of idea?

http://www.newsandentertainment.com/zFcurveball.html
 
Last edited by a moderator:
I think this is a way to do it without having to wait until t+1 to detect a collision at t:

Find the shortest vector from the center of the sphere to the plane that the rectangle is on.

If the vector is greater than the radius of the ball: No Collision

If the vector is less than the radius of the ball and points to a point on the rectangle: Collision, end of the vector is your point of collision.

If the vector is less than the radius of the ball, and the hypotenuse of a triangle formed by the original vector, a vector from the point where the vector hits the plane and the closet rectangle edge is less than the radius of the ball: Collision, end of hypotenuse is your point of collision.

exampleI tried to draw a picture of the last one.

There's also a chance that the point is on the plane and exactly hits the paddle, simply find the distance from the point to the closest side of the rectangle and see if its less than the radius.
 
You can extrude the cuboid (?) by the negative velocity of the ball and check if the position of the ball lays inside it.

Hmm, I actually a had a line vs. cube algorithm that was really sweet and awesome. Maybe I should look it up?
 
Dr_Ian said:
You can extrude the cuboid (?) by the negative velocity of the ball and check if the position of the ball lays inside it.

Hmm, I actually a had a line vs. cube algorithm that was really sweet and awesome. Maybe I should look it up?
I don't get the first line. By the way, did I mention that the ball could possibly go through the cube without at any frame being inside it?

And yeah, you should look it up.

Anyways, I think I've had an idea, since the rectangle always have the same Z coordinates, I could check for when the ball moves to the other side of the the infinite Z-axis plane the rectangle is part of, and calculate where the ball was when it was at that precise Z position.
 
Last edited by a moderator:
if extrude the stationary (convex) shape by the reverse velocity of the other point object, then you know there is a collision if the point object is inside the resulting object.

CODE
O
/
+---+ /
| | /
| |
+---+

to
___.
/ |
/ | O
./ |
+ /
| /
| /
+---+


This is usually not very easy to do, though.

There is a really good way, I'll have a look...
...
(10 mins pass)
...
oh, here is it!
 
Back
Top