Drawing Pulley Rope for Pulley joint

Dear friends,
I am able to make pulley joint between 2 bodies but don't know how to connect them with pulley rope or any simple line..
So pulley is working but line between them is not there just like invisible connection between 2 bodies..
Any body have any idea how to do that..
Thank you,
Viraj
If you ever called b2World::SetDebugDraw, use that b2Draw subclass directly to draw anything else you might need.

I have code like this all over:

Code: Select all

debugDraw.DrawSegment(ballBouncePoint, ballBouncePoint + ballBounceNormal, b2Color(0.0, 0.0, 1.0));


You should be able to get the world transforms of two bodies connected by joints and draw them, or you can also modify b2World::DrawJoint. It looks to me like the pulley joint is drawing 3 lines in that method, a line from the ground anchor to the body anchor, another line between the body anchors, and a 3rd line from the second body's anchor down to the last ground anchor. Can you post a screenshot of what you are seeing?
Thank you for the helpful reply to show lines at the time of debugging..
But that is handled by the debugdraw..
Actually i want to show lines in real like ropes and all that..i am able to make ropes independently but don't know how to take ropes so that ropes just replace those three debug draw lines and change their length accordingly when pulley is up or down..
I have attached the output file
To do ropes right, you'll need to deal with triangle meshes or else cover the ends of the ropes with pulleys and latches and whatnot and prevent the rope from bending.

Code: Select all

void renderSmoothedPath(TSSequence* sequence, vector<b2Vec2> &points, vector<b2Vec2>& normals, vector<GLuint> colors,
b2Float scale, // the entire bitmap can be scaled up and down
b2Float uScale, // how much of the texture to put between points
bool useUScaleAsFixed // is distance between points taken into consideration?
) {
    if(points.empty()) return;
   
   b2Float sequenceVInset = 0.5 * (sequence->height - sequence->frames[0]->height) / sequence->height;
   
   b2Float u = 0.0;
   
    b2Vec2 p1, p2 = points[0];
    GLuint c1, c2 = colors[0];
   
    b2Vec2 tl = sequence->height * 0.5 / scale * normals[0] + p2;
    b2Vec2 bl = sequence->height * -0.5 / scale * normals[0] + p2;
   
    DistortImage distortion(sequence, 1, 4, scale);
   TSRect uvRect(0.0,   sequenceVInset, 0.0, 1.0 - sequenceVInset);
   distortion.addStartVertices(tl, bl, &uvRect, colors[0]);
   
    b2Vec2 lastNormal(0.0, 0.0);
   
   for(unsigned int i = 1; i < points.size(); ++i) {
        c1 = c2;
        c2 = colors[i];
       
        p1 = p2;
      p2 = points[i];
       
      // find distance, orientation from previous point to current
      b2Vec2 xAxis = p2 - p1;
      b2Float length = xAxis.Normalize();
        b2Vec2 yAxis = normals[i];
       
        b2Vec2 nextNormal(0.0, 0.0);
        if(i < normals.size() - 1)
            nextNormal = normals[i + 1];
       
        float32 difference = 0.5 * b2Cross(yAxis, lastNormal) + 0.5 * b2Cross(nextNormal, yAxis);
        float32 similarity = b2Clamp(b2Dot(yAxis, lastNormal), 0.0, 1.0) *
                             b2Clamp(b2Dot(yAxis, nextNormal), 0.0, 1.0);
       
        if(!useUScaleAsFixed) {
            similarity = 1.0; 
            difference = 0.0;
        }
       
        GLuint topColor =    (GLuint)(b2Clamp((float32)sequence->height * difference * -0.2 + 1.0, 0.0, 1.0) * (float32)(c2 >> 24) * similarity) << 24 | (c2 & 0xFFFFFF);
        GLuint bottomColor = (GLuint)(b2Clamp((float32)sequence->height * difference *  0.2 + 1.0, 0.0, 1.0) * (float32)(c2 >> 24) * similarity) << 24 | (c2 & 0xFFFFFF);
       
        lastNormal = yAxis;
       
      b2Vec2 tr = sequence->height * 0.5 / scale * yAxis + p2;
      b2Vec2 br = sequence->height * -0.5 / scale * yAxis + p2;
      
      b2Float uIncrease = useUScaleAsFixed ? uScale : length * scale * uScale / sequence->width;
      
      uvRect = TSRect(u,               sequenceVInset,
                        u + uIncrease,1.0 - sequenceVInset);
      distortion.addMoreVertices(tl, bl, tr, br, &uvRect, topColor, bottomColor);
      
      u += uIncrease;
      
      tl = tr;
      bl = br;
   }
}
Sorry for late reply..
Thank you so much for the useful response..
hi,

you can get the position of the 2 bodies that are connected by a joint, and then draw line between them,
I am using Box2d for web, I think there is something similar in c++.

js code:

var x1 = jointRef.m_bodyA.GetPosition().x;
var y1 = jointRef.m_bodyA.GetPosition().y;

var x2 = jointRef.m_bodyB.GetPosition().x;
var y2 = jointRef.m_bodyB.GetPosition().y;

///draw line between the 2 bodies
ctx.strokeStyle = 'red';
ctx.beginPath();
ctx.lineWidth = 1;
ctx.moveTo(x1 * ScaleFactor, y1 * ScaleFactor);
ctx.lineTo(x2 * ScaleFactor, y2 * ScaleFactor);
ctx.closePath();
ctx.stroke();
Thank you so much for the useful response.
sbo365
It is great idea.
all the best for your work

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