所以,如果您使用鼠标拍摄,我会编码2D射击游戏:
timeSinceLastShot+=timeSinceLastFrame;
double bullet_angle = Math.atan2(MyMouseListener.getClickPosY()-posY, MyMouseListener.getClickPosX()-posX);
float bullet_startX = posX+(skin.getWidth()/2)-(Bullet.getImg().getWidth()/2);
float bullet_startY = posY+(skin.getHeight()/2)-(Bullet.getImg().getHeight()/2);
double bullet_speedX = Math.cos(bullet_angle * Math.PI/180)*500;
double bullet_speedY = Math.sin(bullet_angle * Math.PI/180)*500;
if(timeSinceLastShot>(1f/shotsPerSec) && MyMouseListener.isButtonClicked(MouseEvent.BUTTON1)){
bullets.add(new Bullet(bullet_startX, bullet_startY, bullet_speedX, bullet_speedY));
timeSinceLastShot = 0;
}
所有子弹都向右移动,但我希望它们完全按照鼠标的方向移动。
答案 0 :(得分:0)
我认为你试图将bullet_angle转换为弧度,但它已经是弧度。基本上,bullet_speed是在角度=零时计算的,这是直的。从cos()和sin()计算中删除“* Math.PI / 180”。
timeSinceLastShot+=timeSinceLastFrame;
double bullet_angle = Math.atan2(MyMouseListener.getClickPosY()-posY, MyMouseListener.getClickPosX()-posX);
float bullet_startX = posX+(skin.getWidth()/2)-(Bullet.getImg().getWidth()/2);
float bullet_startY = posY+(skin.getHeight()/2)-(Bullet.getImg().getHeight()/2);
double bullet_speedX = Math.cos(bullet_angle)*500;
double bullet_speedY = Math.sin(bullet_angle)*500;
if(timeSinceLastShot>(1f/shotsPerSec) && MyMouseListener.isButtonClicked(MouseEvent.BUTTON1)){
bullets.add(new Bullet(bullet_startX, bullet_startY, bullet_speedX, bullet_speedY));
timeSinceLastShot = 0;
}