Math question about the Constraint function C

I am attempting to write my own joint types, and have been studying the GDC slide shows from the downloads. I never took Mechanical Engineering and haven't done systems of constraints in calculus for awhile.

Reguarding slides 25 & 26, the Distance Constraint example in slide show GDC08_Catto_Erin_PhysicsTutorial, the constraint function turns out to be a scalar. If I wanted something more complicated like a constraint that keeps a point on a circle, would I need parametric equations? A constraint function for x and y with respect to time? I there somewhere to find more examples of derivations of different types of constraints? I looked at the implementations of some of the constraints in Box2d, but are a little difficult to read without more mathematical formatting.

Thanks.
A point to circle constraint shouldn't be too hard. The Jacobian will end up being the normal vector a the closest point on the circle. It is still a scalar constraints. Actually, point on circle has the same movement as the distance constraint.
So in the slides say C(x, y) = 0, zero is not a 0,0 vector but a scalar? And this scalar is mostly determined by dotting with the Jacobian with something to find the "0" value for the axis of the Jacobian?

And if I have a joint that is constrained on two different axies, I will need one constraint function for each axis? Each Jacobian?

Thanks.
In general a constraint is n-dimensional, where n >=1. A well formed constraint eliminates n degrees of freedom. So the distance constraint eliminates one dimension: motion along the line segment is zero while perpendicular motion and rotation are not constrained.

The Jacobian has n-rows and 3-columns (in 2D) per body, so a constraint between two bodies is n-by-6.

The distance constraint is not a pure dot product with the Jacobian because C = sqrt(x * x + y * y).
So the joint I was considering making is something like a dynamic joint, where the constraints change every frame. I already use the prismatic joint to get the effect I want, but it would be nice if I could alter the prismatic joint slightly to get the full effect I want.

I am using a prismatic joint as a "sticky" joint, where it constrains you to a surface. Since it has to move from fixture to fixture, there's not much sense in trying to make a static joint to a fixture or body. So I fix the prismatic joint to the world static body, and every frame set the axis that allows motion to the axis of the surface (right angle to normal). This works excellent for a platformer because I get to use the joints motor to create locomotion, and the player hugs the floor and doesn't fly off ramps going up. Also they do not slow down, because I am setting the motors speed, so the motion is very smooth.

A problem I have though is that the prismatic joint stops gravity, so there isn't any friction on the surface anymore, due to the lack of normal force. Right now I am computing what the force should be based on the prismatic joint's force, and subtracting that from the motors max force. Works out, but is a bit hairy.

What would be nice, is a sort of dynamic joint, where I set an origin, and an axis. Then the joint would have 2 motors, that act along the axis, and the skewed (right angle) axis. Also I could set limits, so the position can't go above or below a certain point. The the joint would have functions to set the origin and axis every frame, so it would change every frame.

With a joint like this, I could use it like a prismatic joint when a character is on the ground, and have an upper limit of zero (keeping the player on the ground) and no lower limit (player can still have gravity pull them towards the ground). Also this same joint could be used for locomotion in the air, by having no limits for the y axis, and have the motor set the locomotion speed in the x axis (based on the analog stick). Then every frame I would just have the origin move to the player's origin. Also this would be nice because I could set the max impulse on the motor if I wanted it to be more difficult to run up ramps, and it would all be wrapped up in the box2d solver.

Right now I compute the impulse needed to set the players speed in the air, out side of the simulation step, but that only gets on iteration of solving with respect to the other constraints per step.

Other uses for a joint like this would be in implementing a bead on a wire constraint, where all you would have to do every frame is find the closest point on the wire (a curve most likely), and set the axis, limits, and origin based on the curve (this is basically how I do the sticky joint, just find the closest ground or wall that should be stuck to with a sensor slightly bigger than the player fixture).

I found this to be a nice solution to get platformer locomotion, without having to fudge the physics in any way really.

Here's a screen shot of this system in action:
Image

Here are some screen shots of the original game that does not have a sticky joint:

Image
Image

And you can download and play the original game, if you have an analog joystick:

http://www-personal.engin.umd.umich.edu ... ungeon.zip
Try applying an impulse every frame that removes the velocity along the normal.

Code:
b2Vec2 v = body->GetLinearVelocity();
float32 vn = b2Dot(v, normal);
if (vn > 0.0f)
{
  float32 mass = body->GetMass();
  b2Vec2 impulse = -mass * vn * normal;
  b2Vec2 point = body->GetWorldCenter();
  body->ApplyLinearImpulse(impulse, point);
}
This won't work, because I also need the position correction. As the object moves along a changing normal surface, it will detach from it a little bit each frame, especially if the object goes around a 90 degree corner. So I calculate the closest point on the closest fixture, and that's my position correction distance. Also doing it each frame means it only gets a single iteration, where as the joint solvers iterate several times a frame. (maybe that's ok because its the last one since you do it outside the simulation).

But maybe I can just apply an impulse that will also put the object back to the wall, and not worry about getting multiple iterations from the solver.

EDIT: I don't think the above will work either... The code looks roughly like this in the step function:


Code:

Test::Step(settings);

b2Vec2 normal = GetNormal(); // find normal of closest fixture
float32 distance = GetDistance(); // find distance between closest points

// suggested code
float32 mass = body->GetMass();
b2Vec2 point = body->GetWorldCenter();
b2Vec2 impulse;
b2Vec2 v = body->GetLinearVelocity();
float32 vn = b2Dot(v, normal);
if (vn > 0.0f)
{
  impulse = -mass * vn * normal;
  body->ApplyLinearImpulse(impulse, point);
}

// plus code to correct position
impulse = -mass * distance * normal;
body->ApplyLinearImpulse(impulse, point);



I think what happens here is I give it the impulse to get it back to the wall, but gravity is applied in the next step, canceling out some of the impulse, not allowing it to get back to the wall. When I used the joint, this impulse would be applied iteratively and approach a solution closer to what is desired.
You can adjust vn by adding a fraction of the position error. This will drive the object to the surface.

I don't know if this will give a perfect simulation, but a joint may not either.
Here's the vn with position correction (actually i didn't do a fraction of the distance, i did the whole thing)

Image

Here's using the prismatic joint

Image

The prismatic joint works very well even at pretty high speeds. It also has the nice feature that setting the motor allows me to just set how fast I want the character to move, and he accelerates smoothly given the motor impulse. Only problem with it is that when the ball is on top of the platform, the prismatic joint cancels out gravity and the friction between the ball and platform turns out to be zero. Also the balls rotation is fixed, so if it attaches to a dynamic object, it fixes that object's rotation too. I get around this with a little hack that always attaches the ball to a dummy static object, and ignores the relative velocity of the object the ball is attached to. It works out because I'm setting a new position that the ball is attached to every frame which compensates for the fact that it is actually attached to a static object.

I think I'm just going to have to play with the prismatic joint code, and remind myself how to work with systems of constraints. I'm not exactly sure what a new joint should look like, but I think modifying the prismatic joint to ignore the constrained axis in only one direction, and allowing relative rotation to change would do the trick.

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