#### Archived

This topic is now archived and is closed to further replies.

# 3D Pong Collision

This topic is 5421 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi guys, I hope this post does not seen too simple, but ive been stuck for weeks and its stopping my development in games. Im trying to make a nice 3D pong game, so far its awesome. Ive got a nice sphere mapped ball, reflective patterns and it all looks so slick. My problem is how to make the ball rebound off the paddle. Ive researched Sphere to Box collsion (get nearest point in box etc..) but i don''t know how to calculate the rebound (new velocity values upon hit)! Also, what if the ball has gone ''inside'' the box upon collision, just setting the inverse of the velocity of the ball, the ball would repeatedly rebound every frame. Im confused, since there are 4 sides on the paddle the ball can bounce off, so far all i have is a IsCollided() function. Thanks anyone who can help. - MeshMan

##### Share on other sites
Simple 2D pong never needed distance between a 2D rectangle and a circle. Your 3D version shouldn't either.

Treat your paddles as rectangles in a plane. When the ball touches (about to go through it) that plane (Ball.Z + Ball.Radius == Paddle.Z), check whether there is a paddle there (2D check), and if there is, then the simplest thing you could do is keep the X and Y velocities the same (X and Y being both perpendicular to the paddle), and invert Z velocity (Ball.Velocity.Z = -Ball.Velocity.Z). That would be basically the ball bouncing off. If there is no paddle, the player's missed, so do nothing (reset ball position, increment appropriate scores, etc.)

I've made a 3D pong in VB for my computers class once. If you're interested in seeing it (with the source), I'll see if I can find it. It's quite messy though, but should give you a basic idea on how to do simple physics.

---
shurcooL

[edited by - shurcooL on December 12, 2003 4:56:19 PM]

##### Share on other sites
Woops, i was a little vague and not clear on exactly what im doing.

My version of pong isnt exactly 2 paddles that just reflect the ball when they hit one axis. I have two paddles situated in a snooker table type arena, like so:

--------------------
| |
| ---- |
| |
| o |
| |
| ---- |
| |
--------------------

The paddles are a little away from the edge of the boards sides so the ball can rebound from the sides of the board but also get behind the paddles and rebound off the paddle that way (kinda like own goals). The score is calculated by hitting the ball on your opponents back-board, first to 10 wins. So the ball needs to check collision from all sides of the paddles (3d paddles and 3D ball).

Is this clear? Or would you like me to upload a screenie.

- MeshMan

##### Share on other sites
Say hello to Mr Clickerson

That explains how to reflect a vector about a normal (of a plane / line). Reflect your velocity vector using that and you''re done.

##### Share on other sites
Man, thats way over my head, i cant understand a word of that math article besides the normal vector to a plane. *shrugs* All i have is 2 bounding rect''s that say *yeah, we collide*, my math sux so that doesnt help, thanks anyways.

##### Share on other sites
//-------------------------------------------------------// dot product // d = A . B//-------------------------------------------------------float Dot(const Vector& A, const Vector& B) {    return A.x*B.x + A.y*B.y + A.z*B.z;}//-------------------------------------------------------// multiplication // B = A * k//-------------------------------------------------------Vector Mul(float k, const Vecor& A) {    return Vector(A.x * k, A.y * k, A.z * k);}//-------------------------------------------------------// substraction // C = A - B//-------------------------------------------------------Vector Sub(const Vector& A, const Vector& B) {    return Vector(A.x-B.x, A.y-B.y, A.z-B.z);}//-------------------------------------------------------// reflect a vector off a plane// V -= 2.0f * (V . N) * N// if normal not normalised, // V -= 2.0f * [(V . N) / (N . N)] * N//-------------------------------------------------------bool Reflect(Vector& V, Vector& N) {    float VdotN = Dot(V, N); // V . N    float NdotN = Dot(N, N);    if (VdotN > 0.0f)  // ball moving away from plane, don;t reflect        return false;    if (NdotN < 0.0001f) // normal too small.         return false;    float k = 2.0f * (VdotN / NdotN); // the reflexion amount    Vector Vn = Mul(k, N); // the reflexion vector    V = Sub(V, Vn); // the new velocity, reflected off the plane    return true;}//-------------------------------------// process collision of a ball and a paddle//-------------------------------------void ProcessCollision(const Vector& PointOnBall, const Vector& PointOnPaddle, Vector& BallVelocity){    Vector N = Sub(PointOnPaddle, PointOnBall);    Reflect(BallVelocity, N);}`

##### Share on other sites
oliii, why are you doing that and not using operators?

##### Share on other sites
I still dont know where to go with that code, since i need a closest point for the sphere and the paddle bounding box but to get the closest point of a bounding rect / sphere i need a relative point from.

For example, to get closest point on paddle box i would need the closest point from the ball, but to get the closest point on the ball, i would need the closest point on box.
And also to add to the ultimate confusion im in, what about if the ball hits a paddle and intersects the paddle and goes inside it, since it will ''never'' collide exactly on the pixels of the sides of the box.

##### Share on other sites
if its basically just moving side to side, why not use 2d collision detection

##### Share on other sites
Its 3d but collision is 2D since nothing moves on Y axis.

1. 1
Rutin
47
2. 2
3. 3
4. 4
5. 5

• 13
• 10
• 12
• 10
• 13
• ### Forum Statistics

• Total Topics
632994
• Total Posts
3009769
• ### Who's Online (See full list)

There are no registered users currently online

×