Home > Back-end >  How do you get the component of a vector in the direction of a ray
How do you get the component of a vector in the direction of a ray

Time:06-04

I have an object moving in the direction of vector A. There's another vector B which points in an arbitrary direction (but can be considered an infinite line)

I want to get vector C, which is a vector in the direction of B, but with the magnitude of the A's component in the direction of B.

To illustrate, if vector A was Vector(1,.5,0) and vector B was Vector(0,1,0) Then my vector C would be Vector(0,.5,0).

Easy enough if B is aligned on an axis, but how can I do this if B is something like (6,3,8)?

CodePudding user response:

Let C = normalize(B). (divide B by its length)

Then dot(A, C) gives you the length of the desired vector, and dot(A, C) * C is the vector itself.

  • Related