Speed Tests

Raigan, you might like to know that Box2D uses a position solver after the velocity solver. This provides the stability you speak of (long chains, etc). It is more complex than simple distance constraints, but it has many of the same desirable properties.
I did see that -- the main thing keeping me away from Box2D is my inability to derive my own constraint functions.. I'm terrible with vector calculus! Right now with our solver I can write any function of particle positions, feed it to LiveMath! http://www.livemath.com/, and have it return the partial derivatives of C wrt the particle positions -- exactly what I need.

Is there some similar method to automatically calculate Jacobians for impulses? LiveMath! will work on any scalar function (I just treat each vector component as a separate scalar); it's a huge mess to rearrange back into vector form, but it works.

We have all sorts of weird constraints currently, such as an "angle constraint" (not sure what to call it -- given a point pA and axis d on body A, a point pB on body B, it constrains the angle of (pB-pA) relative to d), and most importantly a "rope" constraint (similar to your pulley joint, but with multiple nodes which change dynamically each frame).

Our solver isn't the best, but the main thing is that we're very comfortable working with it.

Being 10-20x slower than Box2D isn't so bad, is it? I'm hoping that once we get more optimized primitives working (i.e circles and capsules.. so that we're not always doing mesh-vs-mesh) we'll close the gap. We're also using doubles for everything, which probably doesn't help. Plus, we're not programming geniuses 😉

Everything is (over-)abstracted; each constraint object is assembled using "configuration of degrees of freedom" objects, which take a body and parameters (localspace axes, positions, etc) and return a scalar measurement of state. Bodies are hidden behind proxies, so that the solver doesn't have to care if the body is a 3DOF rigid body or a 2DOF particle. This way you can (for instance) constrain the orientation of one body relative to the position of a point on another body measured along an axis on a third body, just by plugging various configurations together. Constraints also take a "target" value (C = state - target), which is either fixed (point, min, max, interval) or dynamic (for motors -- has a mass just like the bodies and is "pushed around" by the solver). So instead of a single number for "target distance", we have an object.. sigh.

At this point we're giving up on this generic interface because it's a huge pig! Solving a single constraint involves quite a bit of indirection and delegation which will (we hope) be avoidable during optimization by making hand-coded single-function constraints for the most frequently used types.

We have had "should we just use Box2D?" meetings/arguments about weekly this month, however currently the dealbreaker is penetration. Does v2 guarantee that the vertices of core shapes won't ever penetrate or sweep through each other? This event can totally ruin our rope sim, and so far we haven't found a work-around to "untangle" it when this happens..
raigan wrote:
Is there some similar method to automatically calculate Jacobians for impulses? LiveMath! will work on any scalar function (I just treat each vector component as a separate scalar); it's a huge mess to rearrange back into vector form, but it works.


It takes me about 5 minutes to derive the Jacobian for a constraint but then about 2-3 hours to implement the joint. I can do the 5 minute part if you would do the 2-3 hours part. 🙂

raigan wrote:
Does v2 guarantee that the vertices of core shapes won't ever penetrate or sweep through each other? This event can totally ruin our rope sim, and so far we haven't found a work-around to "untangle" it when this happens..


I think Box2D would work fine for this. That said, you should test it yourself. You can start with the chain example in the testbed.
I appreciate the offer a lot! For now I think we'll keep plugging away at our own thing though.

I've finally got a version of v2 building thanks to VS, so no doubt there will be some tinkering here and there..

(sorry for hijacking the thread, if anyone does get the speed test running in the AS/C# ports, please post the timing info!)
Erin - are there any good methods that you could recommend to measure cache miss rates? What you said before has prompted me to actually try to get a handle on Java's behavior on this, and it's pretty baffling (for instance, using interleaved arrays to store particle locations and velocities for integration would seem like it should improve behavior over storing separate x, y, vx,and vy arrays, since it puts all the data that's needed for a step right in order in a single array, but in fact, it degrades it by a few percent unless you do a lot of careful reorganization of the access order and computation...). I can't find much (if anything) online about Java's behavior here, so I'm hoping to figure it out and maybe even write up a bit about it so that anyone else needing to squeeze the language for all it's worth will have a bit more to go off of.

Also, is there a real basic example that will demonstrate a big timing difference between a cache friendly program and a cache unfriendly one (ideally an example where both versions do "the same thing" algorithmically, i.e. roughly the same number of ops)? This seems like one of the most important optimizations to shoot for, far more important than the few percent here and there that I can squeeze out by going through the usual local optimization routine (not to sniff at that stuff - replacing temporary vector creations with inlined float operations, which go on the stack instead of the heap in Java, gives up to a 3:1 improvement, even in the latest JVMs), and I'm discovering that I really have no idea how it works in Java, so I'd love to start from a simple case where I know exactly what's going on, at least in C.
There are some counter on Intel chips to measure cache misses and they might be exposed in VTune (don't recall exactly). My experience with optimization lately is that mosts algorithms that have random memory access are cache limited.

Here's a simple example. Suppose you have a triangula polyhedron with a vertex array and a face array. The faces contain the three vertex indices. So now compute the total area by streaming through the face array. The vertices are shared, so there will be random access to the vertex array. Now to see the type of effect I am talking about, split the vertex array into 3 arrays of x's, y's, and z's. This should make the algorithm much slower because there will be more cache misses.

Original thread: https://box2d.org/forum/viewtopic.php?t=979