piqnt / planck.js

2D JavaScript Physics Engine
http://piqnt.com/planck.js/
MIT License
4.9k stars 236 forks source link

Intersections between edge bodies or chain bodies #105

Open Boyan-In-Christ opened 5 years ago

Boyan-In-Christ commented 5 years ago

Dear Ali, first thank you for this simple and powerful library you have developed. I am using it in my research. And I met a problem. when I try to make packed convex hulls. These hulls I used connected "edge" shape. The code is like this:

 var convex_edge_i = planck.Edge(planck.Vec2(hull[i].x, hull[i].y), planck.Vec2(hull[i + 1].x, hull[i + 1].y));
var fixture_convex_i = body.createFixture(convex_edge_i, 0);

when I use DistanceJoint to drew these "convex hull" body together, After the simulation, it results these intersections. The setting of DistanceJoint I used is:

                    let distanceJoint = planck.DistanceJoint({
                        frequencyHz: 0.4, 
                        dampingRatio: 0.001  
                    },

The simulation setting is:

                let timestep = 1.0 / 10.0;
                let velocityIterations = 5;
                let positionIterations = 2;
                // Simulation loop
                // attraction simulation
                for (let i = 0; i < 300; ++i) {
                    // Instruct the world to perform a single step of simulation.
                    // It is generally best to keep the time step and iterations fixed.
                    world.step(timestep, velocityIterations, positionIterations);
                }

When I change this convex hulls into "chain" type, seems the result it the same. Do you have any idea about this? Do these settings influence the result? How to avoid intersections? Many thanks!!

Boyan-In-Christ commented 5 years ago

Dear Ali,

Thank you so much for your quick reply. But for some reasons I also need to make several polygons be contained in a big polygon. Does it possible by using polygon type?

Thank you so much!

Could you try using polygons instead?

As far as I know edge/chair work with static bodies (like ground), so not useful for this case.

shakiba commented 5 years ago

As far as I know if your container is dynamic this is the common solution: http://piqnt.com/planck.js/Tumbler

Boyan-In-Christ commented 5 years ago

Thank you so much Ali, this helped a lot. it is now much better. I referenced http://piqnt.com/planck.js/Tumbler also http://piqnt.com/planck.js/Mixer Both "polygon" or "edge" seems could work as the element of building the bounding box for lots of objects. I did two implementations. The first one still have some intersections. The second one seems like what you mentioned before. One more question: When I use the first implementation how to avoid intersections? Can I set some repelling force on the body, so that when such attachments exist, the body can be separated? Each body can keep some distance from other bodies? Thank you so much!!

shakiba commented 5 years ago

Could you share your testbed on codepen/jsbin?

Boyan-In-Christ commented 5 years ago

Dear Ali, I made a codepen, here is the link: https://codepen.io/bozheng-stokes/pen/abormNX There can be intersections somehow. You could also change "box" to "edge", by change var use_box = true; var use_edge = false; to var use_box = false; var use_edge = true;

Thank you very much for your advice on this.

shakiba commented 5 years ago

Could you try setting walls as bullet?

shakiba commented 5 years ago

~Bullet didn't fix it, but I was able to fix it with this:~

            let distance_joint_setting = {
                frequencyHz: 0.4,
                dampingRatio: 0.01
            };
shakiba commented 5 years ago

Great example by the way, thanks for sharing it! I will add it to list of projects made with planck.js, if you don't mind?

Boyan-In-Christ commented 5 years ago

Dear Ali, thank you for your time on this. This parameters are very tricky...is there any best practices? I don't mind you use this code. But I used the code of computing convex Hull from others. Maybe it is better to change it to boxes instead of convex hulls. If you don't mind, I have one more question from my side. Seems the settings of parameters are quite tricky. If I change the density of the fixture or parameters of the simulation the result can be different. Intersection can still be there. Can this be solved? Is it because of collision computation? I saw a open source project

https://github.com/Sinova/Collisions/blob/master/README.md#anchor-demos

Can this be integrated into Planck.js? Thanks again and nice weekend!

shakiba commented 5 years ago

Actually my first suggestion (setting bullet: true for walls) was correct. I was setting that for other bodies by mistake. Please ignore my other suggestion.

Just change line 181 to var body = world.createDynamicBody({ bullet: true });.

shakiba commented 5 years ago

That's a very interesting open-source project, but I'm just maintaining planck.js right now.

Boyan-In-Christ commented 5 years ago

Thank you very much Ali. It indeed works. But actually these objects are not at a high speed, why they still can cause intersections? Also the computational cost of setting them to bullets is really high. When I set objects' bullet to true, the computation time seems exponentially increased according to the number of the bullet objects. How is the difficulty of solving this in planck.js? Many Thanks again!!

shakiba commented 5 years ago

You may also be able to fix it by adjusting planck.internal.Settings variables. But I don't know much more.

Also I recommend discussing this with Box2D community as well, Planck.js is based on Box2D.