快速制作SKNode只需简单地“前进"即可.在一个角度上 [英] Swift-Making an SKNode simply "move forward" on an angle

查看:140
本文介绍了快速制作SKNode只需简单地“前进"即可.在一个角度上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在这里以不同的方式问了这个问题; 通过以下方法快速设置物理体速度角度,但不幸的是,这三种尝试都不是我所想要的,尽管我很感谢他们反正教会了我.我决定只用示例和进一步的解释来代替我的问题,而不要通过评论来使讨论永无休止.所以就在这里.

I've already asked this question in a different way here; Swift-Setting a physics body velocity by angle but the three attempts to answer it were unfortunately not exactly what I'm looking for, although I'm grateful for what they taught me anyway. I decided that I should simply rephrase my question with an example and further explanation instead of perpetuating a discussion via comments. So here it is.

想象一下,我有一个位于屏幕中央的SKNode.假设这是一个球,因此看不到它的任何旋转动作.我需要一种方法来选择一个随机角度,然后让SKNode旋转到其上,然后从屏幕中心的原始位置开始,按上述角度确定的方向连续移动,直到,例如,屏幕.

Imagine I have an SKNode positioned in the centre of the screen. Let's say this is a ball, so any rotation action on it is not visible. I would need a way to have a random angle selected, and have said SKNode rotate to it, and then continuously move in the direction determined by the aforementioned angle from its original position in the centre of the screen, until, say, the edge of the screen.

我知道如何确定随机角度,让SKNode旋转到随机角度,并使其停在屏幕边缘.希望在这个示例中,我需要的是更清楚的方法,一种简单地使SKNode向前移动的方法,但是其角度由单个变量确定,而不是由dx和dy两个确定的速度.

I know how to determine the random angle, have the SKNode rotate to it, and have it stop at the edge of the screen. Hopefully with this example what I need is clearer, a way to simply have an SKNode move forward, but on an angle determined by a single variable, and not a velocity determined by two, dx and dy.

谢谢.

推荐答案

为此,您只需要使用一些三角函数即可!

To do this, you just need to use some trigonometry!

zRotation在0到90之间时,除了将其转换为弧度外,不需要对角度做任何事情.只需调用tan(radianAngle).现在tan将返回节点在x轴上移动1时应在y轴上移动多少.如果您以前学过三角学,那么您应该理解我的意思.如果您尚未学习三角学,请学习它. :)

When zRotation is between 0 and 90, you don't need to do anything to the angle except converting it to radians. Just call tan(radianAngle). Now tan will return how much should the node move in the y axis when it moves by 1 in the x axis. If you learned trigonometry before, you should understand what I'm saying. If you have not learned trigonometry, learn it. :)

假设节点的zRotation为60度,即π/3弧度.将其传递到tan,您将得到√3.这意味着SKAction中的dxdy参数必须为1:√3,以使节点向60度方向移动.

Let's say the node's zRotation is 60 degrees, which is π/3 radians. Pass that into tan and you get √3. This means your dx and dy parameters in SKAction must be in the ratio of 1 : √3 in order to make the node move in the direction of 60 degrees.

zRotation在90到180之间时,您需要首先从180减去角度,然后将其转换为弧度.同样,将其传递给tan,返回值是您的节点在x轴上移动-1时应在y方向上移动的量. dx : dy的比例现在为-1:tan(angleInRadians).

When zRotation is between 90 and 180, you need to first subtract the angle from 180, then convert it to radians. Again, pass that to tan and the return value is how much your node should move in the y direction when it moves by -1 in the x axis. The dx : dy ratio is now -1 : tan(angleInRadians).

zRotation在180到270之间时,从该角度减去180并将其转换为弧度. dx : dy比是-1:-tan(angleInRadians).

When zRotation is between 180 and 270, subtract 180 from that angle and convert it to radians. The dx : dy ratio is -1 : -tan(angleInRadians).

最后,一个zRotation在270和360之间,从360减去角度并将其转换为弧度. dx : dy的比率是1:-tan(angleInRadians).

Lastly, a zRotation bewteen 270 and 360, subtract the angle from 360 and convert it to radians. The dx : dy ratio is 1 : -tan(angleInRadians).

在将角度转换为弧度之前,请检查角度是否为90度.如果是,请对dxdy进行硬编码,因为tan(M_PI / 2)未定义.

Just before you convert the angle to radians, check if the angle is 90 degrees. If it is, please hard code the dx and dy because tan(M_PI / 2) is undefined.

这篇关于快速制作SKNode只需简单地“前进"即可.在一个角度上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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