[英]ccsprite rotation offset when it rotates to touch location
我正在旋转精灵( rect
)以面向屏幕上的触摸位置。 代码可以工作,但是有一个度数的偏移量越大,精灵越垂直。 我在下面添加了一张图片,非常清楚地说明了我的问题。
红点是我在photoshop中添加的触摸位置,用于显示偏移问题。 所以唯一可见的精灵就是矩形(IE rect
)。
当我处于90度角时,偏移是最明显的。 然后它逐渐消失,越靠近中心。
我怎样才能使精灵准确地面对触摸位置? 或者我该如何纠正这个偏移?
- (void) ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch *touch = [touches anyObject];
CGPoint location = [touch locationInView:[touch view]];
location = [[CCDirector sharedDirector] convertToGL:location];
float angle = atan2f(location.y-rect.position.y, location.x-rect.position.y);
angle = CC_RADIANS_TO_DEGREES(angle);
angle *=-1;
rect.rotation = angle;
float distance = sqrtf(pow((location.x-rect.position.x), 2)+pow(location.y-rect.position.y, 2));
rect.scaleX = distance;
}
不应该这样说明:
float angle = atan2f(location.y-rect.position.y, location.x-rect.position.y);
相反:
float angle = atan2f(location.y-rect.position.y, location.x-rect.position.x);
?
此外,您的问题可能与您的精灵anchorPoint
。
默认情况下,精灵的锚点是(0.5, 0.5)
,即精灵的正中心。 这是作为精灵的参考点; 例如,你精灵的position
是锚点位置; 如果应用旋转角度,则精灵会围绕锚点旋转。
所以,你可以尝试像这样设置你的精灵锚点:
rect.anchorPoint = ccp(0,0);
要么
rect.anchorPoint = ccp(1,1);
这应该会更好。
或者,您可以保留现在的锚点,但是相对于视图的中心进行数学计算(我猜这是一个固定的点):
float angle = atan2f(location.y - viewCenter.y, location.x - viewCenter.x);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.