Prismatic joint between two arbitrarily placed bodies

I'm working on a project where I need to connect bodies at arbitrary locations with a prismatic joint. I'd do it with a distance joint, but the length will adjust over time. I'm guessing it would be easier to occasionally myPrismaticJoint.SetLimits(newLower,newUpper) than destroy and recreate large numbers of distance joints repeatedly.

If I understand the prismatic joint correctly, creating one looks something like this:
Image

What I am attempting is more like this:
Image

Getting the axis is the question. Straight horizontal/vertical axes are easy:
Image

The slope for arbitrary points is also easy. I'm guessing it needs to be reduced to a unit circle (?). This function seems to be correct:
Code:
public function getAxis(vecA:b2Vec2, vecB:b2Vec2):b2Vec2 {
    var  rise:Number = vecB.y - vecA.y;
    var   run:Number = vecB.x - vecA.x;
    var slope:Number = rise/run;
    var unitY:Number = Math.sin(Math.atan(slope));
    var unitX:Number = Math.cos(Math.atan(slope));
    return new b2Vec2(unitX,unitY);
}

The result is kind of jumpy, though restitution is low. What should always be a straight joint sometimes appears "bent". Occasionally objects will fly completely apart in all directions (from my experience with C, this phenomenon has been NaN issues, though this is all in AS3).

Just wondering if I have the right understanding of the above, which I will readily admit could be completely wrong. Did I miss some in-built function that's going to make my life easy street? 🙂

Working with Boris' v2.02 port/Flash CS4. Much thanks to Erin for Box2d and Boris for the AS3 port.
To compute the axis just compute the difference between the two points and normalize the vector:
Code:
b2Vec2 axis = point2 - point1;
axis.Normalize();
lol! Thanks, I'll give that a try. 🙂
As this is flash, it's unfortunately a bit uglier:

Code:
var axis:b2V2c =  b2Math.SubtractVV(point2, point1);
axis.Normalize();
Thanks for the additional tip Boris. I got my first attempt to this point:
Image

Taking this further I want to attach the ends of the prismatic joint each to a base with a revolute joint. The circles A & B (below) do not rotate, but the boxes attached to them do. I think this is how it's working:
Image

Here's the view "from the side":
Image

Problem is... it doesn't quite work (surprise). Here's SetAsBox()
Image

Here's trying to get SetAsOrientedBox() as I want to attach the ends (you can't see the ends as they've flown all about the stage!):
Image

I'm not getting something about this joint. If anyone would like to take a stab at the code, here it is. This example tries to implement the above diagram.
😢 see Prismatic Joints Gone Awry
From the revised Forum FAQ:

If you want help with Box2D, please make a post like the following: http://box2d.org/forum/viewtopic.php?f=4&t=4872
Notice that the poster clearly spelled out the problem and included code for the C++ testbed. This is the surest way that you will get effective help.

This also applies to users of other versions of Box2D, such as the Flash port. You can Visual Studio Express for free and you only have to write a little bit of C++ code in the testbed.
It would very likely take me far too long to write successful C++ code.

Here is the code as a Flash PhysTest module (includes all necessary libraries).

What I don't understand in this example is that I end up with what seems to be three joint segments plus two disembodied polygons (end body and slider body). Why do the two polygons fly apart from their positions? Why isn't the slider (polygon associated with "bodyB") sliding on the main (what appears to be middle of the three part) segment? AFAIK all the points are correctly placed.

Also when I used circles instead of polygons it mostly worked. This means the circles stayed at the same angle to each other and I could increase and decrease the distance without the structure flying apart. Only the minimum length was set at the initial distance of the two bodies. That seemed non-adjustable. I'm not sure if it is fixed as body size is on creation, but the fact you can specify a lowerTranslation value seems to suggest otherwise.
I don't think it would take you long to write the C++ code. Also you should post the example code in text, just like the post I linked. Why zip it up when it is just a page of code?

I modified your code to work:
Code:
b2Vec2 positionA(12.0f, 18.0f);
b2Vec2 positionB(13.5f, 16.0f);

b2BodyDef bodyDefA;
bodyDefA.position = positionA;
bodyDefA.fixedRotation = true;     // don't allow this body to rotate
bodyDefA.type = b2_staticBody;

b2CircleShape circleA;
circleA.m_radius = 2.5f;
b2Body* bodyA = m_world->CreateBody(&bodyDefA);
bodyA->CreateFixture(&circleA, 0.0f);

// Circle "B" set higher
b2BodyDef bodyDefB;
bodyDefB.position = positionB;
bodyDefB.fixedRotation = true;     // don't allow this body to rotate
bodyDefB.type = b2_dynamicBody;

b2CircleShape circleB;
circleB.m_radius = 2.5f;

b2Body* bodyB = m_world->CreateBody(&bodyDefB);
bodyB->CreateFixture(&circleB, 1.0f);

float32 boxAngle = b2Atan2(positionB.y - positionA.y, positionB.x - positionA.x);

// create pivotA to attach to the first body

b2BodyDef pivotDefA;
pivotDefA.position = positionA;
pivotDefA.type = b2_dynamicBody;
b2Body* pivotA = m_world->CreateBody(&pivotDefA);

b2PolygonShape polygonA;
polygonA.SetAsBox(0.3f, 2.5f, b2Vec2_zero, boxAngle);
pivotA->CreateFixture(&polygonA, 1.0f);

// create the revolute joint between bodyA and pivotA
b2RevoluteJointDef revDefA;
revDefA.Initialize(bodyA, pivotA, positionA);
m_world->CreateJoint(&revDefA);

// create pivotB to attach to the second body
b2BodyDef pivotDefB;
pivotDefB.position = positionB;
pivotDefB.type = b2_dynamicBody;
b2Body* pivotB = m_world->CreateBody(&pivotDefB);

b2PolygonShape polygonB;
polygonB.SetAsBox(0.3f, 2.5f, b2Vec2_zero, boxAngle);
pivotB->CreateFixture(&polygonB, 1.0f);

// create the revolute joint between bodyB and pivotB
b2RevoluteJointDef revDefB;
revDefB.Initialize(bodyB, pivotB, positionB);
m_world->CreateJoint(&revDefB);

// create a prismatic joint between the two pivots
b2PrismaticJointDef prismDef;

b2Vec2 axis = positionB - positionA;
axis.Normalize();
prismDef.Initialize(pivotA, pivotB, positionA, axis);
prismDef.enableLimit = true;
prismDef.lowerTranslation = 0.0f;
prismDef.upperTranslation = 5.5f;

m_world->CreateJoint(&prismDef);


You had several errors. The worst was setting the polygon position to the body world positions. You didn't do this for the circles, so why do it for the boxes? Anyhow, shapes are defined relative to the parent body. Shapes have no transform of their own.

Also, you are creating these light weight intermediate bodies that reduce stability. See the limitations section of the manual: http://box2d.org/manual.html#_Toc258082979

Finally, it is really confusing what you are trying to achieve. Do you really want your circles not to rotate? Did you see the post by pTymN where he was able to dynamically change the length of a distance joint? You can do this if you use softness as shown in the Web example. Perhaps you need a new joint type.
I just want to say immediately -- thank you! -- for taking a look at it.
I'm going to dig through it for a bit, then try and write a better reply.

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