So I have an object moving in a direction towards a fixed horizontal or vertical wall. How do I compute the angle that the object should bounce off at? The object can approach the wall at an arbitrary angle.
|
If you know the wall's normal vector and have an incoming direction for the object, then what you want is the reflection of a vector across a plane. If n is a normalized vector, and v is the incoming direction, then what you want is −(2(n · v) n − v). The minus sign accounts for the fact that the reflection formula doesn't actually reverse the direction, as an object's velocity would reverse. This answer is given in terms of vector math, not an angle, because that's usually preferable if you don't have an explicit reason to use an angle. Note that if you must talk about angles, the angle of reflection is equal to the angle of incidence. If the angle is measured from the normal, the outgoing angle is the negation of the incoming angle; if the angle is measured from the wall, then it's the complement of the incoming angle. |
|||
|
|
|
You didn't specify if it was a 2D or 3D game. 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. 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. Example:
But if you really care about knowing the angle, then in general terms, the angle of reflection is the same as the angle of incidence. This angle is measured in relation to the wall's normal. Here's a picture that should make it clear:
In case you ever need to handle this for arbitrary walls, you'll need to look into how to reflect a vector. It's really just a small formula that takes the wall's normal and the incidence vector, and returns the reflected vector for you. Here's the formula XNA uses:
And for 2D you could just do:
|
|||||
|
|
This is just about reflecting a vector along the walls normal vector. |
|||
|
|

