Callback in prismatic joint to define rail behavior

I have been experimenting with changing the properties of a prismatic joint every time step to get a curved rail behavior. I figured there are too many kinds of curves to have a definition for them built into Box2D, so I gave the prismatic joint a callback class to allow the curve to be defined elsewhere. It works ok so I will share the code here.

The prismatic joint asks the callback for the info needed to define the current constraints, given the current position of anchor B relative to bodyA. If no callback is set the joint functions as normal. For the most part it seems ok but the limits can be a bit jittery, and it seems quite easy for two bodies on the rail to overlap, as if the joint constraint has a higher priority than the overlap correction constraint.

I also experimented with disabling the rotation constraint of the prismatic joint, by pretending that the rotation never needs correcting as suggested by Boris here
viewtopic.php?f=4&t=1267
This also seems to work well enough, until you put two of these joints between the same two bodies and then all hell breaks loose. It also bounces too much at the limits as well. I'm putting these issues down to the way the rotation correction is fudged.

Source: http://www.iforce2d.net/rail-callback-src.zip
YouTube video: http://youtu.be/hTCIRk9vcGA

The jittery movement at the limits could be a consequence of using an approximation for the translation, because it happens with the sine and spline curves but not with the circle which returns an exact translation...
Do you know if the line joint (from your first linked topic) can be extended to be connected line-segment-paths, which would satisfy a rail constraint?

Judging from the video though, it looks like you already have accomplished exactly that.
afaik the line joint referred to in that thread has since become the wheel joint. If you don't need curves, perhaps the spring part of the wheel joint could be disabled to get a straight 'rail' joint.
I wanted to have the rotation constrained so that the body stays pointing along the track, so I went for the prismatic joint instead (and also because it has a motor).

If I understand you correctly, you're talking about having a bunch of straight lines?
The way the example in the video works is by brute force finding the closest point on the curve to the current position, every time step, so in effect the body is not really attached to any particular part of the track at all. See 4:45 and 7:15 in the video for examples of the 'closest point on curve' suddenly changing. You could do the same with a bunch of straight lines - just find the closest point among all lines each time step. You could use some more sensible heuristics to improve the behaviour, eg. if the body was on segment 4 last time, it should only look at segments 3,4,5 for the closest point next time.
That sounds like a pretty good way to do it. I'm curious: are you making a game? I see you make these fantastic contributions of code and tutorials and I always think if there was a game that uses all of this it would be amazing. You should think about it. 😉
So I checked very carefully. The advantage of using a joint for this kind of operation is that if it fights a contact constraint, the contacts will be interleaved with the joint constraints (position/velocity iterations passed into b2World::Step). That's interesting, I did not know that until now. I've been using small timesteps and utilizing things like forces to sidestep the issue of not actually implementing this at the joint level...
@dc443 not now, still working on my editor. One day I would like to make a game of course - if I could draw to save my life I probably already would be 😀

@pTymN yeah, I've found that most joints can be modified or even created and destroyed as frequently every single step, without any adverse consequences. If there is already a joint that's close to what you're trying to do it can make a good starting point for a slightly different type of 'controller'. In the case of a rail joint, I figured a rail is like a prismatic joint that changes characteristics every time step.
Is the jump that I see in the beginning of the video real framerate hitch because of who knows what, or is the implementation "sticky" right now? I didn't see that happen again like I saw in the first minute.
I think that was just me pressing 'R' to restart the test.

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