Cocos2d-x Box2d notes: Using mouseJoint and PrismaticJoint

Source: Internet
Author: User

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 );

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.