C++ SFML-can';不要让炮弹朝正确的方向射击
我使用此代码对玩家进行怪物追逐,效果很好:C++ SFML-can';不要让炮弹朝正确的方向射击,c++,sfml,C++,Sfml,我使用此代码对玩家进行怪物追逐,效果很好: float angle=atan2(player.y-monster.y,player.x-monster.x); 怪物移动(cos(角度)*0.5f,0); 怪物移动(0,sin(角度)*0.5f) 我想我会改变它,使子弹从玩家射向鼠标指针: float angleShot2 = 0.0f; ... case sf::Event::MouseButtonReleased: { projectile.setPosi
float angle=atan2(player.y-monster.y,player.x-monster.x);
怪物移动(cos(角度)*0.5f,0);
怪物移动(0,sin(角度)*0.5f)代码>
我想我会改变它,使子弹从玩家射向鼠标指针:
float angleShot2 = 0.0f;
...
case sf::Event::MouseButtonReleased:
{
projectile.setPosition(player.x,player.y);
float angleShot = atan2(sf::Mouse::getPosition(window).y - projectile.y,
sf::Mouse::getPosition(window).x - projectile.x );
angleShot2 = angleShot; //so it goes in a straight line
}
...
projectile.move(cos(angleShot2) * 1.0f, 0);
projectile.move(0, sin(angleShot2) * 1.0f);
玩家、怪物和子弹都是长方形
窗口分辨率为1280x900
在设置了播放器的位置后,我使用相机的方式使其跟随播放器
sf::View view2(sf::FloatRect(0, 0, 1280, 900));
view.setSize(sf::Vector2f(1280, 900));
window.setView(view);
...
view.setCenter(player.getPosition());
子弹不会飞到鼠标被释放的地方,而是朝着奇怪的方向飞,也许你有一些关于我的代码的提示或者一种不同的制作子弹的方法。我真的什么都想不出来嗯。。。
我已经尝试将y的cos和x的sin反转,禁用相机。问题是,当所有实体使用世界坐标时,sf::Mouse::getPosition返回窗口坐标中光标的位置。可以通过使用sf::RenderWindow对象的mapPixelToCoords成员函数修复此问题:
...
case sf::Event::MouseButtonReleased:
{
projectile.setPosition(player.x,player.y);
sf::Vector2f mousePosition = window.mapPixelToCoords(sf::Mouse::getPosition(window));
float angleShot = atan2(mousePosition.y - projectile.y,
mousePosition.x - projectile.x );
angleShot2 = angleShot;
}
...