A Flash Developer Resource Site

Results 1 to 17 of 17

Thread: [Resolved] Bounce Angle

  1. #1
    Gross Pecululatarian Ed Mack's Avatar
    Join Date
    Dec 2001
    Location
    UK
    Posts
    2,954
    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

  2. #2
    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

  3. #3
    Gross Pecululatarian Ed Mack's Avatar
    Join Date
    Dec 2001
    Location
    UK
    Posts
    2,954
    Yes, make us pinball games, and make us proud!

  4. #4
    Senior Member
    Join Date
    Jan 2002
    Posts
    368
    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

  5. #5
    ....he's amazing!!! lesli_felix's Avatar
    Join Date
    Nov 2000
    Location
    London UK
    Posts
    1,506
    Sweet!

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

  6. #6
    Senior Member
    Join Date
    Jan 2002
    Posts
    368
    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.

  7. #7
    Gross Pecululatarian Ed Mack's Avatar
    Join Date
    Dec 2001
    Location
    UK
    Posts
    2,954
    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

  8. #8
    Senior Member
    Join Date
    Jan 2002
    Posts
    368
    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.

  9. #9
    Gross Pecululatarian Ed Mack's Avatar
    Join Date
    Dec 2001
    Location
    UK
    Posts
    2,954
    I wanted to scribble.. perhaps put a text input so that people on faster systems can have more, they always do

  10. #10
    ********* mentuat's Avatar
    Join Date
    Mar 2002
    Location
    out of office
    Posts
    717
    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...
    Last edited by mentuat; 02-11-2003 at 08:17 AM.

  11. #11
    Senior Member
    Join Date
    Jan 2002
    Posts
    368
    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.

    Code:
    // 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.

  12. #12
    Senior Member
    Join Date
    Jan 2002
    Posts
    368
    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.

  13. #13
    Senior Member ironmallet's Avatar
    Join Date
    Feb 2001
    Posts
    252

    ah!

    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...

  14. #14
    Senior Member
    Join Date
    Jan 2002
    Posts
    368
    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.

  15. #15
    Senior Member ironmallet's Avatar
    Join Date
    Feb 2001
    Posts
    252
    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?


  16. #16
    Senior Member
    Join Date
    Jan 2002
    Posts
    368
    i merely checked to see if the ball was betwee the left edge and right edge of the line.

    Code:
    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){

  17. #17
    Junior Member
    Join Date
    Jun 2006
    Posts
    1
    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?
    Thanks in advance

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center