BufferGeometry collisions with raycaster

@Mugen87

i have set up a test environment for detecting collisions with BufferGeometry here…

https://3dpk.co.uk/examples/collisionstest/boxbuffercollision

i used the example code you gave in the previous discussion which should theoretically console.log the objects being collided with a BufferGeometry fixed to the cameras position but this does not detect colisions…

const originPoint = MovingCube.geometry.getAttribute( 'position' );
const localVertex = new THREE.Vector3();
const globalVertex = new THREE.Vector3();
for (let vertexIndex = 0; vertexIndex < originPoint.count; vertexIndex++)
{		
    localVertex.fromBufferAttribute( originPoint, vertexIndex );
    globalVertex.copy( localVertex ).applyMatrix4( MovingCube.matrixWorld );    
    const directionVector = globalVertex.sub( MovingCube.position );

var ray = new THREE.Raycaster( originPoint, directionVector.clone().normalize() );
var collisionResults = ray.intersectObjects( collidableMeshList );
if ( collisionResults.length > 0 && collisionResults[0].distance < directionVector.length() && collisionResults[0].object.name == 'wally'){
    console.log("collision obect 1");                
}
else if ( collisionResults.length > 0 && collisionResults[0].distance < directionVector.length() && collisionResults[0].object.name == 'wally1'){
    console.log("collision obect 2");
}
else if ( collisionResults.length > 0 && collisionResults[0].distance < directionVector.length() && collisionResults[0].object.name == 'wally2'){
    console.log("collision obect 3");        
}
else if ( collisionResults.length > 0 && collisionResults[0].distance < directionVector.length() && collisionResults[0].object.name == 'wallyZ'){
    console.log("collision obect 4");
}                  
}

i made another example of my original method using Geometry as the “MovingCube” collision object which logs collided object to console here…

https://3dpk.co.uk/examples/collisionstest/collision

i’m uncertain what i’m doing wrong for the collisions to not be detected and wondering if you could help me debug the issue, what i might be missing, what i’m doing wrong and if there might be a simpler way of doing this?

question forwarded from here…

thank you :slight_smile:

This line is wrong since originPoint is a buffer attribute in your case. It should be a Vector3.

Besides, reuse a single instance of Raycaster and do not create a new one in each iteration.

1 Like

@Mugen87
ok , thank you for the direction, does this mean i need to modify

const directionVector = globalVertex.sub( MovingCube.position );

i’m not really sure what you mean

yes i see the iterations of raycaster, will move the closing } to just before the raycaster declaration…

Assuming MovingCube is not the child of another 3D object it’s fine. However, you should normalize the direction vector since they always have unit length. Meaning directionVector.normalize().

1 Like

@Mugen87

hey man, thanks for the help! i’m not sure if what i’ve done is exactly what you meant but i’ve managed to get it to work, it doesn’t feel like the right solution as i feel like there are now things that i don’t necissarily need in there… here’s what i have, which is detecting collisions…

const originPoint = MovingCube.geometry.getAttribute( 'position' );
const localVertex = new THREE.Vector3();
const globalVertex = new THREE.Vector3();
for (let vertexIndex = 0; vertexIndex < originPoint.count; vertexIndex++)
{       
    localVertex.fromBufferAttribute( originPoint, vertexIndex );
    globalVertex.copy( localVertex ).applyMatrix4( MovingCube.matrixWorld );  
} 
    const directionVector = globalVertex.sub( MovingCube.position );
var ray = new THREE.Raycaster( MovingCube.position, directionVector.normalize() );
var collisionResults = ray.intersectObjects( collidableMeshList );
if ( collisionResults.length > 0 && collisionResults[0].distance < directionVector.length() && collisionResults[0].object.name == 'wally'){
    console.log("collision obect 1");       

}

would you say this is right or would you say there’s a proper way?

thank you!