在圆周上均匀分布节点

时间:2014-08-20 19:21:02

标签: c++ user-interface cocos2d-x trigonometry

我正在尝试做的是“旋转木马”控制,其中节点从原点均匀分布,并在弧上均匀分布。

我的错误在于如何将新节点添加到圆周(节点添加到另一个节点之上或者它们重叠,不均匀分布

picture example of not evenly distributed nodes

这是将(节点)PotSprite(蓝色循环)添加到(圆周)RingSprite(紫色环)的函数:

void RingSprite::addPot () {
    auto pot = PotSprite::create( "Pot.png" );
    this->addChild( pot );

    float radius = this->radius() * 0.88f;
    auto children = this->getChildren();
    float angleStep = 360.0f / children.size();

    Vec2 pos;
    for (unsigned int i = 0; i < children.size(); ++i ) {
        pos = this->calcPointOnCircumference( radius, angleStep * i );
        children.at( i )->setPosition( pos );
    } 
}

Point calcPointOnCircumference (float radius, float angle) {
    return Point (cosf( angle ) * radius + this->getAnchorPointInPoints().x, 
                  sinf( angle ) * radius + this->getAnchorPointInPoints().y);
}

0 个答案:

没有答案