如何在 SFML 中制作一致的 clicked/Intersects/contains 方法?
How to make a consistent clicked/Intersects/contains method in SFML?
当我调整 window 大小时,SFML 中的交集功能出现一些问题。
因此,当 window 处于预定义大小时,我确实知道如何检测交叉点或是否单击了某些内容等等。
但是当调整 window 的大小时,sfml 中 shapes/sprites 的全局边界保持完全相同,而它们在 window 中的表示发生变化。
所以当我现在点击某物时,可能会发生正常的 SFML 包含对象的方法告诉我鼠标指针不在里面,即使它在屏幕上看起来像那样。
我唯一想到的是有一个变量(例如sf::vector2f)来存储window与原始大小相比的当前变化,然后不使用鼠标相对位置到当前 window 但(随着变化成倍增加)投影鼠标位置。
但这可能不是最好的解决方案,所以我想知道我是否遗漏了什么,因此我在寻求建议,在这里做什么?
您可以使用sf::RenderWindow::mapPixelToCoords方法找出鼠标的正确位置。
来自 SFML 文档:
Convert a point from target coordinates to world coordinates.
This function finds the 2D position that matches the given pixel of
the render target. In other words, it does the inverse of what the
graphics card does, to find the initial position of a rendered pixel.
Initially, both coordinate systems (world units and target pixels)
match perfectly. But if you define a custom view or resize your render
target, this assertion is not true anymore, i.e. a point located at
(10, 50) in your render target may map to the point (150, 75) in your
2D world – if the view is translated by (140, 25).
For render-windows, this function is typically used to find which
point (or object) is located below the mouse cursor.
This version uses a custom view for calculations, see the other
overload of the function if you want to use the current view of the
render target.
当我调整 window 大小时,SFML 中的交集功能出现一些问题。 因此,当 window 处于预定义大小时,我确实知道如何检测交叉点或是否单击了某些内容等等。 但是当调整 window 的大小时,sfml 中 shapes/sprites 的全局边界保持完全相同,而它们在 window 中的表示发生变化。 所以当我现在点击某物时,可能会发生正常的 SFML 包含对象的方法告诉我鼠标指针不在里面,即使它在屏幕上看起来像那样。
我唯一想到的是有一个变量(例如sf::vector2f)来存储window与原始大小相比的当前变化,然后不使用鼠标相对位置到当前 window 但(随着变化成倍增加)投影鼠标位置。
但这可能不是最好的解决方案,所以我想知道我是否遗漏了什么,因此我在寻求建议,在这里做什么?
您可以使用sf::RenderWindow::mapPixelToCoords方法找出鼠标的正确位置。
来自 SFML 文档:
Convert a point from target coordinates to world coordinates.
This function finds the 2D position that matches the given pixel of the render target. In other words, it does the inverse of what the graphics card does, to find the initial position of a rendered pixel.
Initially, both coordinate systems (world units and target pixels) match perfectly. But if you define a custom view or resize your render target, this assertion is not true anymore, i.e. a point located at (10, 50) in your render target may map to the point (150, 75) in your 2D world – if the view is translated by (140, 25).
For render-windows, this function is typically used to find which point (or object) is located below the mouse cursor.
This version uses a custom view for calculations, see the other overload of the function if you want to use the current view of the render target.