Help understanding these lines

hi
can some one help to understand what every line does ,thanks

var localVertex = MovingCube.geometry.vertices[vertexIndex].clone();
var globalVertex = localVertex.applyMatrix4( MovingCube.matrix );
var directionVector = globalVertex.sub( MovingCube.position );
		
var ray = new THREE.Raycaster( originPoint, directionVector.clone().normalize() );
var collisionResults = ray.intersectObjects( collidableMeshList );

thanks, this is great forum , i learning a lot in here

// select a vertex in local space from the geometry
var localVertex = MovingCube.geometry.vertices[vertexIndex].clone();
// transform this vertex to world space (you should use MovingCube.matrixWorld for this)
var globalVertex = localVertex.applyMatrix4( MovingCube.matrix );
// compute a direction vector (this type of vector always has unit length so normalize() should already called here)
var directionVector = globalVertex.sub( MovingCube.position );

// setup the raycaster
var ray = new THREE.Raycaster( originPoint, directionVector.clone().normalize() );
// perform intersection test
var collisionResults = ray.intersectObjects( collidableMeshList );

The usage of .clone() like shown here is a typical beginner error since you create a lot of unnecessary objects, especially if you run this code in the animation loop. The idea is to reuse objects e.g.

var localVertex = new THREE.Vector3(); // define this once e.g. at the top of your module

// later, just copy vertices

localVertex.copy( MovingCube.geometry.vertices[vertexIndex] );
2 Likes

wow , great answer, thanks

just one question…what is “world space”

3D coordinate systems are explained in many resources, e.g.:

https://learnopengl.com/Getting-started/Coordinate-Systems

1 Like