Search code examples
cocos2d-iphonebox2d-iphonedirection

box2d cocos2d. Calculate direction for SetLinearVelocity


I'm create character who moved by screen.

Character is sensor kinematic body in box2d world. Kinematic because, i want entity with collision, but without gravity. I'm try use sprites, but there are many problem with collision bounding box, because physic bodes sprite animated.

For transition logic i plan to use SetLinearVelocity(b2Vec2).

body->SetLinearVelocity(0, 0.5) move body top, body->SetLinearVelocity(0.5, 0.5) move body by 45 angle, it's work correct.

But i'm stupid – how move body using this method to different point.

In other words i need create analog CCMoveTo. Problem – how calculate true b2Vec2 for SetLinearVelocity.

Also, if you know better way for transition body by screen, please share your thoughts


Solution

  • O'k. On the cocos2d-iphone.org forum i get answer.Thanks, guys.

    CGPoint direction = ccp( targetPos.x - startPos.x, targetPos.y - startPos.y );
    direction = ccpNormalize(direction);
    float speed = 0.25f;
    b2Vec2 vel = b2Vec2( speed*direction.x/kPointsToMeterRatio, speed*direction.y/kPointsToMeterRatio );
    body->SetLinearVelocity(vel);