chandlerprall / Physijs

Physics plugin for Three.js
MIT License
2.75k stars 455 forks source link

Lag click removing physic mesh #265

Open progettoPGTR opened 8 years ago

progettoPGTR commented 8 years ago

I have a scene with a physic mesh rotating and an airplane. When there is the collission between them, the rotate mesh is removed and re-created in another place of scene. In that moment (removing mesh) there is s little lag, not good for the game. Is it a problem inside the code (See project.html in project.zip)?

Project.zip

ghost commented 8 years ago

@progettoPGTR I'm seeing 2 problems in project.html file.

Firstly, your collision detection code is in pure THREE.js when you could be letting physijs do the work for you:

modelMoveMesh.addEventListener("collision", function(collider_mesh){
    // Insert collision code here...
});

More information can be found about physijs events here. Of course, if you wanted more accurate collision detection without more accurate (and much, much slower) physics your setup is fine.

Secondly, you are removing an entire model every time a collision occurs, which puts a heavy load on the game and thus probably generated the lag you were experiencing. Instead, why not hide the model from plain sight if you don't want it to be seen? You can set modelMoveMesh.visible to false and it will never reveal itself. To move your mesh while it is still in the scene and simulating, you must set modeMoveMesh.__dirtyPosition flag to true so that you can move it freely. After that, you do your changes and set modelMoveMesh.visible to true again when you are ready. More information about moving physijs meshes found here.

progettoPGTR commented 8 years ago

Hi,if i use collision event callback it doesn't work. I need with:ufoMesh.addEventListener( 'collision', function( ufoMeshCollision ) { console.log("hit");}); ufoMesh is json model with physic.ufoMeshCollision is a three js mesh without physic but it is child of mesh with physic.When i start the console logs "hit" always, not when i have collision between them.

----Messaggio originale----

Da: Xavier C. notifications@github.com

Data: 13-apr-2016 0.21

A: "chandlerprall/Physijs"Physijs@noreply.github.com

Cc: "progettoPGTR"maimo89@libero.it

Ogg: Re: [chandlerprall/Physijs] Lag click removing physic mesh (#265)

@progettoPGTR I'm seeing 2 problems in project.html file.

Firstly, your collision detection code is in pure THREE.js when you could be letting physijs do the work for you:

modelMoveMesh.addEventListener("collision", function(collider_mesh){ // Insert collision code here... });

More information can be found about physijs events here. Of course, if you wanted more accurate collision detection without more accurate (and much, much slower) physics your setup is fine.

Secondly, you are removing an entire model every time a collision occurs, which puts a heavy load on the game and thus probably generated the lag you were experiencing. Instead, why not hide the model from plain sight if you don't want it to be seen? You can set modelMoveMesh.visible to false and it will never reveal itself. To move your mesh while it is still in the scene and simulating, you must set modeMoveMesh.__dirtyPosition flag to true so that you can move it freely. After that, you do your changes and set modelMoveMesh.visible to true again when you are ready. More information about moving physijs meshes found here.

— You are receiving this because you were mentioned. Reply to this email directly or view it on GitHub