schteppe / cannon.js

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

Custom mesh and softbodies #43

Open circlegarage opened 11 years ago

circlegarage commented 11 years ago

First of all, many compliments for the awesome work.

Just two questions:

many thanks and again great work!

schteppe commented 11 years ago

Hi! Out of the box, it is not possible to import meshes with cannon.js like that. Here's a related project though, it's able to export a scene from Blender, load into Three.js and then create a Cannon.js scene. Works with boxes at the moment.

Cannon.js does not have support for arbitrary trimeshes. It does only support convex meshes, boxes, spheres, particles, and assembled shapes. I'd like to recommend you to make a simplified physics model for any object you want to simulate. That way you can improve performance greatly. There are very few cases you want to use the same geometry for physics as for rendering. (insert joke about spherical cows in vacuum here)

I haven't studied soft body dynamics, only rigid, so I can't answer your second question. However, you may be able to do what you want using constrained particles, each particle being a vertex. Have a look at the "3D cloth structure" scene in the constraints demo. Try using a less stiff constraint (set solver.k to a smaller value) and make sure to render the constraints to see what is really happening.

Thank YOU for looking into my project :)

kmcminn commented 11 years ago

I haven't tried this but I assumed that using child or parent geometries (natively or via another object relationship) on your mesh would work?

circlegarage commented 11 years ago

mmmhhh ideas:

IMHO these could be the two solution to work with. I tried to have a little mesh with particles but the big problem is that i can achieve a very poor level of performance. Do you think that using a webworker the simulation could be faster?

thanks

schteppe commented 11 years ago

The effect you would get by making a closed cloth and constraining the vertices (particles) with distance constraints to each other and e.g. the center of the shape, would be interesting to see. You should be able to control the softness of the body with the spring parameters to make it behave like you want. This solution has a few drawbacks. For example: how to create concave shapes this way? And another thing: the shape created will not have a surface, just particle vertices. If you try to collide two of these, they will just fall through each other. Other shapes that does not fall in between the particles would be fine though.

Running the simulation in a separate thread may increase the total performance of your app. You still have to exchange data in between threads though, that may slow down. Have a look at how Chandler did it in Physijs.

schteppe commented 11 years ago

How's it going with your soft bodies and meshes? I was wondering what is your goal with the soft bodies? Do you want to simulate a pillow? A deformable car wheel? Maybe I could help you better if I knew!

circlegarage commented 11 years ago

hei schteppe,

sorry for the delay. What I want is to simulate human organs, so something similar to jelly bodies. I was wondering to implement a sort of "central bone" in a custom mesh as you were already thinking, but not a single point (the centrer of the shape). Than constraining the bone to the external points on the surface of the mesh. IMHO in this way we could achieve a good level of realism. The problem is that cannon.js doesn't support meshes other than convex or basic geometries...

I tried using your constraints demo to increment the points in a very simple way (having as a result a long parellelepiped) and results are encouraging.

Thinking about a simple cube we could already have soft bodies simulation just incrementing the total number of constrained points of the cube surfaces...problem is that constraints are very time consuming....

Next step is to grasp the object to perform drag&drop operations.

Now I'm using Unity3D, but I'm very disappointed by the Physix system....(objects trough others for example is very disppointing) and I was considering to use cannon.js

Thanks schteppe!!!

circlegarage commented 11 years ago

just as a brief example, check this out:

http://egraether.com/demos/soft/index.html

schteppe commented 11 years ago

Hi again, What about using a rigid body as center bone and constraining particles to its surface? That would for sure be possible to do. How this should look geometrically is still a question to me though... Anyway, to do this, first construct the center bone body. Could be a CANNON.Cylinder or similar. Then use CANNON.PointToPointConstraint to connect particles to the center bone surface. In this case, the "pivotA" vector goes from the center of mass in the bone to a point on its surface. The second pivot vector should go from the particle to the same point on the bone. The center bone does not have to be a cylinder either. You could set it to be a cube or anything. You're still able to connect any point inside or outside it to a particle pivot point using the PointToPointConstraint. Not sure if all this is understandable... Point is that you don't have to constrain the particles to one center of mass. Maybe I should make you a simple drawing to be able to explain better?

By the way, cool demo :D

circlegarage commented 11 years ago

@schteppe mmhhhhh i think i'm not completely understand. Could you give me some code snippets to make some tests?

thanks bro!

schteppe commented 11 years ago

I'm swamped at the moment, but will try to find some time. I really love the demo, it's awesome :) Just wish there were settings for spring stiffness and solver etc.

circlegarage commented 11 years ago

ehi schteppe any news on this?

schteppe commented 11 years ago

No, sorry.

bnolan commented 10 years ago

Hey @schteppe, any plans to add custom trimeshes to cannon.js? I'd prefer not to use ammo.js since it's so massive, and all I really need is collision detection for my FPS. Is there a way this could be developed? How would you recommend I start out?

schteppe commented 10 years ago

I don't like trimeshes because they have bad performance and need small timesteps to function properly. Game devs start using them but quickly switch to convexes (or other primitives) instead, to get better performance.

Because of this, I've not prioritized trimeshes much. There are other things needed in Cannon that are more important.

What do you need the trimesh for? Maybe you can construct your game physics some other way?

If you really want a trimesh implementation, you can start off with a simple case such as Trimesh/sphere. A first implementation could naively loop over all faces and edges in the trimesh and check for intersections with the sphere. A more optimized version would include a hierarchical volume tree to cut down the number of triangle intersection tests.

bnolan commented 10 years ago

Okay, thanks for the pointers. I'm trying to creating really simple walkthrough visualizations of sketchup architectural models. I've found the HACD library for decomposing meshes into convex hulls. I'll try it out and see if I can get it to work with cannon.js. Thanks for your pointers.

lazd commented 10 years ago

@bnolan, did you end up getting HACD to work with Cannon?

bnolan commented 10 years ago

Hey Larry,

I couldn’t get hacd to accurately map my model (the barcelona pavillion), so I never got to try it with cannon.

Ben

On 22/08/2014, at 6:41 am, Larry Davis notifications@github.com wrote:

@bnolan, did you end up getting HACD to work with Cannon?

— Reply to this email directly or view it on GitHub.

bnolan commented 9 years ago

I'm getting to the point where I might try implementing mesh support. Even if it's really slow to start with, it'd help me out a lot, and I think we maybe be able to use bxh as an acceleration structure.

bnolan commented 9 years ago

Ok, so I had a bit more of a play with HACD and successfully created a mesh. @schteppe, how did you convert the .wrl output from testHACD into the .js description you use for the bunny demo? I tried using meshconv, but if doesn't keep the group information from the hacd output, just making one big mesh which is kind of useless.

schteppe commented 9 years ago

@bnolan Awesome! Please let me know how it goes. Can you point me to your fork if you start working on the new mesh shape?

It was a while ago, I think I did it manually by copy pasting and applying some text editor magic tricks. The .js file format I produced is not standard in any way.

schteppe commented 9 years ago

@bnolan I added trimesh support along with plane collisions recently. No bvh structure yet though, but at least the basic stuff is there. https://github.com/schteppe/cannon.js/commit/b45172f1729431a08dabb4fe71b39054b5bc0094

farhanRIT commented 4 years ago

Hi, is there a best way to create a hoop? Like a basketball hoop? I tried using Trimesh, it is somewhat working but the physics sometimes just all over the place. The ball (a sphere) may behave weirdly sometimes. But for some reason adding more mesh makes it a little bit better but drops the performance a lot. I just wondering if anyone know a best alternative to create a hoop collision? Maybe it doesn't need to be a hoop but something that can simulate a collision with a hoop? Basically I am trying to create a simple basketball game throwing the ball into the hoop.

Another thing is, the cloth, is there a way to create a cylinder cloth? Is that possible? The examples I have seen are all a single plane. I just wondering if I can create a cloth effect for the goal of the basketball hoop (an open ended cylinder where the ball can go through with cloth effect)?