question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Collision response is different from Box2D

See original GitHub issue

I work on a client/server game. The server uses the original Box2D implementation, and the client uses planck.js. I had some major differences in collision response between Box2D and planck. the test involves a (circle) bullet bouncing on a (circle) static body.

I identified the issue in the getWorldManifold function. I’ve only worked on circles for now, but the implementation in planck is different from the one in Box2D

    var pointA = Transform.mulVec2(xfA, this.localPoint);
    var pointB = Transform.mulVec2(xfB, this.points[0].localPoint);
    var dist = Vec2.sub(pointB, pointA);
    if (Vec2.lengthSquared(dist) > Math.EPSILON * Math.EPSILON) {
      normal.set(dist);
      normal.normalize();
    }
    points[0] = Vec2.mid(pointA, pointB);
    separations[0] = -radiusB - radiusA;

First Math.EPSILON does not exists and the right value should be Number.EPSILON (with a needed polyfill for Internet Explorer) I had not seen this so it’s probably valid, I was testing outside of planck.

The end of the code should also be something like this:

            var cA = pointA.clone().addMul(radiusA, normal);
            var cB = pointB.clone().addMul(-radiusB, normal);
            points[0] = Vec2.mid(cA, cB);
            separations[0] = Vec2.dot(Vec2.sub(cB, cA), normal);

Replacing the code got me the expected result (similar collision response as Box2D)

Is there any reason the implementation in planck looks so wrong? Could it be changed?

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
Kal-Torakcommented, Jul 12, 2019

Oh, very nice, I’ll idle there then 👍

0reactions
shakibacommented, Jul 12, 2019

Merged, thank you @FlorentMasson !

Read more comments on GitHub >

github_iconTop Results From Across the Web

Collision Module - Box2D
The Box2D algorithm for dealing with ghost collisions only supports one-sided collision. The front face is to the right when looking from the...
Read more >
Anatomy of a collision - Box2D tutorials - iforce2d
In Box2D it's common to think of bodies colliding with each other, but it's really the fixtures which are used to detect when...
Read more >
How do physics engines like Box2D detect and respond to ...
2D collisions with non-rectangular shapes are often detected using the Separating Axis Theorem (tutorial here). Look it up, there are a lot ...
Read more >
5.12: Collision Events in Box2D - The Nature of Code - YouTube
This video demonstrates how to listen for collisions and trigger events at the moment of collision in Box2D.
Read more >
Does it make sense to use Box2D only for collision detection?
Yes, use Box2D. Box2D has the best collision detection system I've used, compared to Chipmunk and Bullet ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found