- Home /
Inconsistent gravity/random forces/solver problem? (forum crosspost)
Maybe I'm not considering some fundamental forces, or even making stupid errors and assumption in the simplest of equations, or this is indeed an floating point precession error/physics solver error. But here are the steps to reproduce the problem (using Unity 5.3.4f1):
Make a default 2D project with not additional packages.
Make a scene with a BoxCollider2D for a floor and two identical objects with BoxCollider2D and RigidBody2D. Leave the first rigidbody as is, and set gravity scale to 0 on the second rigidbody.
Add this script from this post on any object: http://forum.unity3d.com/threads/inconsistent-gravity-random-forces-solver-problem.400978/ (code tag is messing up)
Populate the rb1 and rb2 fields with the two created rigidbodies.
Play the scene and test the results by setting 'test' to true when BOTH of the objects are lying STILL on the ground. Do it a few times. Look how both of the objects get higher every time you set 'test' to true. In ideal physics they should reach the same height every time, or at least be within some boundaries, not higher EVERY time. This is minor gripe, I can live with it, since the difference is so small.
Now set the 'scale' value to 0.5, for example, and do the same test as above. Correct me if I'm wrong, but since the applied force and the gravity is scaled by 0.5 for the second rigidbody it should have exactly half the acceleration, hence half traveled speed, because the force was and impulse. But if you compare maximum heights reached (scaling the second one up by the factor given) they don't add up. Even worse - the smaller the scale, the bigger the gap.
What am I missing here?
I don't quite understand the test conditions, but gravity is applied to rigidbodies by the PhysX engine automatically (assu$$anonymous$$g that the "Use Gravity" checkbox is ticked, which it is by default), so by also trying to account for gravity manually in your code (with an additional scale factor), you're creating a strange situation. And the reason why they're getting higher every time is because you're never resetting the initial conditions.
What is the actual problem you're trying to solve?
RigidBody2D does not have a UseGravity property, but has a GravityScale property, so by setting it to 0, it should effectively behave the same as setting UseGravity to false. So in this case I'm manually applying gravity, that the engine does by default.
The problem is the huge difference between simulations.
One problem I see with the experiment, regardless of its outcome, is that Rigidbody2D.AddForce() does not appear to be an adequate testing measure for reproducing the gravitational force applied to an object.
The reason for this is that Force$$anonymous$$ode2D does not include options to ignore mass, which gravity most definitely (effectively) does. The problem would be extremely obvious with extremely high-mass objects.
In order to create a proper simulation, your gravitational influence should look more like:
rb2.velocity += trueForce * scale;
and
rb2.velocity += Physics2D.gravity * scale * Time.fixedDeltaTime;
As far as any resulting imprecision beyond that, I can't say for sure. But first, let's ensure that unexpected physical forces aren't involving themselves in the test.
Your answer
Follow this Question
Related Questions
increasing knockback of a rigidbody as received damage increases (like super smash) 1 Answer
How to make physics relative to moving parent 1 Answer
Rigidbody2D: Follow other Rigidbody2D strictly 1 Answer
Recalculating collisions after Physics2D.IgnoreLayerCollision()? 1 Answer
Child GameObject can't use MovePosition on parent's Rigidbody2D? 3 Answers