7

I am a fairly competent programmer and I am not bad at math. I have been working on game physics simulations for around 2 years now.

In some books and tutorials that I have read, they introduce constraints. They calculate Jacobian matrices and use Baumgarte constants and all that complicated stuff.

Even when I try really hard and follow their logic, I always feel lost. It is not that the mathematics or algorithms of the technique are too complicated. It is just that when things like Jacobian matrices enter the picture, it is not intuitive anymore.

In my experience applying simple impulses to resolve collisions have given satisfactory results.

So my concern is: What am I losing by not utilizing constraints? What are the situations that can benefit from having constraints? Maybe some complicated objects with joints under inverse kinematics for example?

For example: http://twvideo01.ubm-us.net/o1/vault/gdc09/slides/04-GDC09_Catto_Erin_Solver.pdf and http://www.bulletphysics.com/ftp/pub/test/physics/papers/IterativeDynamics.pdf

tupcuhamdi
  • 118
  • 7

4 Answers4

4

The classical example for a constrained system is a pendulum. Assuming that the rope of the pendulum is under tension, the position of the bob of the pendulum is constrained to a circle. You can simulate this system by using Newton's laws, i.e., to simulate the force of gravity and the force the rope exerts on the bob. The problem with this approach is, however, that it is more complicated to simulate and the bob may leave the circle, due to round of errors during the simulation.

Both issues disappear when you use a constrained system. You do not have to deal with constraining forces and your simulation is guaranteed to always fulfill the constraints.

H. Rittich
  • 141
  • 2
2

In addition to the other good answers above, a common use of constraints for physics sim is conserving energy. As mentioned for constraint to circular motion of a pendulum, it's possible that rounding error can lead a solver off from the known correct configuration of the system. Similarly, if you know energy should be conserved, that constraint can ensure that you don't accidentally start getting magic energy appearing from nowhere. Inverse kinematics is another great example.

That said, it really depends on your use case, which you haven't elaborated. In my experience it's not usually an issue unless you have incredibly ill-conditioned systems that exacerbate small errors over time and feed them back to create larger errors (think chaos theory's butterfly effect). I doubt your average game mechanics bother with constraint-based solvers unless they need high precision of detailed kinematic interactions. These solvers are much more sophisticated to write and set up correctly, as you can see from all the math.

Bottom line: unless you suspect QWOP-level insanity may ensue from the setup of your mechanical systems, just add enough damping and small enough step sizes to your system and you'll probably be fine with regular solvers.

Bonus: A cheap hack if you are concerned about numerical stability in your case but not about scientific precision (this is gaming after all) is that small constraint/conservation corrections could be made periodically in such a way that isn't visually noticeable but keeps things in check if the error crosses some tolerance. Or add some hacky checks on calculations of forces and make sure they don't cross some extreme threshold through divisions by numbers close to zero (e.g. arising from poor precision in a subtraction in the denominator).

For context, I have a PhD in applied math and have developed my own solver / simulation package for scientists.

RHC
  • 121
  • 3
  • Thank you very much for the great answer. Yes, you are right assuming that I do not care much about numerical stability and conservation of energy. – tupcuhamdi Aug 11 '17 at 08:29
2

If you read in to Catto's notes that you posted the link to where he talks about using an iterative solver that works based on impulses, it is probably in practice not that much different than what you might be doing by, as you say "applying simple impulses to resolve collisions". Though the jacobian sounds like a complicated math idea, I reality it is basically just a bunch of vectors which 1) allow you to measure whether the constraint is met, and 2) give you the most efficient directions to apply impulses to keep the constraint satisfied. If you are already applying impulses to satisfy some constraint then you are probably doing something similar or even equivalent without being conscious of it.

When dealing with 3 dimensional rigid bodies and multidimensional constraints like hinges, the jacobian math provides a framework for expressing the constraint on the linear and angular velocities and deriving both linear and angular impulses to solve the constraints. It also provides a way where you could throw all of the constraints together (e.g. For a collection of bodies with many constraints) and solve them simultaneously. But in practice, and especially in games, we tend to use iterative solvers which are more or less a fancy way of doing a little impulse here, and a little there, and so on until everything is just right.

When you say that you "apply simple impulses to resolve collisions" perhaps that means you are constraining the position but not constraining the velocities? Such an approach can work in some contexts, but it implies that you only are applying impulses when the objects are penetrating? How about when an object is resting on a surface, under the influence of gravity? A constraint solver that works on a velocity or acceleration constraint will tell you the appropriate and correct forces at the contact points allowing you to do proper friction and such, whereas constraint only on a position level will result in constraint forces that oscillate and are much more unstable under changes in frame rate. E.g., if you have a position constraint where the impulse is proportional to the amount of penetration then equilibrium is only met at a specific spot where gravity and the constraint impulse cancel each other out. So that might be another critical difference between your approach and a constraint based approach, but it is hard to tell without knowing your algorithm and problem domain (2d particles in a meteor simulation in space? 3d simulation of a human body in a car crash simulation?)

uglycoyote
  • 146
  • 3
1

Im not expert on this topic, but i have read the presentation and skipped over the paper.

As Rittich said, you can apply the newtonian impuls calculations on a system of pendulums, which are paired by constrains to each other. In this case, you would need to calculate the force that applies to the inner pendulum, then the outer pendulum and the forces between them seperatly. This problem only gets stonger, the more bodys can interact with each other, so for example an n-elemental pendulum.

With the constraint-based solver, you got information for a body consisting of several constrains, that can be solved via numerical calcultaions. That means, instead of calculation the exact position every time a force works on the the contrains, you can use iterative calculations to approximate the results.

Approximation sound problematic, but in most cases it is not noticable after a few iterative steps.

PSquall
  • 1,332
  • 9
  • 15