在我的游戏中,我使用此代码向特定位置(mouseX / Y)拍摄。如果 mouseX / Y 相同子弹进入右方向,但当mouseX / Y 不同(例如20和10)时,子弹进入完全错误方向。谢谢你的帮助!
double bulletVelocity = 3.0; //however fast you want your bullet to travel
//mouseX/Y = current x/y location of the mouse
//originX/Y = x/y location of where the bullet is being shot from
double angle = Math.atan2(mouseX - originX, mouseY - originY);
o.setVelX((float)((bulletVelocity) * Math.cos(angle)));
o.setVelY((float)((bulletVelocity) * Math.sin(angle)));