ioscocos2d-iphonebox2dbox2d-iphone

How to set revolution anchor when using b2RevoluteJointDef


The demo program is once slide the screen. The body (In my case, it is a blue cannon) will rotate correspondingly. So I defined a revolution joint for the cannon body and ground body, but seems the anchor is not setting correctly and lead to can't rotate the body with mouse joint. "international practices",I would upload related coding/screen next. Please see if you can help me.

Picture 1: Define related body,revolution joint

Picture 2: Touch callback function:Create mouse joint

Picture 3: Simulator screen(From the simulator,Seems the revolution anchor point is not correct)

I can't upload image or post img links here due to lack of "reputation", I have added the img links in myself comment .


Solution

  • First, when creating revolute joint you should use Initialize, like this:

    cannonJointDef.Initialize(groundBody, cannonBody, cannonBody->GetPosition());
    

    Last argument is anchor point in the world coordinates. In this case it will be the center of the cannon.

    Then, try to rotate cannon without mouseJoint, just by setting rotation directly:

    CGPoint touchVec = ccpSub(touchPos, _cannon.position);
    float angle = ccpToAngle(touchVec) - M_PI_2;
    cannonBody->SetTransform(cannonBody->GetPosition(), angle);