Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
  • Move Body To A Targeted Destination In Cocos2d WIth Appropriate Angle

    • 0
    • 4
    • 2
    • 0
    • 0
    • 0
    • 0
    • 0
    • 656
    Comment on it

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

Sign In

Sign up using

Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: