 # Move mesh to point using vector subtraction

made a simple playground (finally figured out how to use the playground ).

https://playground.babylonjs.com/#8EVURR#2

can anyone show me how to move my cube to the target using vector subtraction? (so, it’ll move maybe idk slowly to the target).

Not sure about substract but here is animated example - https://playground.babylonjs.com/#8EVURR#4

Operating with vectors

Given 2 lines with each 2 points iterating on each line and drawing lines to their neighbors

2 Likes

Thank you for showing me this; do you have an example of just moving a mesh from point a to point b with a vector? I believe you use vector subtraction an x% each frame.

Subtracting two non-normalized vectors will yield a vector that points in the direction you want to move. Vector.Distance is a way of getting just the distance.

The magnitude (length) of the result from subtracting two vectors tells you how far the total distance between the two vectors is, whereas the normalized vector is the directional components.

Every frame, you can perform that operation and then choose to move your object by whatever % of that distance you wish. ‘Scalar.Lerp’ or Scalar.SmoothStep will perform their respective easing operations on the value given the ratio of start position and end position distances.

Ed: here is an algorithm you might implement:

1. Find the direction to move by subtracting two normalized vectors
2. Determine how far the object will move in a single unit of time - it’s speed if you will
3. Multiply (scale) the normalized direction vector by the speed factor. This gives you the magnitude and direction of the position change of your object
4. Add this vector to the moving object’s current world position