Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

THREE.Raycaster intersecting original mesh position after position.set()

Using Three.js, when I position an object using position.set(), THREE.Raycaster does not seem to know about the objects new set position. If I leave the objects at 0,0,0 the mesh is properly intersected.

function init(){
var targets = [];
var object;
// More scene initializing code...

// creating a cube object and placing it at (-10,0,-10)
    var objectGeometry = new THREE.CubeGeometry(2,2,2);
    var objectMaterial = new THREE.PhongMeshMaterial({color: 0xEEEEEE });
    object = new THREE.Mesh(objectGeometry, objectMaterial);
    object.position.set(-10, 0, -10);
    scene.add(object);
    targets.push(object);

// more objects created and added to the scene

}

I have a character controller that I can move around with a camera that follows so I plug in the character (obj) and the targets to test in a function testRay();

function testRay(obj,targets,distance){
// code to keep the ray pointing out in front of the character at all times
    var endRayX = (Math.cos((270*(Math.PI/180))-obj.rotation.y)*distance + obj.position.x);
    var endRayZ = (Math.sin((270*(Math.PI/180))-obj.rotation.y)*distance + obj.position.z);
    var vector = new THREE.Vector3(endRayX,obj.position.y,endRayZ);
    var raycaster = new THREE.Raycaster(obj.positon,vector);
    intersects = raycaster.intersectObjects(targets);
    if(intersects.length>0){
        console.log("intersects.length: "+ intersects.length);
        console.log("intersects.distance: "+ intersects[0].distance);
        console.log("intersects.face: "+ intersects[0].face);
        console.log("intersects.point: " + intersects[0].point);
        console.log("intersects.object: " + intersects[0].object);
    }
}

Then every requestAnimationFrame(main) call, I testRay() plugging in the character controller and the targets to see if it intersects.

function main(){
    // some other code
    testRay(character,targets,30)
    window.requestAnimationFrame(main);
}

Okay, so the problem is not getting it to intersect, that works great. It is getting it to intersect the objects' new location after using position.set() during creation. I noticed that I could move objects around simply by intersecting them and that seems to move the intersection coordinates as well. Would it be better to move the object after adding it to the scene? Is there a difference?

like image 601
SacWebDeveloper Avatar asked Dec 05 '25 16:12

SacWebDeveloper


1 Answers

This question is a little old but maybe this will help someone: I was having a similar problem. It seems that three.js uses the objects world transform matrix for ray intersection and depending on your order of operations, this may not have been updated after position.set(); and before the raycaster.intersectObject(); call. I believe that this is done automatically by three when the object is rendered in the scene, but in my case the object wasn't actually in the scene. All I had to do was call object.updateMatrixWorld() before doing the raycasting.

like image 152
rydrman Avatar answered Dec 08 '25 04:12

rydrman



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!