Advertisement

Question on finding contact point

Started by September 10, 2019 12:36 PM
4 comments, last by Dirk Gregorius 5 years, 1 month ago

I am developing a simply 2d physics engine and I' ve read the source code of box2d and matter.js. I found some insteresting thing on finding contact point:

Box2d use indicent edge, reference edge and clip to determine a contact point. It's very complex method and hard to understand.

However, matter.js use a very simply method to do the same thing. just find the vertex that contained by opposite polygon with hill-climbing:


var verticesB = SAT._findSupports(bodyA, bodyB, collision.normal),
            supports = [];

        // find the supports from bodyB that are inside bodyA
        if (Vertices.contains(bodyA.vertices, verticesB[0]))
            supports.push(verticesB[0]);

        if (Vertices.contains(bodyA.vertices, verticesB[1]))
            supports.push(verticesB[1]);

        // find the supports from bodyA that are inside bodyB
        if (supports.length < 2) {
            var verticesA = SAT._findSupports(bodyB, bodyA, Vector.neg(collision.normal));
                
            if (Vertices.contains(bodyB.vertices, verticesA[0]))
                supports.push(verticesA[0]);

            if (supports.length < 2 && Vertices.contains(bodyB.vertices, verticesA[1]))
                supports.push(verticesA[1]);
        }

and it works well in demo.

My question is: why does box2d use such complex method? It's clear that matter.js's method is better. Or there are some potential shortcomings in matter.js's method?

 

A student 

I cannot say what matter.js is doing, but if you only keep vertices which are contained inside the other polygon as contact points you will have a lot of trouble. E.g. see the following picture. The shapes are clearly touching, but none of the vertices of either shape is contained in the other. 

image.thumb.png.55373d8cf6444952ab5916c18bc7b037.png

I also somewhat disagree that Box2D method is *very* complicated. I would say it is reasonable simple. In particular in 2D. I have pointed out this many times here. If you want to learn about physics programming stick with Box2D and Box2D Lite in the beginning. There are also many resources from the GDC Physics Tutorial which explain every aspect of a physics engine on the Box2D website. These are presentations from experts in this field who do this for a living and have shipped games with this. This is the kind of resource I would be looking for and want to learn from. Box2D has been used by many shipped games including Angry Bird. The stable stacking which is part of the game mechanics is due to its high quality contact point creation and contact block solver. I would not call a method better because you find it easier to understand. :)

Erin gave Box2D Lite its own Github website. You can find it here:

https://github.com/erincatto/box2d-lite

Presentations from GDC Physics Tutorial can be found here:

https://box2d.org/downloads/

http://essentialmath.com/tutorial.htm

 

HTH,

-Dirk

 

Advertisement
9 minutes ago, Dirk Gregorius said:

I cannot say what matter.js is doing, but if you only keep vertices which are contained inside the other polygon as contact points you will have a lot of trouble. E.g. see the following picture. The shapes are clearly touching, but none of the vertices of either shape is contained in the other. 

image.thumb.png.55373d8cf6444952ab5916c18bc7b037.png

I also somewhat disagree that Box2D method is *very* complicated. I would say it is reasonable simple. In particular in 2D. I have pointed out this many times here. If you want to learn about physics programming stick with Box2D and Box2D Lite in the beginning. There are also many resources from the GDC Physics Tutorial which explain every aspect of a physics engine on the Box2D website. These are presentations from experts in this field who do this for a living and have shipped games with this. This is the kind of resource I would be looking for and want to learn from. Box2D has been used by many shipped games including Angry Bird. The stable stacking which is part of the game mechanics is due to its high quality contact point creation and contact block solver. I would not call a method better because you find it easier to understand. :)

Erin gave Box2D Lite its own Github website. You can find it here:

https://github.com/erincatto/box2d-lite

Presentations from GDC Physics Tutorial can be found here:

https://box2d.org/downloads/

http://essentialmath.com/tutorial.htm

 

HTH,

-Dirk

 

thanks for your reply,very helpful.

A student 

"

Quote

My question is: why does box2d use such complex method? It's clear that matter.js'shod is better. Or there are some potential shortcomings in matter.js'shod?

The use of SAT or GJK algorithms for collision detection is employed for detection among triangulated meshes. For a AABB or OBB this might seam overkill. However, for more generic solutions  SAT or GJK algorithms are employed.
In fact, some physics engines do low level colision detection with AABB or OBB and do high level colision detection with SAT or GJK. However, due to the time it takes to compute for multiple meshes unless there is a technique for reducing the amount of inter-triangle colision tests this is slow to compute at interactive rates.

I think this is not correct if you mean general triangle meshes. Both SAT and GJK only work for collision/distance between *convex* polyhedra. Also, you use SAT for nearly all boolean overlap tests I am aware of. At least in a specialized form. It is the basic idea for these kind of tests. For distance between two AABB you can write a simpler and faster specialization, but for OBB vs AABB/OBB I would already use GJK.

This topic is closed to new replies.

Advertisement