Question

I'm having trouble getting the coordinates of where the player is looking in the world using a Raycaster. It's not picking up on any intersections.

More specifically, I'm trying to get the coordinates of the intersection on the terrain, which is made up of multiple BufferGeometry meshes. Here's the code I'm using taken from the Raycaster example: (groupedMeshes array stores the BufferGeometry meshes)

var mouseX = ( event.clientX / window.innerWidth ) * 2 - 1;
var mouseY = -( event.clientY / window.innerHeight ) * 2 + 1;

var vector = new THREE.Vector3( mouseX, mouseY, camera.near );

// Convert the [-1, 1] screen coordinate into a world coordinate on the near plane
var projector = new THREE.Projector();
projector.unprojectVector( vector, camera );

var raycaster = new THREE.Raycaster( camera.position, vector.sub( camera.position ).normalize() );

// See if the ray from the camera into the world hits one of our meshes
var intersects = raycaster.intersectObject( groupedMeshes );
lastIntersects = intersects;

if ( intersects.length > 0 ) {
    console.log("Intersection!");
}

And an image showing the player's view:
(The red dot explaining that I want to cast a ray from the center of the camera to the terrain)

enter image description here

The mouse/pointer is locked (1st person camera view) so I'm assuming I want to check for an intersection at the center of the screen? The code above doesn't do that so I've been testing without locking the pointer.

If anyone could see why it's not detecting any intersections, I would really appreciate the help. Thanks.


EDIT:
Seems I should be using raycaster.intersectObjects( groupedMeshes ); not raycaster.intersectObject( groupedMeshes );. My bad.

So my only question now is how do I shoot the ray from the player's (first person) view to the terrain. Thanks!

EDIT2:

var vector = new THREE.Vector3( 0, 0, 0.5 );

var projector = new THREE.Projector();
projector.unprojectVector( vector, camera );

var raycaster = new THREE.Raycaster( camera.position, vector.sub( camera.position ).normalize() );

var intersects = raycaster.intersectObjects( groupedMeshes );
lastIntersects = intersects;

if ( intersects.length > 0 ) {
    console.log(intersects);
}
Était-ce utile?

La solution

Worked it out from looking at this example script.

I'm now using this, which allows me to shoot a ray from the location of the camera in the direction player/camera is facing:

var raycaster = new THREE.Raycaster();
var direction = new THREE.Vector3( 0, 0, -1 );
var rotation = new THREE.Euler( 0, 0, 0, "YXZ" );

function shootRay() {
    rotation.set( pitchObject.rotation.x, yawObject.rotation.y, 0 );

    raycaster.ray.direction.copy( direction ).applyEuler( rotation );
    raycaster.ray.origin.copy( yawObject.position );

    var intersections = raycaster.intersectObjects( groupedMeshes );
    if ( intersections.length > 0 ) {
        console.log(intersections[0].point);
    }
}
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top