Move a 3D Vector on a plane and keep the magnitude

928 Views Asked by At

I have a Vector3(x,y,z) and I want to "rotate" (don't know the correct term) on a plane (Y=0).

I got a good result (not that good) if I just update the vector.y value to 0. In that way x, y and z have the good direction but his magnitude is lower (I just made a projection, not the good rotation).

I don't know I'm understood so I made an ugly draw:

enter image description here

Currently I have the orange vector on result, and I want the brown one (who start at the origin)

How can I get the good x and z to get the same vector with y=0 ?

1

There are 1 best solutions below

0
On BEST ANSWER

Pseudocode:

New.x = old.x * Sqrt((old.x^2 + old.y^2 + old.z^2) / (old.x^2 + old.z^2))
New.z = old.z * Sqrt((old.x^2 + old.y^2 + old.z^2) / (old.x^2 + old.z^2))
New.y = 0

Caveat: if old.x = 0 and old.z = 0, it is unclear to which vector on the plane you want to map to. And the pseudo-code above will give division by zero. You will have to deal with that special case separately.