How to make the Camera have collision detection Physi.js First Person Shooter

678 views Asked by At

Project Premise

The idea for my THREE.js game is the have the player navigate though a maze of objects and reach an exit. I need collision detection not just for cubes and spheres, but also models imported from Blender. I thought about using RayCasting but I decided to go with Physi.js.

The Problem

,The idea is to give the camera a "body" so that the player can't pass though wall or other objects like they can when they are just a camera object. However, My "player" object seems to loses all of its Physi.js attributes as soon as I give it THREE.PointerLockControls(player). What I'm doing is creating a Physijs.BoxMesh, the player, and adding the camera to that. After that I pass the "player" object to my setupControls() function.

        var geometry = new THREE.BoxGeometry( 500, 500, 500 );
        var material = new THREE.MeshPhongMaterial( {color: 0x0000FF} );
        player = new Physijs.BoxMesh(geometry, material, 1, {restitution: .9, friction: .1});

        scene.add(camera);
        scene.add(player);
        player.add(camera);


        //give player control of THIS mesh.
        setupControls(player);

The Controls work as expected, I can control the player mesh with the camera stuck to it creating an FPS view, but I when I do this I can still move though walls and other objects. I've even moved the camera back on the Z position to confirm whats going on and I can see that the player mesh is just passing though objects instead of being hindered or knocking them over.

Solutions?

If I remove setupControls(player), then the Physi.js physics begin to work on the player mesh! It will fall from gravity, bounces around and everything. It looks really cool, but now I can't control it! It seems like I can only have one or the other lol. So does anyone know what I could do to solve this problem? Is what I'm preposing even possible? I'm new to THREE.js so any input is much appreciated!

0

There are 0 answers