How does the math behind this raycast work?

Hi, I’m trying to learn how raycast guns work but I keep finding this one little snippet of code in each raycast creation in the tutorial videos I watch, and I don’t really understand why the math behind it works. I was wondering if anyone could explain it to me as I really wanna learn how this works.

local bulletray = Ray.New(gun.Position, "(gun.Position - mouse.Hit.p).Unit * 300")

I understand that this is setting the direction of the ray (the part in parenthesis) but I simply don’t understand how subtracting the two vectors then taking the unit form of it and multiplying it by 300 would help with that. Thanks.

VectorA - VectorB = Vector with direction from VectorB to VectorA OR the final Vectorposition

This is why you are subtracting both vectors. Then, you are taking the unit as a direction vector only is for a direction, so you only need the magnitude to 1 to be more accurate. Then, you are multiply the directional vector by 300 to say „Hey, from my gun position until 300 i will detect, further no“. With this you can try to reduce the lag of your game. Srry if you find a typo, bad english :frowning:

For more informations you can go to this website here:
MathIsFun
Or try to draw you self to understand, drawing is the best solution (for me, for you idk…)

2 Likes

Let’s drop into 2D and plot out an example.
image
Here, the Gun is positioned at (0, 10) while the Mouse’s Hit is positioned at (10, 15).

When we subtract the the Mouse’s Hit position by the Gun’s position, we get the vector [10, 5]. This is known as the ‘displacement’. Fundamentally this is a useful value as:
(Position[1] + Displacement) = Position[2]
(Position[2] - Displacement) = Position[1]

And when we feed this displacement into Raycast, it’s actually representing both a ‘Mangitude’ and a ‘Direction’.

jif

Direction is the easy part. If our displacement is [10, 5] then that can be interpreted as that in order to go from the Gun’s position to the Mouse’s Hit position, we’d travel 10 Units forwards, and 5 Units up.

Magnitude is actually the length of that line (the length of the raycast).
image
Which in our case is roughly 11.18

When we take the Unit of a vector, what we’re doing is we’re dividing that vector by its magnitude. By doing this, we’re creating a new vector that only has a magnitude of 1, but is still representative of the former vector’s direction. For us, this works out to be about [0.894, 0.447].
image

And this normalized vector (this Unit Vector) turns out to be very useful as it allows us to assign a new Magnitude through multiplication. In your example, the Unit Vector was multiplied by 300, which is to result in a Magnitude of 300 studs. Meaning the Ray will start from the Gun, and continue for 300 studs in the direction toward the Mouse’s Hit. If we were to apply that to our example, the ray would actually extend waaay past the Mouse’s Hit position, ending at about (268.328, 134.164).
image

Knowing this, the math in your example is actually undesirable. Unlike as corrected in my example, it actually subtracts the Initial Position (the Origin Position) from the Final Position, rather than the other way around. This would actually result in the ray traveling the complete opposite direction! Moving away from the Mouse’s Hit Position rather than towards it.
(10, 15) - (0, 10) = (10, 5)
(0, 10) - (10, 15) = (-10, -5)

4 Likes