让精灵跟随鼠标,使用视图时出现奇怪的故障
Getting sprite to follow mouse, weird glitch when using views
我想知道是否有人可以帮助我。我的精灵一直跟随我的鼠标,直到我开始使用视图,我才开始使用SFML,使精灵跟随我的播放器,我使用这个。
void player::rotateToMouse(sf::Sprite &sprite, sf::RenderWindow &window)
{
this->mouse = sf::Mouse::getPosition(window);
const float PI = 3.14159265;
float a = playerPosition.x - mouse.x;
float b = playerPosition.y - mouse.y;
mouseAngle = (atan2(b, a)) * 180 / PI;
playerSprite.setRotation(mouseAngle + 180);
}
我将这段代码添加到我的更新方法中,像这样,
void player::update(sf::RenderWindow &window){
this->rotateToMouse(playerSprite, window);
this->followPlayer();
我这样设置我的视图,
void player::followPlayer(){
view.reset(sf::FloatRect(0, 0, 32 + 10, 32 + 10));
view.zoom(15);
view.setCenter(playerSprite.getPosition());
}
我的移动代码是,
if (sf::Keyboard::isKeyPressed(sf::Keyboard::W)){
playerSprite.move(std::cos(3.14159265 * mouseAngle / 180.f) * speed *-1, std::sin(3.14159265 * mouseAngle / 180.f) * speed *-1);
}
else if (sf::Keyboard::isKeyPressed(sf::Keyboard::S)){ //Speed
playerSprite.move(std::cos(3.14159265 * mouseAngle / 180.f) * speed , std::sin(3.14159265 * mouseAngle / 180.f) * speed );
}
this->playerPosition = playerSprite.getPosition();
我可以看到精灵的面稍微偏离,过了一会儿他开始随机旋转,我是c++的新手,如果有人能帮助我,我将不胜感激。
您需要使用sf::RenderTarget::mapPixelToCoords
。基本上,你需要改变计算鼠标位置的方式:
this->mouse = sf::Mouse::getPosition(window);
this->mouse = window.mapPixelToCoords(sf::Mouse::getPosition(window));
相关文章:
- 分段故障(堆芯转储)矢量
- 如何在选项卡视图Qt中设置一个新项目,并保存以前的项目
- C++中的动态铸造故障
- 数组的指针从不分段故障
- 视图中的参数推导失败:take_while
- vscode g++链路故障:体系结构x86_64的未定义符号
- 访问被拒绝后,c++中的故障保护代码
- Windows 10-使用gtkmm-3.0库和g++[包括再现]的分段故障
- 如何维护资源管理器项目视图中当前可见的项目列表
- 在另一个类视图中添加最多2个图表的正确方法是什么
- 调试 CUDA MMU 故障
- OpenVR:向视图方向移动
- 使用 WIN32 API (C/C++) 对特定树视图项进行着色
- 将所选值(通过视图)从 boost::multi_array 复制到另一个数组 (C++)
- Geeksforgeeks C 程序故障排除:IEE 754 表示法为十进制
- 如何在不使用滚动条的情况下使视图更改
- 列表视图更改选择颜色
- Qt - QVector 和模型视图 - 从列表视图获取自定义类的最佳方法是什么?
- Arch Linux.AUR 包 mysql 不能用 makepkg 构建.错误:构建 () 中出现故障
- 让精灵跟随鼠标,使用视图时出现奇怪的故障