

Here's the formula XNA uses: public static Vector3 Reflect(Vector3 vector, Vector3 normal) It's really just a small formula that takes the wall's normal and the incidence vector, and returns the reflected vector for you.
#Finding angle of reflection how to#
In case you ever need to handle this for arbitrary walls, you'll need to look into how to reflect a vector. Medium where light coming form must have higher index of refraction compared to second medium ( n > n t) For any angle of incidence less than the. For total internal reflection, Incident angle > Critical angle. Here's a picture that should make it clear: The critical angle can be calculated from Snells law by setting the refraction angle equal to 90°. This angle is measured in relation to the wall's normal. Example: if( /* hit vertical wall */ )īut if you really care about knowing the angle, then in general terms, the angle of reflection is the same as the angle of incidence. Simply negate the X-component of the object's velocity when hitting a vertical wall, or the Y-component of the object's velocity when hitting an horizontal wall. But if it's a 2D game and your walls are guaranteed to be horizontal or vertical and you just want to bounce the object off them, there's a much easier way than having to deal with reflections. This is certainly not the best way to go and I'm sure you can just calculate it in some way but I think it's a possible solution to your problem.You didn't specify if it was a 2D or 3D game.

You could just create a vector for these calculations or keep the same general idea but working with angles. Note: I noticed that you're using an angle to represent the direction instead of a vector. After, you rotate back to your real situation. You rotate by 70 degrees, check where the collision was from, and reverse the appropriate components. These bounces will continue until the sound has. A sound wave hitting a flat wall at 45 will reflect off it at 45. On the second case, it's the same thing, except that the ball's velocity is not perpendicular to the block. What is reflection Sound waves reflect off of harder surfaces the same way billiard balls bounce off the bumpers of a pool table the angle of incidence equals the angle of reflection. After that, you just have to rotate back to the real situation (which is not on the drawing).

This will also make the ball act correctly when it hits corners (it will reverse both components). If it was vertical (like in this case), the y component ( = ). If the collision was horizontal, reverse the x component of the velocity. Then, you check where the collision was from. What you need to do is rotate everything by 30 degrees (the inclination of the block) (you can use matrices for that). One of the way to fix the collision problem is to bring everything back on the horizontal axis. On the first case, the blue block has an inclination of 30 degrees. The green arrows represent the ball's velocity (as a vector), the blue blocks represent your large black blocks and the red arrow is the resultant vector. Here's a quick drawing on my white board that shows a possible solution for that: I'm not sure if I understood your problem correctly, but I assumed that what you want to do is find the resultant direction of you ball after a collision. I'll open another question for that and link it here. Ok, the problem with angles was solved with Vectors, but I still have the problem with sides and back collision.
#Finding angle of reflection code#
Ps: If needed, I can supply code and a playable swf. More than that, the ball should hit the sides too, so, how can I measure the direction of the hit and the new direction of movement of the ball? My problem comes now, with the pads (the 3 dark and large blocks around the small colored blocks).Īs they turn around, the surface angle changes. The ball is moved by: // Event.ENTER_FRAME Where the blue line is the ball direction, and the black line (base line) is the block surface.ĭirection is a angle expressed in radian (that can be convert to degrees, of course). I've managed to get the collision per pixel and my ball " reflects" on contact with the blocks with the following math: Ball.Direction = 2 * (Block.Direction - Ball.Direction) + 180 I have a cannon at the center, blocks around the cannon and pads around the blocks.
