向鼠标射击行为很恶劣 - Java

问题描述:

在我的游戏中,我使用此代码向特定位置(鼠标X/Y)拍摄。如果 mouseX/Y是相同子弹进去方向,但是当mouseX/Y是不同(例如20和10),然后子弹变为在完全错误方向。感谢帮助!向鼠标射击行为很恶劣 - Java

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))); 
+2

[atan2]的文档(https://docs.oracle.com/javase/7/docs/api/java/lang/Math.html#atan2(double,%20double))表示输入应该是( y,x),是你遇到的问题? – Haem

+0

是的!谢谢。 – Nithorg

+1

@HeikkiMäenpää您可能都会回答或删除问题,以便从未回答的问题列表中找出答案。 – Yunnosch

文档为atan2说,你需要输入参数,以便(y,x)。这与典型的顺序有点奇怪,可能是因为您将这些顺序输入到分区中。