In order to move body along with the sprite in cocos2d with box2d,you no need to worry about. Just take a look to this:-
PhysicsSprite* sprite = (PhysicsSprite*)body->GetUserData();
CGPoint target;
//set target for the body
target = CGPointMake(0,size.height);
int speed=10;
//convert target point into toMeters
b2Vec2 destination = [self toMeters:target];
//calculate distance between destination vector and a body current position
b2Vec2 toTarget = destination - body->GetPosition();
toTarget.x = toTarget.x/toTarget.Length()*speed;
toTarget.y = toTarget.y/toTarget.Length()*speed;
//Move body to a targeted position using impulse
body->ApplyLinearImpulse(toTarget,body->GetWorldCenter());
//calculate angle
float val = 0.0174532925199432957f;
float ax = destination.x-body->GetPosition().x;
float ay = destination.y-body->GetPosition().y;
float angle = atan(ay/ax);
//convert angle into radian by multiplying it with val
angle=angle*val;
//finally transform body along with the angle
body->SetTransform(body->GetPosition(), angle);
Hope this one will be helpful for you,enjoy code :)
0 Comment(s)