Translating a vector and not the mesh directly


I’m trying to translate a vector and then apply that to the mesh.position instead of working directly on the mesh but am having some trouble.

What works is:

// during setup
this.posStart = this.plane.position.clone();

// this is in the render loop
this.plane.position = this.posStart;
this.plane.locallyTranslate(new Vector3(0, 0, z));

// Result is moving from a fixed position and not incrementing on movement

Issue is I don’t want to have to set the position of the plane to posStart every time and instead work with a vector and then only apply that position to the mesh once it’s been calculated. Something like:

// during setup
var posStart = this.plane.position.clone();

// this happens in render loop
var newPos = this.posStart.clone();
newPos.translate(Axis.Y,2,Space.WORLD); // ERROR: newPos.translate is not a function
this.plane.position = newPos;

What would be the best way to do this?



A translation of a vector is achieved by addition.

To translate vector V by T use


if you want to change the value of V or

W = V.add(T)

to keep V

Other vector3 properties and methods Vector3 - Babylon.js Documentation

This will simply add one vector to another, which is not what I need. The code:

this.plane.locallyTranslate(new Vector3(0, 0, z));

Will translate the mesh/plane in the direction it is facing, in relation to it’s parent space (which vector3 is not aware of), not to mention locallyTranslate is not a method on vector3.

What would the best way be to modify/translate a vector3 to move towards another vector3 (I am modifying the plane to move to vector3(0,0,0)).

What I was looking for was Vector3.Lerp / Vector3.LerpToRef, or the math to make that calculation (I’m a bit rusty with my vector/matrix math).

Or more manually via:

let directionVec = this.startVec.subtract(this.vecZero);
let newPos = new Vector3();
newPos.x = directionVec.x * p;
newPos.y = directionVec.y * p;
newPos.z = directionVec.z * p;
this.plane.position = newPos;