1
$\begingroup$

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

  • 1
    Could we see all of the relevant code? That is the part covering how you compute trajectories?2011-04-29
  • 0
    Here is the code I've got so far with some explanation to what I'm doing and why. http://pastebin.com/rrtC30ri2011-04-29
  • 0
    OK, have you already tried my suggestion? I.e. for the top and bottom wall, just reverse angle.2011-04-29

2 Answers 2

3

The relevant quantity here is the velocity vector of the ball. When you hit a wall, you want to flip the component of that vector perpendicular to the wall and keep the other component equal.

So, if you fix a velocity vector by giving an initial speed $v_0$ and a direction given by an angle $\alpha$ with respect to the horizontal direction and assuming there are only horizontal end vertical walls, this is what will happen:

  1. The moment you hit a horizontal wall, you want to flip the vertical component of the vector and keep the horizontal component equal. This means $$ v_0(\cos(\alpha),\sin(\alpha)) \mapsto v_0 (\cos(\alpha),-\sin(\alpha)) = v_0 (\cos(-\alpha)),\sin(-\alpha))$$ which corresponds to reversing the angle $\alpha$.
  2. The moment you hit a vertical wall, you want to flip the horizontal component of the vector and keep the vertical component equal. This means $$ v_0(\cos(\alpha),\sin(\alpha)) \mapsto v_0(-\cos(\alpha),\sin(\alpha)) = v_0(\cos(\pi-\alpha)),\sin(\pi-\alpha))$$ which corresponds to adding $\pi$ to the reversed angle $-\alpha$.
  • 0
    Thanks for your answer but it's a bit over my head this math, how do I convert it into AcriptScript? What is α2011-04-29
  • 1
    Hello Owen, just replace the angle with -angle when you bounce on either the top or bottom wall. For the left and right wall, replace the angle with pi-angle as user9325 suggested. So item.radians = - item.radians for bottom and top, item.radians = Math.PI - item.radians for left and right.2011-04-29
  • 0
    Ok, I get it now. Thanks a lot Raskolnikov and everyone else who helpped.2011-04-29
0

The reflection changes the sign of the angle giving 0.65, but since you also go in the opposite direction you change this angle by $\pi \sim 3.14$. So you get $0.65 - 3.14 \sim -2.5$.

Note that it does not make a difference if you add or substract $\pi$ as angles are only determined up to multiples of $2\pi$.

  • 0
    That works fine for the left and right walls but the top and bottom wall don't. `item.radians = Math.PI + item.radians;` This send the ball back in the same direction. `item.radians = Math.PI + item.radians;` And this just goes crazy flying off the screen.2011-04-29
  • 0
    Better yet @Owen, post what code you have and we might be able to point out any slip-ups you may have had.2011-04-29
  • 0
    @J.M. Here is the code I've got so far with some explanation to what I'm doing and why.2011-04-29
  • 0
    http://pastebin.com/rrtC30ri2011-04-29