1

GLKitでユーザーの指に続くテクスチャがあります。2点間のアークタンを使用して角度を描くためにラジアンを計算します。

ここでの秘訣の一部は、オブジェクトを指の中心に置いておくということです。そこで、原点や中心を基準にして物を描くことができるように、アンカーポイントのアイデアを紹介しました。私の目標は、スプライトを所定の位置に移動してから回転させることです。レンダラーに次のコードがあります。

// lets adjust for our location based on our anchor point.
GLKVector2 adjustment = GLKVector2Make(self.spriteSize.width * self.anchorPoint.x, 
                                       self.spriteSize.height * self.anchorPoint.y);

GLKVector2 adjustedPosition = GLKVector2Subtract(self.position, adjustment);
GLKMatrix4 modelMatrix = GLKMatrix4Multiply(GLKMatrix4MakeTranslation(adjustedPosition.x, adjustedPosition.y, 1.0), GLKMatrix4MakeScale(adjustedScale.x, adjustedScale.y, 1));

modelMatrix = GLKMatrix4Rotate(modelMatrix, self.rotation, 0, 0, 1);

effect.transform.modelviewMatrix = modelMatrix;
effect.transform.projectionMatrix = scene.projection;

もう1つの注意点は、私のスプライトがテクスチャエイリアス上にあることです。回転を外すと、スプライトは指の下の中央に正しく描画されます。私のプロジェクトマトリックスはGLKMatrix4MakeOrtho(0、CGRectGetWidth(self.frame)、CGRectGetHeight(self.frame)、0、1、-1);です。そのため、UIkitとそれに埋め込まれているビューと一致します。

4

2 に答える 2

2

回転する前に、追加のオフセットを計算するために、もう少し数学を追加する必要がありました。

// lets adjust for our location based on our anchor point.
GLKVector2 adjustment = GLKVector2Make(self.spriteSize.width * self.anchorPoint.x, 
                                       self.spriteSize.height * self.anchorPoint.y);

// we need to further adjust based on our so we can calucate the adjust based on our anchor point in our image.
GLKVector2 angleAdjustment;

angleAdjustment.x = adjustment.x * cos(self.rotation) - adjustment.y * sin(self.rotation);
angleAdjustment.y = adjustment.x * sin(self.rotation) + adjustment.y * cos(self.rotation);

// now create our real position.
GLKVector2 adjustedPosition = GLKVector2Subtract(self.position, angleAdjustment);
GLKMatrix4 modelMatrix = GLKMatrix4Multiply(GLKMatrix4MakeTranslation(adjustedPosition.x, adjustedPosition.y, 1.0), GLKMatrix4MakeScale(adjustedScale.x, adjustedScale.y, 1));

modelMatrix = GLKMatrix4Rotate(modelMatrix, self.rotation, 0, 0, 1);

これにより、画像内の回転したい場所に基づいて追加の調整が作成され、それに基づいて変換されます。これは魅力のように機能します..

于 2012-02-24T20:15:15.887 に答える