[Solved] Vector change length

Hi again guys,

I have a problem calculating a vector, that may be more a mathematical problem, and I cant figure out if I’m a jerk because I can’t find the correct function or because I always had F in maths.

Here’s the thing : I have two vectors, the green and the red. The green always points upward but the red can have differents direction and coordinates (and, as a consequence, can differ in length). I know the length of these two. What I want to do is create a Vector3f (the purple one on the picture) which has the same direction as the red one but the same length as the green one.


How can I do this ? I tried to use the scaleAdd function of the Vector3f class like this :

Vector3f purpleVector = redVector.scaleAdd((greenVector.length()/redVector.length())/100, new Vector3f(0.0f, 0.0f, 0.0f));

But the result is a very tiny vector, that's strange...

Any ideas, guys ?
purpleVector = redVector.normalize()*greenVector.length()

Probably not the most efficient, but it's easy to understand.
1 Like

multiplyiing a vector with a float will only change its length:


WIkipedia might be quite complicated, but it gives you the name of what youre looking for :slight_smile:

Kwando is doing exactly that.

He is normalizing the vector, (set length to 1) and then multiplying its length by the greenVectors length.

1 Like

Scalar multiplication is exactly what I was looking for, thank you lads !

It works, the exact expression for what I want is :

Vector3f purpleVector = redVector.normalize().mult(greenVector.length());

Damn, if these demonic math teachers could speak in a common language, I could handle myself easily...

I tried to talk in a non-demonic way here :wink: https://wiki.jmonkeyengine.org/legacy/doku.php/jme3:math_for_dummies