Hello, i already posted on the box2d forum and maybe you have an idea. Here is the situation :
In the image there is a b2DistanceJoint (the blue line) and a b2MouseJoint dragging the black smiling ball.
The joint (blue line) is created as follows:
b2DistanceJointDef jointDef; jointDef.Initialize( bodyA, bodyB, bodyA->GetWorldCenter(), bodyB->GetWorldCenter() ); jointDef.length = 1.2; jointDef.dampingRatio = 0.5; jointDef.frequencyHz = 0.9; jointDef.collideConnected = false; grumoJoint = (b2DistanceJoint*)world->CreateJoint(&jointDef);
It is springy and has the length of 1.2. But when i’m dragging with a b2MouseJoint the black ball, there is no length limit on the b2DistanceJoint! The blue line can go where I drag the ball. How to limit the distance that the ball can be dragged?
Is there an additional joint i can add so to limit tha distance to where i can drag the ball in all directions?
Thanks:)
I did solved the problem by adding an additional rope joint when creating the mouse joint (for dragging):
b2MouseJointDef jointDef; jointDef.bodyA = groundBody; jointDef.bodyB = grumo->getBody(); jointDef.target = grumo->getBody()->GetWorldCenter(); jointDef.maxForce = 2000; grumoMouseJoint = (b2MouseJoint*)world->CreateJoint(&jointDef);
//Create rope joint b2RopeJointDef rDef; rDef.maxLength = 1.15f; rDef.localAnchorA = rDef.localAnchorB = b2Vec2_zero; rDef.bodyA = grumo->getBody(); rDef.bodyB = lastBodyJointed; grumoRopeJoint = (b2RopeJoint*)world->CreateJoint(&rDef);
Nice solution…thanks for sharing
And in that first image i can see that predictive trajectory line…Can you share some knowledge about how you implemented that?
Yes of coure. I did followed this tutorial:
[http://www.iforce2d.net/b2dtut/projected-trajectory]
And used their function: b2Vec2 getTrajectoryPoint( b2Vec2& startingPosition, b2Vec2& startingVelocity, float n )