TeaAnyOne
TeaAnyOne

Reputation: 497

Simple collision detection three.js

Is there a way to implement simple collision detection, preferably using ray casting

Here's a pictures of my game. The randomblock moves down the board and the paddle has to avoid it

all my objects are created by using this code:

paddle = new THREE.Mesh(

        new THREE.CubeGeometry(
                paddleWidth,
                paddleHeight,
                paddleDepth,
                paddleQuality,
                paddleQuality,
                paddleQuality),                                

                paddleMaterial);

Ive tried using the raycasting solution before but it never seems to work

enter image description here

Upvotes: 0

Views: 3644

Answers (1)

Kahless
Kahless

Reputation: 1253

Hey just a few notes for yea. You should start using your browser console to check for error message's you have a few going on. if your using firefox or chrome hit CTRL + Shift + J then click console. CubeGeometry has been changed to BoxGeometry. Also I changed up your controls a bit. J and K now slide the thing back and forth. Here's the link to the new Codepen with working collision detection.

http://codepen.io/anon/pen/GHofz?editors=001

function collision() {
    var originPoint = paddle.position.clone();
    for (var vertexIndex = 0; vertexIndex < paddle.geometry.vertices.length; vertexIndex++) {   
        var ray = new THREE.Raycaster( paddle.position, paddle.geometry.vertices[vertexIndex] );
        var collisionResults = ray.intersectObjects( collidableMeshList );
        if ( collisionResults.length > 0)  {
           hit = true;
        }
    } 
}   

Let me know if you have any questions.

Upvotes: 2

Related Questions