TheProgrammer
TheProgrammer

Reputation: 1489

Trying to get the point of intersection of a raycaster

QUESTION:

I load frontObject onto the scene. I get the centre of said object.

Then I move beyond the object by setting a new point that reuses all the object's centre coordinates except the Z axis (the z coordinate becomes 100), so that I can trace a raycaster from the outside of the character (frontObject) towards it and get the intersection point on the back of the character so that I may place a shield on that point.

Sadly, I get the following output:

enter image description here


CODE:

let box = new THREE.Box3().setFromObject(frontObject);
            let sphere = box.getBoundingSphere();
            let centerPoint = sphere.center;

            backObject.position.set(0,132,-15);

            console.log("CENTER POINT X: "+centerPoint.x);
            console.log("CENTER POINT Y: "+centerPoint.y);
            console.log("CENTER POINT Z: "+centerPoint.z);

            centerPoint.z = 100;

            var newCoordinate = shootRay(centerPoint, frontObject);

            console.log("NEW POINT X: "+newCoordinate.x);
            console.log("NEW POINT Y: "+newCoordinate.y);
            console.log("NEW POINT Z: "+newCoordinate.z);

function shootRay(center, frontObject) {

var raycaster = new THREE.Raycaster();
var direction = new THREE.Vector3( 0, 0, 1 );
raycaster.ray.direction.copy( direction );
raycaster.ray.origin.copy( center);

var intersects = raycaster.intersectObject(frontObject);
console.log("GO");
if (intersects) {
    var point  = intersects.point;
    console.log("POINT:"+point);
    return point;

}

}


EDIT:

https://jsfiddle.net/Username100/y54kpe1h/66/

Upvotes: 1

Views: 837

Answers (1)

manthrax
manthrax

Reputation: 5016

Your code is somewhat buggy. You'll want to read and address the warnings that you posted.

For var intersects = raycaster.intersectObjects(frontObject); frontObject needs to be an Array like [frontObject] or you need to use the singular intersectObject.

I also recommend using a debugger like the one built in to chrome to put a break point, instead of console.log('POINT , and then seeing what you're getting back from the raycast.

Let me know if this helps...

Upvotes: 1

Related Questions