Reflection, within the context of this article, is flipping something's velocity/direction over the normal direction of a wall. This kind of calculation has a number of uses, including simulating a particle bouncing off an obstruction, and bouncing light rays off mirrors in raytracing.
Formula

This process takes in two inputs, the slope of the wall (m) and the velocity of the particle (xv and yv). It first calculates the unit normal vector of the wall (see picture) as normal x and normal y. It is important that the normal is a unit vector because otherwise the input velocity and output velocity will not have the same speed.
set [normal x v] to ((m) / ([sqrt v] of ((1) + ((m) * (m))))) set [normal y v] to ((-1) / ([sqrt v] of ((1) + ((m) * (m)))))
But this script will not work when the slope of a wall is zero (which is quite often). To fix this, one will have to catch it:
if <(m) = [0]> then set [normal x v] to [0] set [normal y v] to [1] else set [normal x v] to ((m) / ([sqrt v] of ((1) + ((m) * (m))))) set [normal y v] to ((-1) / ([sqrt v] of ((1) + ((m) * (m))))) end
After calculating this, you must take the dot product of the normal vector and the incoming vector and double it.
set [dot product v] to (((normal x) * (xv))+((normal y) * (yv))) set [dot product v] to ((2) * (dot product))
Then one must scale the normal vector by this double-dot product and subtract this from the original vector. The resulting vector is the new velocity vector.
set [ax v] to ((dot product) * (normal x)) set [ay v] to ((dot product) * (normal y)) change [xv v] by ((0) - (ax)) change [yv v] by ((0) - (ay))
All together, with a few code tweaks:
if <(m) = [0]> then set [normal x v] to [0] set [normal y v] to [1] else set [normal x v] to ((m) / ([sqrt v] of ((1) + ((m) * (m))))) set [normal y v] to ((-1) / ([sqrt v] of ((1) + ((m) * (m))))) end set [dot product v] to (((normal x) * (xv))+((normal y) * (yv))) set [dot product v] to ((-2) * (dot product)) change [xv v] by ((dot product) * (normal x)) change [yv v] by ((dot product) * (normal y))
See Also
- Using Vectors
- Finding Direction with Velocity
- List of Mathematical Functions Done in Scratch — elaborates on finding the slope of a line
- Physics (disambiguation)
- Physics Engine