Calculate reflection vector off surface?

I want to reflect a vector off a wall, like shining a flashlight into a mirror.

I know I can get sufficient data to calculate this from just using a few ray cast distances, but I’m having trouble with the math, and not really sure where to even start in looking it up online.

Anyone know either a better way to figure out the reflection angle or a good place to look up that math?

Let d = incoming vector, n = wall normal, and r = reflected vector, then:

r = d – 2(d . n) n

where . is the dot product.

https://math.stackexchange.com/questions/13261/how-to-get-a-reflection-vector

3 Likes

Thanks! This worked exactly as I wanted.

rule("calculate reflection angle for current_position and current_velocity")
{
	event
	{
		Subroutine;
		reflection_angle;
	}

	actions
	{
		"surface normal of surface you're gonna hit"
		Event Player.current_target_surface_normal = Ray Cast Hit Normal(Event Player.current_position,
			Event Player.current_ray_cast_hit_position, Null, Null, True);
		"Let d = incoming vector, n = wall normal, and r = reflected vector, then: r = d – 2(d . n)  where . is the dot product."
		Event Player.current_reflection_angle = Event Player.current_velocity - 2 * Dot Product(Event Player.current_velocity,
			Event Player.current_target_surface_normal) * Event Player.current_target_surface_normal;
	}
}