Calculating a weighted midpoint between two 3D (XYZ) magnitude direction vectors

59 Views Asked by At

I'm really struggling. I have two "3D direction/motion magnitude" vectors for a 3D game engine.

The vectors consist of XYZ 3D components in the range 0.0 - 1.0 (they're normalized), which represent the magnitude of motion in that 3D direction. It can be used for displacing the model's position in that direction by simply multiplying the "direction magnitude" vector with the desired distance, and using that as the new XYZ 3D position's offsets.

I have access to the "forward" and "right" vectors, which represent the magnitudes for moving the 3D model "forwards" and "right", both of which are relative to the 3D model's "heading/facing direction".

The goal is to generate movement at an angle between the "forward" and "right" direction, let's say "80% forward, 20% right", meaning that the character should move "forward-ish with a slant towards the right".

I've been trying to find articles about this but failing (everyone else talks about angle/yaw/pitch vectors which is not what I am using), so I thought about it a bit, and came up with this idea, which seems to be working, but I'm not sure if it's the correct algorithm or if it would require more complex math.

Since I am only dealing with normalized 3D XYZ magnitude vectors, perhaps my solution is correct?

-- Bias the direction by changing the weight, 0.5 means midpoint between the two.
local weight = 0.5
forward_vec:mul(1.0 - weight)
right_vec:mul(weight)

-- Calculate final motion vector (result is in forward_vec).
forward_vec:add(right_vec)
forward_vec:normalise()

This seems to generate the desired outcome but I'm scared and I'd really love to hear an expert's insights.

1

There are 1 best solutions below

1
On BEST ANSWER

Yes this should work.

If you have two points $A$ and $B$ such that $\vec{OA}$ is the forward vector and $\vec{OB}$ is the right vector, for any weight $\omega \in (0,1)$ then we have that the vector $$ (1- \omega)\vec{OA} + \omega \vec{OB} $$ is equal to $\vec{OC}$ where $C$ is a point which lies on the line segment $AB$.

For $\omega = 0$ we find $C = A$, for $\omega = 1$ we find $C = B$, for $\omega = 1/2$ we find $\vec{OC} = \frac{\vec{OA} + \vec {OB}}{2}$ so $C$ is the midpoint between $A$ and $B$.

This is the desired vector which you can then normalise if you whish.