[英]Shoot to the mouse direction
I've got this "Shot" class. 我有这个“射击”课。 In the code, the target variables are the mouseX and mouseY.
在代码中,目标变量是mouseX和mouseY。 So when i click the mouse button, my player class will create a new shot object.
因此,当我单击鼠标按钮时,我的播放器类将创建一个新的射击对象。 But the shooting is inaccurate.
但是射击是不准确的。 How can i calculate the correct dx and dy?
我如何计算正确的dx和dy?
If i add the dx and dy to the "bullet's" x and y, the bullet will move to the mouse's direction.This is what i want. 如果我将dx和dy添加到“子弹”的x和y,则子弹将移动到鼠标的方向。这就是我想要的。 The mouse position is stored in targetX and targetY, when the object is created.
创建对象时,鼠标位置存储在targetX和targetY中。 This is the point what the oval wants to reach.
这就是椭圆形想要达到的点。
Links: 链接:
The code (from Shot.java): 代码(来自Shot.java):
public class Shot extends Entity {
private float targetX, targetY;
public Shot(World world, float x, float y, int width, int height, Color color, float targetX, float targetY) {
super(world, x, y, width, height, color);
this.targetX = targetX;
this.targetY = targetY;
}
@Override
public void render(GameContainer gc, Graphics g, Camera camera) {
g.setColor(color);
g.fillOval(x - camera.getX(), y - camera.getY(), width, height);
}
@Override
public void update(GameContainer gc, int delta) {
float dx = targetX - x;
float dy = targetY - y;
x += dx * delta * .001f;
y += dy * delta * .001f;
}
}
I tried this, but still not work: 我试过了,但还是行不通:
@Override
public void update(GameContainer gc, int delta) {
float length = (float) Math.sqrt((targetX - x) * (targetX - x) + (targetY - y) * (targetY - y));
double dx = (targetX - x) / length * delta;
double dy = (targetY - y) / length * delta;
x += dx;
y += dy;
}
I did it! 我做的! Here is my solution:
这是我的解决方案:
The problem was that, the target was the window's mouse position, and not the world's mouse position. 问题在于,目标是窗口的鼠标位置,而不是世界的鼠标位置。
This is how i calculated the world's mouse positions: 这就是我计算世界鼠标位置的方式:
float mouseWorldX = x + (mouseX - screen_width / 2); // x = player's x position
float mouseWorldY = y + (mouseY - screen_height / 2); // y = player's y position
This is code from my game at the moment is used to move a unit to the mouse when the right mouse button is pressed: 这是我游戏中的代码,当前用于在按下鼠标右键时将单位移动到鼠标:
length = Math.sqrt((target_X - player_X)*(target_X - player_X) + (target_Y - player_Y)*(target_Y - player_Y)); //calculates the distance between the two points
speed_X = (target_X - player_X) /length * player_Speed;
speed_Y = (target_Y - player_Y) /length * player_Speed;
This will move an object to the target in a line at a set speed. 这将以设定的速度将对象沿一条线移动到目标。
Edit: this is the actual code right from my game 编辑:这是我游戏中的实际代码
if(input.isMouseButtonDown(Input.MOUSE_RIGHT_BUTTON))
{
length = (float) Math.sqrt((player_waypoint_X - player_X)*(player_waypoint_X - player_X) + (player_waypoint_Y - player_Y)*(player_waypoint_Y - player_Y));
velocityX = (float) (player_waypoint_X - player_X) /length * (float) PlayerStats.player.db_player_Speed;
velocityY = (float) (player_waypoint_Y - player_Y) /length * (float) PlayerStats.player.db_player_Speed;
player_waypoint_X = input.getMouseX() - 2;
player_waypoint_Y = input.getMouseY() - 2;
}
For testing purposes the velocity's are defined in the init method along with length. 为了测试目的,在init方法中定义了速度以及长度。 Every time the right mouse is pressed the waypoints's X and Y are changed to the mouse location.
每次按下鼠标右键,航路点的X和Y都会更改为鼠标位置。
I learned this from this question velocity calculation algorithm . 我从这个问题速度计算算法中学到了这一点。
为了使子弹并非每次射击都改变方向,请创建一个数组列表,以使发射的每个子弹具有自己的x和y速度
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.