Move CCSprite around another CCSprite Circle Shape

2019-09-03 18:05发布

问题:

What I am trying to accomplish is this:

We have CCSprite Circle A and CCSprite Circle B.

Move Circle B around Circle A. I already tried to create a CCNode and attach the circle B to it. In this case it works perfectly but the position is constant also. I need to move the circle around A and update the position. I will have more objects on the screen and I will check if B intersect some other objects, but for that case I need to update the position while rotating. Much appreciate your help guys. I am using Cocos2D v3.0

回答1:

Maybe something like this? Put this in your update method. I used this code in my box2d project for orbit. Change it for your needs.

    b2Vec2 center = bodyA->GetPosition();
    int smoothness = 1000;
    int radius = 100;

    for (int i = 0; i < smoothness; i++) {
        float angle = (i / smoothness) * 360 * DEGTORAD;

        b2Vec2 pos( sinf(angle), cosf(angle));
        b2Vec2 newposition = center + radius * pos;

        bodyB->SetTransform(newposition, bodyB->GetAngle());

 }


回答2:

Put the anchor point of B in the position of the anchor point of A and then rotate the B by 360 in animation.