melax / sandbox

sandbox for various 3d math, geometry, graphics and physics code
MIT License
231 stars 48 forks source link

Deriviation angular constraint #1

Closed tvandenzegel closed 9 years ago

tvandenzegel commented 9 years ago

Wow, very very good example code!

Learning a lot! A like the minimalistic approach and also that it just compiles right of the box!! Really cool!

But I'm a little bit confused. In physics.h in the function ConstrainAngularRangeW.

What is exactly the "position constraint" for the angular limits? Should it not be "current_angle - max_angle" but in the code I see "2 x (sin(current_angle/2) - sin(max_angle/2))" Is it possible to explain or where the derivation comes from? :) It really frustrates me! I mean, this differs from the limits derivation of the hinge joint in the document http://danielchappuis.ch/download/ConstraintsDerivationRigidBody3D.pdf

And

if(jmax.x==jmin.x) { Angulars_out.push_back(LimitAngular(rb0,rb1, qxdir(jf1), 2 * (-s.x+sin(jmin.x/2.0f)) /physics_deltaT));
}

differs from

if(jmax.y==jmin.y) { Angulars_out.push_back(LimitAngular(rb0,rb1,qydir(jf1),physics_biasfactorjoint * 2 * (-s.y+jmin.y) /physics_deltaT)); // btw '2' is because of quat angle is t/2 }

melax commented 9 years ago

yes, you are correct the necessary spin or rotational adjustment will depend directly on the angle. Admittedly, this routine needs some fixing.
To explain why the code still works is that it is is often working with ranges near 0 in which case sin(x) is close enough to x.
I didn't notice this omission earlier, but yes, the constraint for when the x axis is locked (xmin=xmax) really should have the biasfactor multiplied into it - this is the typical method to soften it a bit to prevent jitter. Also, the routine also looks like it assumes the last twist axis (z) is locked and zmin==zmax==0. Oops.

This particular routine was quickly written for ragdolls where you typically load a file and you might get hinge type joints (elbow, knees), or possibly a bit of lateral swing in other joints (hips, shoulders). So it wasn't tested in with a sufficient variety of use cases.

The ConstrainConeAngle shows an clearer way of generating an angular constraint based on a simple specification for range limit that is easy to visualize and understand.
However, even the implementation could be better. Ideally we want to avoid code that has to renormalize vectors that have the potential to be [0,0,0], and also avoid calling acos or asin.

tvandenzegel commented 9 years ago

Thank you very much for your reply!

Now everything makes sense. :)

I was thinking in that direction, that sin(x) was an approximation for x. But now I know that was not the purpose.