schteppe / cannon.js

A lightweight 3D physics engine written in JavaScript.
http://schteppe.github.com/cannon.js
MIT License
4.71k stars 710 forks source link

Load obj file to a trimesh object #278

Open danielszh opened 8 years ago

danielszh commented 8 years ago

Hi,

Is there a way to load obj file to a trimesh object in cannon.js?

Thx

danielszh commented 8 years ago

Actually, I want to do a collision detection project with cannon.js. I used the bunny.js model in https://schteppe.github.io/cannon.js/demos/bunny.html I made two bunnies moving toward each other to collide. When they collided, the FPS dropped down to only 1. It is extremely slow.

I am wondering why. And I want to know if connon.js uses any AABB tree structure (Bounding Volume Hierarchy) to improve the efficiency.

Thank you very much.

donmccurdy commented 8 years ago

@danielszh AABB is helpful when two bodies are distant, and you want to quickly verify that they're not close enough to bother examining closely. Where your FPS is dropping, the bodies were close enough that Cannonjs needed to examine every face on each model, and this is slow.

If your game/scene doesn't depend too much on highly realistic bunny collision simulation, you should consider simulating each model with a less complex shape. At the minimum, lowering the polycount is a good idea. Better would be to use a convex hull around each bunny, or a bounding box for best performance. Example code for generating convex hulls and bounding boxes for a model here.

yasuomang commented 2 years ago

@danielszh AABB is helpful when two bodies are distant, and you want to quickly verify that they're not close enough to bother examining closely. Where your FPS is dropping, the bodies were close enough that Cannonjs needed to examine every face on each model, and this is slow.

If your game/scene doesn't depend too much on highly realistic bunny collision simulation, you should consider simulating each model with a less complex shape. At the minimum, lowering the polycount is a good idea. Better would be to use a convex hull around each bunny, or a bounding box for best performance. Example code for generating convex hulls and bounding boxes for a model here.

If my scene depend on highly realistic bunny collision simulation,what should i do to improve performance

donmccurdy commented 2 years ago

The more geometry you have in your simulation the more expensive it will be. Especially if that geometry cannot be simplified to a convex hull or simpler shapes, which are faster for collision detection. Perhaps some physics engines have optimizations to handle a bit more (I don't know), but additional mesh detail is never free. A high-quality video game (for example) is not going to do collision detection on the same high-detail meshes they use to render, they'll use much simpler collision geometries instead.