1

I've looked around at loads of solutions that others have suggested around this, and I have a certain amount of collision detection already that works fine. But this is centred around one object colliding with many... however I now have to counter for bullets. So I have many objects that could collide with many.

Here's my basic collision detection for my Hero smashing into walls.

    for (var vertexIndex = 0; vertexIndex < hero.geometry.vertices.length; vertexIndex++){      
        var localVertex = hero.geometry.vertices[vertexIndex].clone();
        var globalVertex = hero.matrix.multiplyVector3(localVertex);
        var directionVector = globalVertex.sub( hero.position );

        var ray = new THREE.Raycaster( hero.position, directionVector.clone().normalize() );
        var collisionResults = ray.intersectObjects( collidableMeshList );
        if ( collisionResults.length > 0 && collisionResults[0].distance < directionVector.length() ) 
        {
            console.log('BANG!')
        }
    }

The trouble is if I put this same detection code in a loop for each of the bullets it naturally slows down to an unusable speed.

Is there a more basic detection that I can implement that will tackle this?

I'm slowly getting my heads around three.js and am sure I'm missing something simple but just couldn't understand other examples. Any help or a good point in the right direction would be greatly appreciated.

Edit

I've been playing around, obviously my first attempts have failed and I'm not much closer but wondering if I would be closer on the lines if I was to try getting the bullets position and see if its position is within the my an object inside my collisions array.

Not sure, I've seen that plenty of people have come across this before but struggling to find a solution that is defined enough for me to understand.

1 Answer 1

0

Ok... my collidableMeshList needed some clearing up, but I found this approach did not cause too many issues with the performance (as in it didn't slow things down too much) I found this from this thread

Take a look at the code, if anyone feels that this can be further improved let me know!

bullets.forEach(bullet => {
    var bulletBB = new THREE.Box3().setFromObject(bullet);
    var enemyBB = new THREE.Box3().setFromObject(enemy);
    var bulletCollision = bulletBB.isIntersectionBox(enemyBB);
    if(bulletCollision){
        console.log('Hit Enemy!');
    }
}

Check out the thread attached for further information into how it works.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.