arpo
arpo

Reputation: 1889

Get direction between two 3d vectors using Three.js?

I have two points:

v1 = (0, 0, 0);
v2 = (10, 4, -3);

I want to get the direction between these two points so I can rayCast from point v1 to v2.

How do I do that?

Upvotes: 13

Views: 18843

Answers (2)

WestLangley
WestLangley

Reputation: 104763

The pattern to follow to create a direction vector from v1 to v2 is this:

var dir = new THREE.Vector3(); // create once an reuse it

...

dir.subVectors( v2, v1 ).normalize();

Direction vectors in three.js are assumed to have unit-length. In other words, they must be normalized. If the direction vector you use when raycasting does not have length equal to 1, you will not get accurate results.

three.js r.82

Upvotes: 35

Hellium
Hellium

Reputation: 7346

A little bit of math : direction = v2 - v1

Plus, a little bit of ThreeJS of documentation : https://threejs.org/docs/#api/math/Vector3

Aannd ... Paf ! Solution !

 var direction = new THREE.Vector3();
 direction.subVectors( v2, v1 ) ;

Upvotes: 6

Related Questions