C++ 鼠标输入不是';t校正到世界坐标

C++ 鼠标输入不是';t校正到世界坐标,c++,sfml,C++,Sfml,可能有数百万个这样的问题,但是,当窗口重新调整大小时,我无法获得鼠标的坐标,以便它们与程序坐标系对齐。我尝试了mappixeltoords()并使用sf::Event::MouseButton或sf::mouse获取鼠标坐标,但没有效果 这个问题很可能是因为我的无能,但我一辈子也弄不明白。另外,我需要这样做来改变一个矩形的坐标,而不是检测一个盒子是否被悬停在上面,如果是的话,我觉得答案会容易得多。 编辑: 源代码: //标准C++: #包括 //SFML: #包括 int main() { sf

可能有数百万个这样的问题,但是,当窗口重新调整大小时,我无法获得鼠标的坐标,以便它们与程序坐标系对齐。我尝试了
mappixeltoords()
并使用
sf::Event::MouseButton
sf::mouse
获取鼠标坐标,但没有效果

这个问题很可能是因为我的无能,但我一辈子也弄不明白。另外,我需要这样做来改变一个矩形的坐标,而不是检测一个盒子是否被悬停在上面,如果是的话,我觉得答案会容易得多。 编辑:
源代码:
//标准C++:
#包括
//SFML:
#包括
int main()
{
sf::RenderWindow窗口(sf::VideoMode(800600),“示例”);
sf::事件;
sf::矩形鼠标点;
设置大小(sf::Vector2f(1,1));
setFillColor(sf::Color::Red);
while(window.isOpen())
{
while(window.pollEvent(事件))
{
if(event.type==sf::event::Closed)//关闭窗口
{
window.close();
返回0;
}
if(event.type==sf::event::MouseButtonPressed)
{
if(event.mouseButton.button==sf::Mouse::Left)
{
//获取鼠标位置:
sf::Vector2i mouse=sf::mouse::getPosition(窗口);
//将像素映射到坐标:
window.mappixeltoords(鼠标);
//将鼠标位置设置为矩形:
mousePoint.setPosition(mouse.x,mouse.y);
}
}
}
window.clear();
window.draw(鼠标点);
window.display();
}
}
经过一些怀疑,我上传了一些简单的源代码,这证明了我的观点。单击LMB时,它会将矩形移动到程序认为鼠标所在的位置。当屏幕未缩放时,它会被正确校准,但当它被更改时,矩形会移动到鼠标所在位置附近的某个点。

如SFML的和部分所示,您可以使用
mappixeltoords
功能将像素/屏幕坐标映射到世界坐标

该函数的签名如下:

Vector2f sf::RenderTarget::mapPixelToCoords(const Vector2i& point) const
因此,其用法如下所示:

//Get the mouse position:
sf::Vector2i mouse = sf::Mouse::getPosition(window);
//Map Pixel to Coords:
sf::Vecotr2f mouse_world = window.mapPixelToCoords(mouse);
//Set position of the mouse to the rectangle:
mousePoint.setPosition(mouse_world);
换句话说,
mappixeltoords
函数将
const sf::Vector2i&
作为参数,并返回
sf::Vector2f
,并且原始向量未被修改

如果某些东西不能按预期工作,建议您仔细查看文档。

如SFML的和部分所示,您可以使用
mappixeltoords
功能将像素/屏幕坐标映射到世界坐标

该函数的签名如下:

Vector2f sf::RenderTarget::mapPixelToCoords(const Vector2i& point) const
因此,其用法如下所示:

//Get the mouse position:
sf::Vector2i mouse = sf::Mouse::getPosition(window);
//Map Pixel to Coords:
sf::Vecotr2f mouse_world = window.mapPixelToCoords(mouse);
//Set position of the mouse to the rectangle:
mousePoint.setPosition(mouse_world);
换句话说,
mappixeltoords
函数将
const sf::Vector2i&
作为参数,并返回
sf::Vector2f
,并且原始向量未被修改

如果某些东西不能按预期工作,建议您仔细查看文档。

如SFML的和部分所示,您可以使用
mappixeltoords
功能将像素/屏幕坐标映射到世界坐标

该函数的签名如下:

Vector2f sf::RenderTarget::mapPixelToCoords(const Vector2i& point) const
因此,其用法如下所示:

//Get the mouse position:
sf::Vector2i mouse = sf::Mouse::getPosition(window);
//Map Pixel to Coords:
sf::Vecotr2f mouse_world = window.mapPixelToCoords(mouse);
//Set position of the mouse to the rectangle:
mousePoint.setPosition(mouse_world);
换句话说,
mappixeltoords
函数将
const sf::Vector2i&
作为参数,并返回
sf::Vector2f
,并且原始向量未被修改

如果某些东西不能按预期工作,建议您仔细查看文档。

如SFML的和部分所示,您可以使用
mappixeltoords
功能将像素/屏幕坐标映射到世界坐标

该函数的签名如下:

Vector2f sf::RenderTarget::mapPixelToCoords(const Vector2i& point) const
因此,其用法如下所示:

//Get the mouse position:
sf::Vector2i mouse = sf::Mouse::getPosition(window);
//Map Pixel to Coords:
sf::Vecotr2f mouse_world = window.mapPixelToCoords(mouse);
//Set position of the mouse to the rectangle:
mousePoint.setPosition(mouse_world);
换句话说,
mappixeltoords
函数将
const sf::Vector2i&
作为参数,并返回
sf::Vector2f
,并且原始向量未被修改


如果有什么东西不能按预期工作,我们总是建议仔细查看文档。

与之密切相关。@Quentin从我读这篇文章的方式来看,听起来他在尝试做一些与我不同的事情,考虑到唯一的答案就是完全按照我已经尝试过的方式来做,我怀疑它是否有用。您需要使用
mappixeltoords()
,如果这不起作用,我很确定这是您的代码中的一个问题,因此您必须提供最少的可编译示例。@Lukas我添加了一些源代码,其作用与我的程序完全相同,你看到提供的代码中有任何缺陷吗?与之密切相关。@Quentin从我读这篇文章的方式来看,听起来他试图做一些与我不同的事情,考虑到唯一的答案是完全按照我已经尝试过的方法来做,我怀疑它是否有用。如果不起作用,你需要使用
mappixeltoords()
,我很确定这是您的代码中的一个问题,因此您必须提供最少的可编译示例。@Lukas我添加了一些源代码,其作用方式与我的程序完全相同,您是否看到提供的代码中有任何缺陷?与之密切相关。@Quentin从我阅读本文的方式来看,听起来他在尝试做一些与我不同的事情,考虑到唯一的答案是完全按照我已经尝试过的方法来做,我怀疑它是否有用。你需要使用
mappixeltoords()
,如果这不起作用,我很确定这是你的代码中的一个问题,因此你必须提供最少的可编译的代码