0

我写这个问题是因为我不理解 Cocos2D for iPhone 提供的示例中的一段代码:

-(CGAffineTransform) nodeToParentTransform
{   
b2Vec2 pos  = body_->GetPosition();

float x = pos.x * PTM_RATIO;
float y = pos.y * PTM_RATIO;

if ( ignoreAnchorPointForPosition_ ) {
    x += anchorPointInPoints_.x;
    y += anchorPointInPoints_.y;
}

// Make matrix
float radians = body_->GetAngle();
float c = cosf(radians);
float s = sinf(radians);

if( ! CGPointEqualToPoint(anchorPointInPoints_, CGPointZero) ){
    x += c*-anchorPointInPoints_.x + -s*-anchorPointInPoints_.y;
    y += s*-anchorPointInPoints_.x + c*-anchorPointInPoints_.y;
}

// Rot, Translate Matrix
transform_ = CGAffineTransformMake( c,  s,
                                   -s,  c,
                                   x,   y );    

return transform_;
}

它在 PhysicsSprite.mm 文件中。

也许是因为的空间几何学很差,但如果有人能解释我,我非常感激。

非常感谢。

4

1 回答 1

0
if( ! CGPointEqualToPoint(anchorPointInPoints_, CGPointZero) ){
    x += c*-anchorPointInPoints_.x + -s*-anchorPointInPoints_.y;
    y += s*-anchorPointInPoints_.x + c*-anchorPointInPoints_.y;
}

上面的代码只是简单地将 xy 坐标轴逆时针旋转 $180-\theta$ 度,并将新坐标添加到从上述行之前的代码中获得的先前 x,y 坐标。

http://en.wikipedia.org/wiki/Rotation_of_axes提供了轴的旋转公式

于 2012-09-02T16:45:42.303 回答