MouseJoint:
The role is to achieve the effect of dragging the mouse.
The usage is as follows:
[Cpp]
Void HelloWorld: ccTouchesBegan (CCSet * touches, CCEvent * pEvent)
{
// CCLOG ("touchBegan ");
If (mouseJoint! = NULL)
Return;
CCLOG ("touchBegan ");
CCSetIterator it;
CCTouch * touch;
For (it = touches-> begin (); it! = Touches-> end (); it ++)
{
Touch = (CCTouch *) (* it );
If (! Touch)
Break;
CCPoint location = touch-> locationInView ();
Location = CCDirector: sharedDirector ()-> convertToGL (location );
B2Vec2 locationWorld = b2Vec2 (location. x/PTM_RATIO, location. y/PTM_RATIO );
// Obtain the coordinates of the touch
If (paddleFixeture-> TestPoint (locationWorld) // you can determine whether the image is within the touch range.
{
// If touched
B2MouseJointDef md;
Md. bodyA = groundBody; // It is generally a world boundary.
Md. bodyB = paddleBody; // the object to be dragged.
Md.tar get = locationWorld; // specify the coordinates of the drag.
Md. collideConnected = true ;//
Md. maxForce = 1000.0f * paddleBody-> GetMass (); // give a drag force
MouseJoint = (b2MouseJoint *) world-> CreateJoint (& md); // create
PaddleBody-> SetAwake (true );//
CCLOG ("touchBegan ");
}
}
}
The mouseJoint parameter is defined above.
Then change the coordinates in touchedMoved.
[Cpp]
Void HelloWorld: ccTouchesMoved (CCSet * touches, CCEvent * pEvent)
{
If (mouseJoint = NULL)
Return;
CCSetIterator it;
CCTouch * touch;
For (it = touches-> begin (); it! = Touches-> end (); it ++)
{
Touch = (CCTouch *) (* it );
If (! Touch)
Break;
CCPoint location = touch-> locationInView ();
Location = CCDirector: sharedDirector ()-> convertToGL (location );
B2Vec2 locationWorld = b2Vec2 (location. x/PTM_RATIO, location. y/PTM_RATIO );
MouseJoint-> SetTarget (locationWorld );
}
}
Finally, remember to add this release statement in touchedEnded.
If (mouseJoint! = NULL)
{
World-> DestroyJoint (mouseJoint );
MouseJoint = NULL;
}
PrismaticJoint
This is simpler.
It defines a physical motion range.
[Cpp]
// Restrict paddle
B2PrismaticJointDef jointDef;
JointDef. collideConnected = true;
B2Vec2 worldAxis (1.0f, 0.0f); // This is restricted to the direction of the horizontal X axis.
JointDef. initialize (paddleBody, groundBody, paddleBody-> GetWorldCenter (), worldAxis); // each parameter indicates a restricted object, a restricted area (generally the world), a restricted point, direction
World-> CreateJoint (& jointDef );