将弧度角转换为CGVector [英] Converting radian angle to CGVector

查看:191
本文介绍了将弧度角转换为CGVector的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Sprite Kit我试图设置一个 SKPhysicsBody 根据给定的角度移动,所以例如如果你想让精灵向右移动你会指定1.571弧度。要将指定的角度转换为速度,我使用下面的方法将弧度转换为 CGVector 。我从内存中实现的ORIGINAL版本具有将所有角度偏移90度的奇怪效果。 (即如果使用0度,精灵向右移动(就像你指定90度时一样)

Using Sprite Kit I am trying to set an SKPhysicsBody moving according to a given angle, so for example if you wanted the sprite to travel to the right you would specify 1.571 radians. To turn the specified angle into a velocity I am using the method below to convert radians to a CGVector. The ORIGINAL version that I implemented from memory has the strange effect of offsetting all the angles by 90degrees. (i.e. if 0 degrees is used the sprite moves right (just like it would if you specified 90degrees)

我已经通过交换 dx dy 分配在新版本中解决了这个问题。我的问题是为什么这种情况发生了,我在原版中是否有错(似乎有其他人在网上这样做)或者是否存在基于所使用的特定坐标系的原因。

I have fixed this in the NEW version by swapping the dx and dy assignments. My question is why does this happen, do I have it wrong in the original (there do seem to be others doing it that way on the web) or is there some reason based on the particular coordinate system being used.

// ORIGINAL
- (CGVector)convertAngleToVector:(CGFloat)radians {
    CGVector vector;
    vector.dx = cos(radians) * 10;
    vector.dy = sin(radians) * 10;
    NSLog(@"DX: %0.2f DY: %0.2f", vector.dx, vector.dy);
    return vector;
}


// NEW, SWAPPED DX & DY
- (CGVector)convertAngleToVector:(CGFloat)radians {
    CGVector vector;
    vector.dy = cos(radians) * 10;
    vector.dx = sin(radians) * 10;
    NSLog(@"DX: %0.2f DY: %0.2f", vector.dx, vector.dy);
    return vector;
}

注意:也在Sprite Kit中顺时针旋转负,到目前为止 convertAngleToVector 正在进行正向顺时针旋转(即1.571弧度是正确的,它应该留在哪里)我可以做 cos(弧度* -1) sin(弧度* -1)但基于我交换dx和dy可能存在一些潜在原因。

NOTE: also in Sprite Kit clockwise rotations are negative, so far convertAngleToVector is doing positive clockwise rotations (i.e. 1.571 radians is right, where it should be left) I could just do cos(radians*-1) and sin(radians*-1) but there might be some underlying reason for this based on me swapping dx and dy.

推荐答案

Sprite Kit编程指南(强调补充):


Sprite Kit还有一个标准的旋转约定。图4-2显示了极坐标约定。 0弧度的角度指定正x轴。正角度是逆时针方向。

Sprite Kit also has a standard rotation convention. Figure 4-2 shows the polar coordinate convention. An angle of 0 radians specifies the positive x axis. A positive angle is in the counterclockwise direction.

在该坐标系中,指向右边的零弧度角是正确的。如果要使用零角度直线向上(沿正y轴)并顺时针增加的系统,则需要在将角度转换为矢量之前转换角度。

In this coordinate system, an angle of zero radians pointing to the right is correct. If you want to use a system in which a zero angle is straight up (along positive y axis) and increase clockwise, you'll want to transform your angles before converting them to vectors.

这篇关于将弧度角转换为CGVector的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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