Matt
Matt

Reputation: 11357

Box collision detection and bouncing

I'm making pong, and am finding it really difficult to write an algorithm that bounces the ball off the four walls properly (I will deal with scoring later on, because only part of the West+East sides will be goals). So at the moment I want the ball to bounce around the box.

Detecting whether the ball has hit a wall is easy, but I'm having trouble calculating the new angle.

This is what I've come up with so far:

        if(dstY == 0) {
            // North wall
            if(angle < 90) {
                newAngle = angle + 90;
            } else {
                newAngle = angle - 90;
            }
        } else if(dstX == maxWidth) {
            // East wall
            if(angle < 90) {
                newAngle = angle + 270;
            } else {
                newAngle = angle + 90;
            }
        } else if(dstY == maxHeight) {
            // South wall
            newAngle = angle + 90;
        } else if(dstX == 1) {
            // West wall
            if(angle < 270) {
                newAngle = angle - 90;
            } else {
                newAngle = angle - 270;
            }
        }

That only works for about half of the collisions, and looks really ugly. I'm sure this should be really simple and that it's been done many times before.

In my code, dstX/dstY are the X/Y destination coordinates. X=0 and y=0 at the top left.

Upvotes: 0

Views: 2115

Answers (4)

jt78
jt78

Reputation: 926

Thanks @jbx, I knew there was a simpler way :) However, that doesn't seem to work for the east and west walls and the paddles (if they're on those walls). This seems to work for me on the east and west walls though:

(180 - (angle + 90)) - 90.

which simplifies to just (180-angle). Hope that helps.

Upvotes: -1

heckruler
heckruler

Reputation: 1

A word of wisdom about using jbx's gradient method. If the ball hits near the corners of the box, while dx will be inverted, dy can place the ball above the top boundary.

Upvotes: 0

jbx
jbx

Reputation: 22178

You can look at this in 2 ways:

Angles: If you know the angle the ball is colliding at, just perform 180 - angle to find the new angle.

Gradient: Probably simpler. You must be moving the ball at a certain dY and dX every t milliseconds. so if you hit the wall you can simply play with inverting signs of dY and dX. For example if you hit the right wall, dX becomes -dX while dY continues on its course.

Upvotes: 1

trashgod
trashgod

Reputation: 205885

In this KineticModel, the method collideWalls() uses two-dimensional vector arithmetic to simplify the simulation of an elastic collision between a particle and a flat surface.

Upvotes: 1

Related Questions