为什么SFML位置是浮动的?
Why SFML position is float?
在 SFML 文档中,我看到 Sprites 和形状的位置是... float
。为什么?它是否与 sf::View
相关联,哪些边界也是 float
?
文档链接:
您需要浮点数才能正确设置精灵动画。想象一下 x=1 的位置,然后根据增量时间将位置增加到 1.1。将精灵移动到 x=2(下一个像素)需要 10 帧。使用整数 x 将四舍五入为 1,您根本不会移动。
除此之外,为了渲染东西,你必须计算变换矩阵(使用浮点数),所以你迟早要转换成浮点数。这就是图形 API 在内部的工作方式,SFML 简单地将所有内容包装起来,因此您只需担心屏幕坐标(像素,在这种情况下)。
SFML 中的坐标是浮点数而不是整数,因为实际的转换——从世界坐标到屏幕上的像素——不必以 1:1 方式发生。
您希望如何处理坐标及其映射完全取决于您。
例如,您可能希望以整数部分标识瓦片地图中瓦片坐标的方式使用浮点坐标,而小数部分表示瓦片边界之间的 space。在其他一些情况下,您可能希望将坐标 1:1 映射到像素。
最后,sf::View
与使用的 sf::RenderTarget
结合确定坐标的解释方式及其实际含义:
sf::RenderWindow window({512, 512}, "Test Window");
// This view is basically the default view for the window above.
// It maps coordinates 1:1 to pixels, which means a position (25,50) would map to the pixel at (25,50).
// This is the most common way.
sf::View pixelSpace({256, 256}, {512, 512});
// This view only shows things between (0, 0) and (1, 1), similar to how texture coordinates are usually handled.
// This can be useful, for example if you want to visualize the UV mapping of a model.
// You set this view and then just render based on the texture coordinate.
sf::View uvSpace({.5f, .5f}, {1, 1});
// This is an example for a tile based game, where the view allows you to
// make something 1.0 wide and high and it will automatically be the size of one tile.
// In this example I assume a 16:10 display ratio and I want 16x10 tiles to show.
sf::View tileSpace({8, 5}, {16, 10});
在 SFML 文档中,我看到 Sprites 和形状的位置是... float
。为什么?它是否与 sf::View
相关联,哪些边界也是 float
?
文档链接:
您需要浮点数才能正确设置精灵动画。想象一下 x=1 的位置,然后根据增量时间将位置增加到 1.1。将精灵移动到 x=2(下一个像素)需要 10 帧。使用整数 x 将四舍五入为 1,您根本不会移动。
除此之外,为了渲染东西,你必须计算变换矩阵(使用浮点数),所以你迟早要转换成浮点数。这就是图形 API 在内部的工作方式,SFML 简单地将所有内容包装起来,因此您只需担心屏幕坐标(像素,在这种情况下)。
SFML 中的坐标是浮点数而不是整数,因为实际的转换——从世界坐标到屏幕上的像素——不必以 1:1 方式发生。
您希望如何处理坐标及其映射完全取决于您。
例如,您可能希望以整数部分标识瓦片地图中瓦片坐标的方式使用浮点坐标,而小数部分表示瓦片边界之间的 space。在其他一些情况下,您可能希望将坐标 1:1 映射到像素。
最后,sf::View
与使用的 sf::RenderTarget
结合确定坐标的解释方式及其实际含义:
sf::RenderWindow window({512, 512}, "Test Window");
// This view is basically the default view for the window above.
// It maps coordinates 1:1 to pixels, which means a position (25,50) would map to the pixel at (25,50).
// This is the most common way.
sf::View pixelSpace({256, 256}, {512, 512});
// This view only shows things between (0, 0) and (1, 1), similar to how texture coordinates are usually handled.
// This can be useful, for example if you want to visualize the UV mapping of a model.
// You set this view and then just render based on the texture coordinate.
sf::View uvSpace({.5f, .5f}, {1, 1});
// This is an example for a tile based game, where the view allows you to
// make something 1.0 wide and high and it will automatically be the size of one tile.
// In this example I assume a 16:10 display ratio and I want 16x10 tiles to show.
sf::View tileSpace({8, 5}, {16, 10});