gpt4 book ai didi

xna - 为什么 Vector2(来自 XNA 的库)使用 float 而不是 int?

转载 作者:行者123 更新时间:2023-12-04 19:37:31 25 4
gpt4 key购买 nike

为什么 Vector2(来自 XNA 的库)使用 float 而不是 int?

计算机屏幕上的位置以像素为单位,因此光标位置可以由两个整数定义。没有半像素这样的东西。那我们为什么要使用浮点数呢?

在 SpriteBatch 类中,我发现了 7 个称为 Draw 的重载方法。他们两个人:

public void Draw(Texture2D texture, Rectangle destinationRectangle, Color color);
public void Draw(Texture2D texture, Vector2 position, Color color);

所以我们可以看到 Draw 接受 int 和 float 坐标。

我在实现游戏对象的屏幕坐标时遇到了这个问题。我认为 Rectangle 是保存对象大小和屏幕坐标的好选择。但现在我不确定...

最佳答案

在数学上,向量是一个运动,而不是一个位置。虽然屏幕上的位置在技术上可能无法介于整数之间,但运动绝对可以。如果向量使用 int s,那么您可以移动的最慢的将是 (1, 1) 。使用 float s,您可以移动 (.1, .1)(.001, .001) 等。

(还要注意 XNA struct Point 确实使用了 int s。)

关于xna - 为什么 Vector2(来自 XNA 的库)使用 float 而不是 int?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10460251/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com