0
votes

I'm currently working on a 2D physics abstraction layer that can use Box2D or Chipmunk2D as physics engine. I initially used Chipmunk2D and am now implementing Box2D. One of the issues I ran into was the maximum speed of Box2D (as defined by b2_maxTranslation). To solve this issue I've decided to scale the world so it fits better with Box2D units.

Currently I'm scaling down with a constant factor:

  • positions (including polygon fixture definitions and circle radius)
  • forces (before calling b2Body::ApplyForce)
  • impulses (before calling b2Body::ApplyLinearImpulse)

As I'm using densities to define the fixtures, this would also scale down the body masses.

What I've noticed is, this does not work. Whenever I apply a force the body moves much faster than when the scale is high.

Is there something else I need to scale to make the bodies update correctly or should I scale the impulses and forces in some other way?

2

2 Answers

0
votes

I wouldn't scale the world to avoid hitting the b2_maxTranslation setting. I'd keep all the physics units normalized to MKS units and either raise the b2_maxTranslation setting or raise the frequency of the step function.

For instance, if I wanted to double the max speed, I'd either double the compiled in b2_maxTranslation value (from 2.0f to 4.0f), or I'd run twice as many step methods and have each simulate half of the time period I'd otherwise have it simulate. With all the units normalized to MKS, Box2D will take those units directly and I'd guess Chipmunk would too.

For an engine that didn't use values scaled to MKS units however, I'd scale the values to that engine's units from MKS.

Hope this helps.

0
votes

Given a scale X that scales the geometry, I ended up doing the following (I didn't bother scaling the damping effect):

Velocity (SetLinearVelocity) - Linear

Force (ApplyForce) - Cubic

Position (any fixture def CreateFixture) - Linear

Mass (GetMass) - Square

Gravity (SetGravity) - Linear

Impulse resolving (PostSolve WorldManifold.normal) - Cubic inverse

Where

  • linear = X * designed factor
  • square = X * X * designed factor
  • cubic = X * X * X * designed factor

I did several tests with these factors and they seemed to work fine in preserving angular velocities as well as linear movement (I did try several others that failed just this). See https://bitbucket.org/Kipt88/polymorph/src/b4d29e2434a2ed1eda2183f9e16f4782c40ee026/modules/physics2d/files/source/box2d/?at=master for reference (source code no longer maintained).