Search code examples
javascriptthree.jsjavascript-objectsphysijs

Extending Physijs.Mesh


I am working with Physijs to create a simple fps. Without Physijs I would create a Player object and use the code below to extend the THREE.Mesh class.

function Player() {
    THREE.Mesh.apply(this, arguments);
    ...
}
Player.prototype = Object.create(THREE.Mesh.prototype);

Then I would instantiate a Player and add the camera to it for first person vision. However I would have to program all of the collision detection for Player so he wouldn't fall through the floor. I can use new Physijs.BoxMeshfor the floor, but what about the player? How do I create a 'Player' class that has physics applied to it using Physijs? I tried something like... Physijs.Mesh.apply(...)... and Object.create(Physijs.Mesh.prototype), but that doesn't work.

Thank you for any feedback.


Solution

  • To do collision detection between meshes, all the meshes must be known to physi.js. In other words, you must use one of the physi.js shapes.

    So you need a physi.js mesh for your Player so he doesn't fall through the floor. This doesn't have to be the same mesh that is visible to the user. You can have an invisible mesh that follows the player around and perhaps has a simpler geometry.

    If you want anything more than one of the basic shapes (box, plane, sphere, etc) for your Physi.js mesh, yo'll need to build composite shapes.