Search code examples
flashactionscript-3math

Find radians reflection angle



I'm coding a simple Flash game, just to learn flash and improve my maths, but I'm getting very confused with Radians as there are new to me.

What I've done so far is using your mouse you (click & release) you shoot a ball off in that direction using radians. Now what I'd like to happen is when the ball hits the wall it bounces off in it's reflection angle. Eg. if the ball hits the right-hand-side wall travelling at in a radians of -0.65 it should bounce back in the radians of about -2.5

Could some please explain to me how I go about doing this? Thanks


Solution

  • If you work with radians, you should (always) express them in Pi, simply divide by Pi. Make it much easier to work with and understand.

    -0.65 rad = -0.207*Pi rad
    -2.5 rad = -0.796*Pi rad
    

    A full circle is 2*Pi. The normal for a horizontal surface is 0.5*Pi (or -0.5*Pi).

    normal = 0.5*Pi
    angle of incidence = i + Pi - 0.5*Pi //EDIT: +Pi, need to invert the direction
    angle of reflection = 0.5*Pi - angle of incidence
    

    See Reflection on wikipedia for a image and some explanation. That gives this formula for a horizontal wall:

    r = 0.5*Pi - (i + Pi - 0.5*Pi)
    r = 2*Pi - i 
    r = -i //subtract 2*Pi
    

    For example

    i = -0.207*Pi //gives
    r = 0.207*Pi //for a horizontal wall
    

    Similar for vertical wall:

    r = 0 - (i + Pi - 0)
    r = -Pi - i
    

    EDIT I realized this was not correct. For the formula to work, i has to point exactly in the opposite direction. Fixed it, hopefully it is correct now. If I got time later, I'll include an image explaining the mistake and solution.