gpt4 book ai didi

c++ - 如何在 SFML 中制作一致的点击/相交/包含方法?

转载 作者:行者123 更新时间:2023-11-28 05:33:04 25 4
gpt4 key购买 nike

当我调整窗口大小时,SFML 中的交集功能出现了一些问题。因此,当窗口处于预定义大小时,我确实知道如何检测交叉点或是否单击了某些内容等等。但是当调整窗口大小时,sfml 中形状/ Sprite 的全局边界保持完全相同,而它们在窗口中的呈现发生变化。因此,当我现在单击某个对象时,可能会发生正常的 SFML 包含对象方法告诉我鼠标指针不在内部,即使它在屏幕上看起来像那样。

我唯一想到的是有一个变量(例如sf::vector2f)来存储窗口相对于原始大小的当前变化,然后不使用鼠标相对于当前窗口的位置,而是(随着变化成倍增加)投影鼠标位置。

但这可能不是最好的解决方案,所以我想知道我是否遗漏了什么,因此我在寻求建议,在这里做什么?

最佳答案

您可以使用 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.

关于c++ - 如何在 SFML 中制作一致的点击/相交/包含方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38978427/

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