Line plane intersection

Started by
6 comments, last by jonton 8 months ago

Hello,

I'm basically a good high school math guy with a few university courses so this kind of thing is over my head, especially right now (I've basically given up making games, my brain isn't functioning I've been though so much, not the least of which was being tasered for some reason…)

I have a 3D Pong game I made years ago. It always seemed to work but now when I run it it seems the computers are too fast or something and the ball gets lost. What I think happens is the Z value gets reset every point but X and Y sometimes wind up out of bounds, and I don't know where on X and Y to place the ball to make it the same as where it crossed the goal. What happens is two computer players can get the ball going so fast that it exits the playing area after the velocity is added to X and Y.

Anyway this game is one that I have had on the shelf for a while because of this problem. If I could fix it I could put it back online. The code for the game is on GitHub at https://github.com/TheOestG/pung2.

Thanks.​

None

Advertisement

Normally I'd Google this stuff and get it working with some effort… anyway, that's what forums are for.

None

And ya know what, I'll have to do the same thing with wall collisions, it probably doesn't happens often though. When the computers play they get it going pretty much instantly off off each others paddles and slowly move around until one of them misses. It's really pretty trippy that's why I want to get it working.

None

Hi @theoestg ,

This is an ideal problem to start with when looking at geometric tests.

The only functions you'll need is a dot product of two vectors and a way of returning the sign of a scalar value:

float Dot(Vector3 a, Vector3 b)

{

return a.x b.x + a.y b.y + a.z * b.z;
}

bool Sign(float a)

{

return (a >= 0.0f);

}

Firstly, if you use the implicit representation of the plane:

ax + by + cz + d

Where [a, b, c] is the normal of the plane (call this vector n) and d is the scalar distance from the origin to the closest point on the plane.

And define a line segment as being between points p0 and p1.

Calculate the signed distance from the plane to the two points using the dot product:

d0 = Dot(n, p0) - d;

d1 = Dot(n, p1) - d;

If the sign of these two distances are the same then the line segment doesn't intersect the plane:

if (Sign(d0) == Sign(d1)) return false;

If the signs are different then the line intersects. Then if you want to know where along the line segment the intersection was, calculate:

t = d0 / (d0 - d1);

This will give you a value between 0 and 1 that you can use to interpolate between p0 and p1:

p = (p1 - p0) * t + p0;

Hope this helps.

Thanks so much jonton, that's exactly what I need. I'll try it with the goals first then do whatever I need for wall collisions as well (which I haven't looked at again yet.) Thanks.

None

I definitely learned something. It's really easy to do. But this game is a goner. It was built with dated libraries that don't really work properly and never really did. I get a lot of graphical glitching and things that were never fixed in this abandoned library, so I deleted the game for good. It would be easier to start from scratch and do it properly. But I'm glad I learned this. Thanks.

None

No worries. Good luck with the next iteration.

This topic is closed to new replies.

Advertisement