XNA 2D 矢量角 - 计算的正确方法是什么? [英] XNA 2D vector angles - what's the correct way to calculate?

查看:26
本文介绍了XNA 2D 矢量角 - 计算的正确方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 2D 中 XNA 中矢量角的标准工作方式是什么?

what is in XNA in 2D the standard way vector angles work ?

向右 0 度,向上 90 度,向左 180 度,向下 270 度?

0 degrees points right, 90 points up, 180 left, 270 down ?

float VectortoAngle(Vector2 vec)

Vector2 AngleToVector(float angle)

所以 VectortoAngle(AngleToVector(PI)) == PI ?

so that VectortoAngle(AngleToVector(PI)) == PI ?

推荐答案

回答你的第一个问题,0 度点向上,90 度点向右,180 度点向下,270 度点向左.这里是一个简单的2D XNA旋转教程给你更多信息.

To answer your first question, 0 degrees points up, 90 degrees points right, 180 degrees points down, and 270 degrees points left. Here is a simple 2D XNA rotation tutorial to give you more information.

至于将向量转换为角度和返回,我发现了几个很好的实现here:

As for converting vectors to angles and back, I found a couple good implementations here:

Vector2 AngleToVector(float angle)
{
    return new Vector2((float)Math.Cos(angle), (float)Math.Sin(angle));
}

float VectorToAngle(Vector2 vector)
{
    return (float)Math.Atan2(vector.Y, vector.X);
}

此外,如果您不熟悉 2D 编程,您可能需要查看 Torque X 2D,它为您提供了很多.如果您为 XNA 付费开发,您可以免费获得引擎二进制文件,并且有一个实用程序类可以从角度转换为向量并返回,以及其他类似的有用功能.

Also, if you're new to 2D programming, you may want to look into Torque X 2D, which provides a lot of this for you. If you've payed to develop for XNA you get the engine binaries for free, and there is a utility class which converts from angles to vectors and back, as well as other useful functions like this.

正如 Ranieri 在评论中指出的那样,当向上为 0 度时,该功能没有意义.这是一个 (up is (0, -1), right is (1, 0), down is (0, 1), left is (-1, 0):

As Ranieri pointed out in the comments, that function doesn't make sense when up is 0 degrees. Here's one that does (up is (0, -1), right is (1, 0), down is (0, 1), left is (-1, 0):

Vector2 AngleToVector(float angle)
{
    return new Vector2((float)Math.Sin(angle), -(float)Math.Cos(angle));
}

float VectorToAngle(Vector2 vector)
{
    return (float)Math.Atan2(vector.X, -vector.Y);
}

我还想指出,我已经使用 Torque 一段时间了,它使用 0 度向上,所以这就是我得到那部分的地方.Up 的意思是,在这种情况下,以与文件中相同的方式将纹理绘制到屏幕上.所以向下将绘制纹理颠倒.

I would also like to note that I've been using Torque for a while, and it uses 0 degrees for up, so that's where I got that part. Up meaning, in this case, draw the texture to the screen in the same way that it is in the file. So down would be drawing the texture upside down.

这篇关于XNA 2D 矢量角 - 计算的正确方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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