PDA

Click to See Complete Forum and Search --> : [Resolved] Bounce Angle

Ed Mack
03-13-2002, 02:34 PM
Heres a nice little tidbit of info:

To work out the angle that an object should bounce off at try this:

R = 2W - P

P = balls path
W = walls angle
R = reflection

All angles must be in range of 0-360

DonDanny
03-14-2002, 05:19 AM
And if you wanna know the angle of a moving object. You could figure out it's slope by taking it's last two X and Y coordinates, then devide them

slope = (y2-y1)/(x2-x1)

and after that throw the Arcus Tangens at them. You'll get the Angle returned.
Now everyone, go out and do bouncing balls from not straight walls;)

Ed Mack
03-14-2002, 03:46 PM
Yes, make us pinball games, and make us proud!

bit-101
03-19-2002, 05:45 PM
Originally posted by Ed_Mack
Yes, make us pinball games, and make us proud!

heck, been making them for a while now.
http://www.bit-101.com/pinball/pinball_05.swf
http://www.bit-101.com/pinball/pinball_06.swf

lesli_felix
03-19-2002, 07:53 PM
Sweet!

Yes, I've been inspired. Watch this space.....

bit-101
03-19-2002, 11:13 PM
ok, i don't want to show off...ok i do...but anyway, here is the best stuff i've done on bouncing off of angles.

http://www.bit-101.com/content/020227.swf
you can hold down control and use the mouse to draw a line. then drop the ball onto the line.

Ed Mack
03-20-2002, 10:45 AM
No fair, there's a line limit :( Cool though, I keep telling myself someday I'll make some sort of bouncing thing, I started this morning, and then came here and forgot about it :D

bit-101
03-20-2002, 10:57 AM
well, it's gotta do a mathematical hit test on each line segment, so i had to limit it, or it starts after much more than 100 lines.
all flash 5 by the way, maybe i'll update it with mx eventually.

Ed Mack
03-20-2002, 12:17 PM
I wanted to scribble.. perhaps put a text input so that people on faster systems can have more, they always do :D

mentuat
03-20-2002, 01:24 PM
hello!!!

my first post of flashkit wahay! (although i think i had before but forgotten my password can't remember)

slight annoyance i can't use 'men****' as my normal user name though.

anyway to the point: i has to solve this problem a while back too - for making a vaguely realistic bounce off the front and back of a basketball hoop with the main problem being the ball passing through the point of contact at speeds so it kinda has to reverse back til it is just touching to get the right angle. does that make any sense?

it's at **url not here anymore** and yup, it needs optimizing.

bit-101, that bouncing off angles thing is great, finally i can create my own bagatelle board...

bit-101
03-20-2002, 01:55 PM
lets see if i can quickly explain my method. it uses "coordinate rotation".
you have a movie clip, which just a horizontal line, called, "wall". you can then rotate this movieclip to any angle.
you have a ball, called, "ball".
you have vx and vy, which are the velocity of the ball on x and y.

// get the position of ball in relation to wall
x=ball._x-wall._x;
y=ball._y-wall._y;

// get angle in radians of wall
angle=wall._rotation*Math.PI/180;

// rotate the scene to make it as if wall was lying flat
// -angle is the amount we need to rotate it.
// first rotate ball's position:
x1=Math.cos(angle)*x+Math.sin(angle)*y;
y1=Math.cos(angle)*y-Math.sin(angle)*x;

// then the velocities
vx1=Math.cos(angle)*vx+Math.sin(angle)*vy;
vy1=Math.cos(angle)*vy-Math.sin(angle)*vx;

// now check if ball is below wall
if(y1>0-ball._height/2){
// set it at the edge of the wall
y1=0-ball._height/2);

// reverse the y speed, and lose a little momentum
vy1*=-.9;

// convert new positions and speeds back to world coords
x=Math.cos(angle)*x1-Math.sin(angle)*y1;
y=Math.cos(angle)*y1+Math.sin(angle)*x1;
vx=Math.cos(angle)*vx1-Math.sin(angle)*vy1;
vy=Math.cos(angle)*vy1+Math.sin(angle)*vx1;
ball._x=wall._x+x;
ball._y=wall._y+y;
}

that about does it. not responsible for any typos. :D

bit-101
03-20-2002, 02:04 PM
i should add that the code above could be massively optimized. i wrote it above trying to be as clear and simple as possible.

i would normally calculate the sin and cos values into a variable and use that throughout, rather than recalculating each time. and would only calculate what was absolutely necessary PRIOR to the "if" statement, and calculate the rest inside the if-block. a few other tricks too.

ironmallet
03-25-2002, 01:17 PM
hey bit-101, I like the way you handle the rotational transformations. I was suffering under the delusion that the lines were scaled diagonal lines rather than rotated flat lines. Keep up the great work. Off I go to spend my morning working out the details of stuff that's old hat to you...

bit-101
03-25-2002, 01:31 PM
well, the file where you can draw the lines and then have the ball bounce, does use the old 45 degree 100x100 line scaled. then it calculates the angle using Math.atan2(_yscale, _xscale)

but if you are just making a few lines and rotating them, it's easier to use a horizontal line and use the _rotation converted to rads.

ironmallet
03-25-2002, 03:09 PM
yeah, that makes sense.

btw I just got a swf to work that I can drag walls around and rotate them and have the ball bouncing and any way thanks for the help with it. The coordinate transformations were the real key. It's fun to look back on your early march stuff with a better idea of what was going on.

one thing that the code above doesn't address, that I kludged into working, how does the ball know where the wall ends?

bit-101
03-25-2002, 03:24 PM
i merely checked to see if the ball was betwee the left edge and right edge of the line.

bounds=line.getBounds(_root);
if(ball._x>bounds.xMin && ball._x<bounds.xMax){
// then do the rest of the stuff here
}

i also usually do one more thing. as written above, if the ball goes under the line from the side, it will trigger a hit and jump above it. so i check to see if it's just a "little bit" below the line. i use vy1. the adjusted y speed. if it is further below the line than that speed, than it could not have come "through" the line, because it couldn't have come that far in one frame. it must have come in from the side, so we don't want to touch it.

change to this:
// now check if ball is below wall
if(y1>0-ball._height/2 && y1<vy1-ball._height/2){

Felipegm
06-30-2006, 02:13 PM
Hello bit101...

I´m searching for a good tutorial on how to make a pinball game in flash.
I´l already have the ball phisics working well, but I´m getting the flippers to kick the ball and to make the ball to bounce on angles.
Could you please show a link to a tutorial or send me a fla with some examples?