Log in

View Full Version : OT: would anyone happen to know how to calculate bouncing a ball off an angled box?


narf poit chez BOOM
September 12th, 2003, 08:07 PM
would anyone happen to know how to calculate bouncing a ball off an angled box?

[ September 12, 2003, 19:08: Message edited by: narf poit chez BOOM ]

Erax
September 12th, 2003, 08:21 PM
Possibly, but I need more details.

narf poit chez BOOM
September 12th, 2003, 08:38 PM
ok, right now what i've got is a 3d setup, but i'm only using the x and z planes. i generate a ball at the bottom-left corner of the screen and send it at an angle from 0-90 into a bunch of boxes. the boxes are currenty square and not angled, but what i want is to randomly shoot in balls from all sides of the screen and have them bounce off rectangular boxes.

Erax
September 12th, 2003, 08:49 PM
Easy, the incoming and outgoing angles are always equal. I can make a drawing when I get home, but for now imagine an underlined V. The underline is the box surface and the V is the ball's trajectory. Now the angle from the left side of the underline to the left of the V must be equal to the angle from the right side of the underline to the right side of the V.

This in 2-D, which is what you are using, right ?

Alneyan
September 12th, 2003, 08:58 PM
*Looks at the thread* *Runs away in terror* Please, Narf, you could add a warning in the title for the ones like me who have issues with this kind of... things. http://forum.shrapnelgames.com/images/icons/icon7.gif

narf poit chez BOOM
September 12th, 2003, 09:10 PM
well, the objects are 3d, but it's on a flat plane.

ok, so i figure out what angle the wall is at, then:

wall at 45 degrees, ball comming in at 90, 45-90=-45, 45+-45=0. ball coming in at 270, 45-270=-225, so, if negative 360+-225=135? doesn't work there. but, since it's hitting the bottom...angle at 225, same tilt, 225-270=-45, 225--45=270. doesn't work.

i missed something, right?

what more warning do you want?

Suicide Junkie
September 12th, 2003, 11:27 PM
Neglecting corner hits;

1) You want to know the relative angles between wall and ball. All arithmetic is modulo 360 degrees. (359 + 1 = 0)
2) We can rotate the coordinates by minus the wall angle. This makes the wall look like zero, and the ball will be ball_angle - wall_angle.
You want to reflect in the plane of the wall.
Taking 90 degrees off it, then taking the negative, and adding the 90 degrees back.
85-90 =-5, -5 -> +5, 5+90 = 95 (check)
224 -90 = 134, 134 -> -134 (or 226), 226+90 = 316 (check)
3) Restore the original coordinates by adding back the wall's angle.

So:
NEW_ANGLE = ((((OLD_ANGLE-90-WALL_ANGLE)*-1)+90+WALL_ANGLE) +360) % 360

Where % = modulo operator in whatever programming language you use.
360 is added before the modulo operation because to ensure the resulting value is positive. (Some programming Languages deal with modulo of negative numbers differently)

-----
For Corner Hits:
The effective plane that the ball is bouncing off of is perpendicular to the line connecting the center of the ball to the corner.

Erax
September 12th, 2003, 11:36 PM
OK, I can explain this to you for one simple situation and you'll have to work it out for the rest.

Let's say that the ball travels along the X-axis, therefore its trajectory is at 0 degrees (at 90 degrees it would be going 'up' the Y-axis). And let's say further that the wall it's going to bounce off of intercepts the X-axis at some point.

OK, now we are going to measure the wall's angle of inclination starting at the X-axis and ending at the wall's 'upper' segment. This angle is therefore being measured in a counter-clockwise direction. Let's call its value A.

Since the ball's trajectory is at 0 degrees, the angle between it and the lower segment of the wall is also equal to the wall's inclination, A. Now it's going to rebound at an angle to the wall's upper segment that is equal to this inclination, therefore to the X-axis itself it is an angle of 2A.

Some examples : Wall at 45 degrees - ball rebounds at 90 degrees (goes up).

Wall at 30 degrees - ball rebounds at 60 degrees.

Wall at 90 degrees - ball rebounds at 180 degrees (trajectory doubles back on itself).

Wall at 135 degrees - ball rebounds at 270 degrees.

The trick is, A is always between 0 and 180; outside these bounds the wall has 'flipped over' and you still have A between 0 and 180.

That's the best I can do from here. I'm not too good at explaining things without drawing lots of diagrams.

narf poit chez BOOM
September 13th, 2003, 12:57 AM
thanks to the both of you.

% 360

modulo
<font size="2" face="Verdana, Helvetica, sans-serif">that's for making sure it's still between 0-360, right?

anyone know how to figure out what side it hit when the box is rotated? i've got an idea, but it's clunky, rotating the whole thing, ball and box, back to 0 while keeping there relative positions and then back.

[ September 13, 2003, 00:36: Message edited by: narf poit chez BOOM ]