通过触摸在贝塞尔曲线路径上旋转精灵 - Cocos2D/Box2D
我有一个可以通过触摸旋转的箭头。我想知道是否可以沿曲线旋转箭头?我做了一些研究,我认为它被称为贝塞尔路径?是否可以使用此代码在贝塞尔曲线路径上旋转精灵,如果可以,我将如何合并它?
UITouch *touch = [touches anyObject];
//acquire the previous touch location
CGPoint firstLocation = [touch previousLocationInView:[touch view]];
CGPoint location = [touch locationInView:[touch view]];
//preform all the same basic rig on both the current touch and previous touch
CGPoint touchingPoint = [[CCDirector sharedDirector] convertToGL:location];
CGPoint firstTouchingPoint = [[CCDirector sharedDirector] convertToGL:firstLocation];
CGPoint firstVector = ccpSub(firstTouchingPoint, _arrow.position);
CGFloat firstRotateAngle = -ccpToAngle(firstVector);
CGFloat previousTouch = CC_RADIANS_TO_DEGREES(firstRotateAngle);
CGPoint vector = ccpSub(touchingPoint, _arrow.position);
CGFloat rotateAngle = -ccpToAngle(vector);
CGFloat currentTouch = CC_RADIANS_TO_DEGREES(rotateAngle);
//keep adding the difference of the two angles to the dial rotation
arrowRotation += currentTouch - previousTouch;
例如,
我有一个球放在地上,其上方有一个箭头。当您触摸屏幕并移动箭头时,箭头将沿半圆轴移动。
曲线看起来像这样 半圆 箭头将绕轴旋转。
如果我需要更清楚,请告诉我。我真的需要一些帮助。
I have an arrow that I rotate with touch. I was wondering if it was possible to rotate the arrow on a curved line? I've done some research and I think it is called a bezier path? Is it even possible to rotate a sprite on a bezier path using this code and if so how would I even incorporate it?
UITouch *touch = [touches anyObject];
//acquire the previous touch location
CGPoint firstLocation = [touch previousLocationInView:[touch view]];
CGPoint location = [touch locationInView:[touch view]];
//preform all the same basic rig on both the current touch and previous touch
CGPoint touchingPoint = [[CCDirector sharedDirector] convertToGL:location];
CGPoint firstTouchingPoint = [[CCDirector sharedDirector] convertToGL:firstLocation];
CGPoint firstVector = ccpSub(firstTouchingPoint, _arrow.position);
CGFloat firstRotateAngle = -ccpToAngle(firstVector);
CGFloat previousTouch = CC_RADIANS_TO_DEGREES(firstRotateAngle);
CGPoint vector = ccpSub(touchingPoint, _arrow.position);
CGFloat rotateAngle = -ccpToAngle(vector);
CGFloat currentTouch = CC_RADIANS_TO_DEGREES(rotateAngle);
//keep adding the difference of the two angles to the dial rotation
arrowRotation += currentTouch - previousTouch;
For example,
I have a ball sitting on the ground and an arrow right above it. When you touch the screen and move the arrow, the arrow moves on a half circle axis.
The curve would look like this Half Circle and the arrow would be rotating on the axis.
Please let me know if I need to be more clear. I really need some help with this.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这几天遇到同样的问题。这个答案中的大多数链接都已损坏,所以我找到了材料 这里和这里并制作了这段代码。就像魔术一样。希望它能帮助某人。
小描述:
我有一个对象(自我)女巫用手指围绕另一个对象(self.target)旋转,并且我有一些动画精灵,例如自我运动指南,它通过贝塞尔函数围绕 self.target 旋转。
算法非常快,我有 100 多个指南的永久初始化,并且它可以在没有 CPU 过载的情况下工作。
Have faced with same problem couple of days. Most of links in this answer is broken, so I have found material here and here and made this code. Works like magic. Hope it will help someone.
Small description:
I have object (self) witch rotates by finger around another object (self.target), and i have some animated sprites like guides of self movement, which rotates around self.target by bezier function.
algoritm is quite fast, i have permanent initialization of 100+ guides and it works without CPU overload.
从未尝试过,但我认为这是可能的,基本上你想要做的是当用户触摸屏幕时箭头移动到曲线上的触摸位置并且箭头在曲线上旋转?
首先你需要让箭头旋转,然后执行贝塞尔动作
Never tried but i think it's possible, basically what you want to do is when the user touch the screen the arrow move to the touch location on a curve line and the arrow rotate on the curve ?
first you need to make the arrow rotate and then perform the bezier action