Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

THREE JS change vector length by absolute value

Tags:

three.js

I have two Vector3 points A and B.

I want to get vector C placed of trajectory path from A to B, but add to it 100 pint to his length.

How can i calculate this vector?

enter image description here

like image 916
Martin Avatar asked Dec 15 '14 15:12

Martin


2 Answers

You can increment a non-zero-lenth vector in three.js by length len like so:

var oldLength = vector.length();

if ( oldLength !== 0 ) {

    vector.multiplyScalar( 1 + ( len / oldLength ) );

}

So, here is how you compute the point C.

var A = new THREE.Vector3( 10, 20, 30 );
var B = new THREE.Vector3( 20, 30, 40 );
var C = new THREE.Vector3();

var len = 10;

C.subVectors( B, A ).multiplyScalar( 1 + ( len / C.length() ) ).add( A );

three.js r.69

like image 124
WestLangley Avatar answered Nov 09 '22 01:11

WestLangley


In a two dimentional context, this will give you the coordinates of C:

 var oldLength = A.distanceTo(B);
 var newLength = oldLength + 100;

if(oldLength > 0)
{
  C.x = A.x + (B.x - A.x) * newLength / oldLength;
  C.y = A.y + (B.y - A.y) * newLength / oldLength;
}
like image 43
BaptisteB Avatar answered Nov 09 '22 00:11

BaptisteB