[英]Top down shooter. How could I make the bullets fire in the direction the player is aiming?
I have a player made out of rectangles. 我有一个用矩形制成的播放器。 I currently have it so that it always aims towards the mouse. 我目前拥有它,因此它始终瞄准鼠标。 I would like the player to be able to fire the bullets straight towards the mouse but I am unsure of what equation I would use to make the bullet move the correct amount along the x and y so that it meets the point it is aimed at. 我希望玩家能够将子弹直接射向鼠标,但是我不确定我将使用什么方程式使子弹沿着x和y方向移动正确的量,以使其达到目标。
This is what I currently have for the player. 这是我目前为播放器所拥有的。
public void playerImage(Graphics g) {
Graphics2D g2d = (Graphics2D) g;
theta = Math.atan2(playerY - InputHandler.mouseY + playerHeight / 2,
playerX - InputHandler.mouseX + playerWidth / 2) - Math.PI / 2;
g2d.rotate(theta, playerX + playerWidth / 2, playerY + playerHeight / 2);
g2d.setColor(Color.blue.darker());
// Main body
g2d.drawRect(playerX, playerY, playerWidth, playerHeight);
// Left arm
g2d.drawRect(playerX - playerWidth / 2, playerY, playerWidth / 2,
playerHeight);
g2d.fillRect(playerX - playerWidth / 2, playerY, playerWidth / 2,
playerHeight);
// Right arm
g2d.drawRect(playerX + playerWidth, playerY, playerWidth / 2,
playerHeight);
g2d.fillRect(playerX + playerWidth, playerY, playerWidth / 2,
playerHeight);
g2d.setColor(Color.red);
g2d.fillRect(playerX, playerY, playerWidth, playerHeight);
}
Using the point where the player is located and the point where the bullet should go, you can define a line that goes through them. 使用播放器所在的点和子弹应到达的点,可以定义一条穿过它们的线。 This will give you an equation like f(x) = a*x + b, which then you can use to calculate your coordinates for the path. 这将为您提供一个方程,如f(x)= a * x + b,然后您可以用它来计算路径的坐标。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.