Link: http://blog.csdn.net/zhangxaochen/article/details/8009302
When I was a beginner at box2d, I thought it was a strange way to drag the body. We need to construct a b2mousejointdef struct in cctouchesbegan (if we have determined that the mouse has been clicked on the body) and set the associations between two bodya and bodyb, then, set target to the coordinates of the mouse in the box2d physical world, in meters ]. The Code is as follows: see here: http://is.gd/VoBVUJ
void HelloWorld::ccTouchesBegan(CCSet *pTouches, CCEvent *pEvent){if(_mj)return;CCTouch* touch=(CCTouch*)pTouches->anyObject();CCPoint tpos=touch->getLocation();b2Vec2 posInWorld(tpos.x/PTM_RATIO, tpos.y/PTM_RATIO);if(_carBody->GetFixtureList()->TestPoint(posInWorld)){b2MouseJointDef mjDef;mjDef.bodyA=_world->CreateBody(new b2BodyDef);mjDef.bodyB=_carBody;mjDef.target=posInWorld;mjDef.collideConnected=true;mjDef.maxForce=(float)ULONG_MAX;_mj=(b2MouseJoint*)_world->CreateJoint(&mjDef);}} //ccTouchesBegan
Where _ MJ is the member field of the helloworld class: "b2mousejoint * _ mj ;"
CCPoint tpos=touch->getLocation();
To get the GL coordinates of the touch point, which is obtained in cocos2d-x Engine 1.0:
CCPoint tpos = touch->locationInView();tpos = CCDirector::sharedDirector()->convertToGL(location);
Then, convert the touch coordinates to the world coordinates of box2d:
b2Vec2 posInWorld(tpos.x/PTM_RATIO, tpos.y/PTM_RATIO);
When constructing mousejointdef, the target is set to the coordinate:
mjDef.target=posInWorld;
In cctouchesmoved, we constantly assign new touch points to target:
void HelloWorld::ccTouchesMoved(CCSet *pTouches, CCEvent *pEvent){ if(!_mj) return; CCTouch* touch=(CCTouch*)pTouches->anyObject(); CCPoint tpos=touch->getLocation(); b2Vec2 posInWorld(tpos.x/PTM_RATIO, tpos.y/PTM_RATIO); _mj->SetTarget(posInWorld);} //ccTouchesMoved
In this way, the body is dragged on the screen. Of course, do not forget to settouchenabled (true) in the class Init () function or constructor; // version 1.0 is setistouchenabled (true );
------------------------------------------------------
But what exactly is "target? Remember that if box2d is not used and you want to implement the cocos2dx genie drag, it is like this:
void HelloWorld::ccTouchesMoved(CCSet *pTouches, CCEvent *pEvent){ cout<<"in ccTouchesMoved..."<<endl; CCTouch* touch=(CCTouch*)pTouches->anyObject(); CCPoint start=touch->getLocation(); CCPoint end=touch->getPreviousLocation(); 。。。。 CCPoint newPos=sprite->getPosition(); newPos=ccpAdd(newPos, ccpSub(end, start)); sprite->setPosition(newPos); 。。。。。。。
In cctouchesmoved, get the coordinates of the current touch and the previous touch coordinates, and then add the difference between the current coordinates and the two touch coordinate vectors as the new coordinates. This should be well understood. Why can the target of box2d drag the body? Check the box2d help manual and see the following description:
8.10 mouse Joint
The mouse joint is used in the testbed to manipulate bodies with the mouse. It attempts to drive a point on a body towards the current position of the cursor. There is no restriction on rotation.
The mouse joint definition has a target point, maximum force, frequency, and damping ratio. the target point initially coincides with the body's anchor point. the maximum force is used to prevent violent reactions when multiple
Dynamic bodies interact. You can make this as large as you like. The frequency and damping ratio are used to create a spring/damper effect similar to the distance joint.
Extends users have tried to adapt the mouse joint for game play. users often want to achieve precise positioning and instantaneous response. the mouse joint doesn' t work very well in that context. you may wish to consider using kinematic
Bodies instead.
Target is a 'target'. If you bind the body you want to move to a mousejoint object, the body will automatically fly toward this 'target' as long as the target coordinates are constantly updated. Because our driving force settings are very large: mjdef. maxforce = (float) ulong_max;
So the body flew so quickly that it seemed to be moved by fingers. If maxforce is small, the drag effect looks like dragging it with a loose rubber band. Very interesting ~
Link: http://blog.csdn.net/zhangxaochen/article/details/8009302
{Over }}