Take the 2-minute tour ×
Mathematics Stack Exchange is a question and answer site for people studying math at any level and professionals in related fields. It's 100% free, no registration required.

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

share|improve this question
1  
Could we see all of the relevant code? That is the part covering how you compute trajectories? –  Raskolnikov Apr 29 '11 at 11:10
    
Here is the code I've got so far with some explanation to what I'm doing and why. pastebin.com/rrtC30ri –  Owen Apr 29 '11 at 12:00
    
OK, have you already tried my suggestion? I.e. for the top and bottom wall, just reverse angle. –  Raskolnikov Apr 29 '11 at 12:25
add comment

2 Answers 2

up vote 3 down vote accepted

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$.
share|improve this answer
    
Thanks for your answer but it's a bit over my head this math, how do I convert it into AcriptScript? What is α –  Owen Apr 29 '11 at 13:20
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. –  Raskolnikov Apr 29 '11 at 14:08
    
Ok, I get it now. Thanks a lot Raskolnikov and everyone else who helpped. –  Owen Apr 29 '11 at 14:37
add comment

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

share|improve this answer
    
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. –  Owen Apr 29 '11 at 10:40
    
Better yet @Owen, post what code you have and we might be able to point out any slip-ups you may have had. –  J. M. Apr 29 '11 at 11:17
    
@J.M. Here is the code I've got so far with some explanation to what I'm doing and why. –  Owen Apr 29 '11 at 11:41
    
pastebin.com/rrtC30ri –  Owen Apr 29 '11 at 11:51
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.