[英]SFML - can't get the projectile shooting in the right direction
我使用以下代碼來追趕玩家,這很好用:
float angle = atan2(player.y - monster.y, player.x - monster.x); monster.move(cos(angle) * 0.5f,0); monster.move(0, sin(angle) * 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());
項目符號不會放到釋放鼠標的地方,而且方向很奇怪,也許您對我的代碼有一些提示,或者使用其他方法來制作項目符號。 我真的什么都做不了……我嘗試將cos轉換為y,將sin轉換為x,禁用相機
問題是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;
}
...
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.