如何沿着圆圈移动点? [英] How to move point along circle?

查看:256
本文介绍了如何沿着圆圈移动点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用圆半径和移动角度以圆形运动移动精灵。

I want to move a sprite in a circular motion by using a circle radius and movingangle.

我知道例如精灵正在以半径10,它的当前位置是(387,38),角度是28度。

I know for instance that the sprite is moving in a circle with the radius 10 and It's current position is (387,38) and angle is 28 degrees. Now I wann't move it say, 100px along the circle perimeter.

p1 = x&y coordinate, known (387,38) in example with movingangle 28 degrees
r = radius, known (10 in example)
l = distance to move along bend, known (100px in example)
p2 = new point, what is this x and y value?

我有一个解决方案,但我不喜欢它。它对我没有意义,它需要更多的计算,我认为是必需的。它的工作原理是首先使用p1计算中心点,然后做相同的事情向后,所以说得到p2(使用余弦和窦)。应该有一个更快的方式我相信,但我不能找到任何人做这个。

I have a solution that works but I dont quite like it. It kind of don't make sense to me and it requires more calculations than I think is required. It works by first calculating the center point using p1 and then doing the same thing backwards so to speak to get p2 (using cosinus and sinus). There should be a quicker way I believe but I cant find anyone doing exactly this.

我标记cocos2d的原因是因为这是我正在使用的,有时游戏框架

The reason i tagged cocos2d is because thats what I'm working with and sometimes game frameworks provide functions to help with the trigonometry.

推荐答案

您可以使用锚点的魔法以一种非常简单的方式来完成它。与任何节点的转换完成相对它的锚点。旋转也是一种变换。所以,你可以这样做

You can do it with magic of anchor point in a really simple way =) All the transformations with any node are done relatieve to it's anchor point. Rotation is a kind of transformations too. So, you can do something like this

CGFloat anchorY = neededRadius / spriteHeight;
[yourSprite setAnchorPoint:ccp(0.5f, anchorY)];
[yourSprite setPosition:neededPosition]; //remember that position is also for anchor point
[self addChild: yourSprite];
[yourSprite runAction:anyRotateAction]; // i mean, CCRotateBy or CCRotateTo

这篇关于如何沿着圆圈移动点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆