2012-02-23 2 views
1

GLKit에서 사용자의 손가락을 따라 가야합니다. 두 점 사이에 arctan을 사용하여 각도를 그리는 라디안을 계산합니다.GLKit을 사용하여지도에서 스프라이트의 중심을 중심으로 회전하는 방법

여기 트릭의 일부는 물체를 손가락의 중앙에 오도록 유지하는 것입니다. 그래서 앵커 포인트 (anchor point)라는 아이디어를 도입하여 사물이나 원점을 기준으로 물건을 그릴 수있게했습니다. 내 목표는 스프라이트를 제 위치로 이동시킨 다음 회전시키는 것입니다. 내 렌더러에 다음 코드가있다.

// 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; 

또 다른 한 가지 사실은 내 스프라이트가 텍스처 별칭에 있다는 것입니다. 내 회전을 꺼내면 내 스프라이트가 내 손가락 아래 가운데에 정확히 그려집니다. 내 프로젝트 매트릭스는 GLKMatrix4MakeOrtho (0, CGRectGetWidth (self.frame), CGRectGetHeight (self.frame), 0, 1, -1)입니다. 그래서 그것은 UIkit과 내장 된 뷰와 일치합니다.

답변

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); 

이렇게하면 이미지에서 회전 할 위치를 기반으로 추가 조정이 이루어집니다. 이것은 당신이 위치로 이동

0

내가 중심

먼저 주위에 스프라이트를 회전하는 데 사용되는 유사한 코드가 .. 매력처럼 작동, 당신이 그것을 회전, 당신은 halfsprite 다시

이동
- (GLKMatrix4) modelMatrix { 
    GLKMatrix4 modelMatrix = GLKMatrix4Identity; 

    float radians = GLKMathDegreesToRadians(self.rotation); 
    modelMatrix = GLKMatrix4Multiply(
     GLKMatrix4Translate(modelMatrix, self.position.x , self.position.y , 0), 
     GLKMatrix4MakeRotation(radians, 0, 0, 1)); 
    modelMatrix = GLKMatrix4Translate(modelMatrix, -self.contentSize.height/2, -self.contentSize.width/2 , 0); 
    return modelMatrix; 
} 
관련 문제