我写这个问题是因为我不理解Cocos2D为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文件中。
也许是因为我对空间几何很不了解,但如果有人能解释我,我非常感谢。
非常感谢。
if( ! CGPointEqualToPoint(anchorPointInPoints_, CGPointZero) ){
x += c*-anchorPointInPoints_.x + -s*-anchorPointInPoints_.y;
y += s*-anchorPointInPoints_.x + c*-anchorPointInPoints_.y;
}
上面的代码只是将xy坐标轴逆时针旋转$180-\θ$度,并将新坐标添加到先前的x、y坐标上,该坐标是从上述行之前的代码中获得的。
http://en.wikipedia.org/wiki/Rotation_of_axes提供轴的旋转公式