Java 使用鼠标瞄准LibGDX

Java 使用鼠标瞄准LibGDX,java,libgdx,Java,Libgdx,我正在用libGDX做一个游戏,但是我在设置Bullet类时遇到了问题。我无法使投射物进入鼠标位置 我试着用Math.atan()来找到我需要射击的角度,但我没能让它起作用。现在我只是用距离来求x轴和y轴上的速度 private static final int SPEED = 500; private static Texture texture; String path = "C:\\Users\\minicodcraft\\Downloads\\game\\core\\assets\\";

我正在用libGDX做一个游戏,但是我在设置Bullet类时遇到了问题。我无法使投射物进入鼠标位置

我试着用Math.atan()来找到我需要射击的角度,但我没能让它起作用。现在我只是用距离来求x轴和y轴上的速度

private static final int SPEED = 500;
private static Texture texture;
String path = "C:\\Users\\minicodcraft\\Downloads\\game\\core\\assets\\";
private float x, y; // starting position
private float xVelocity, yVelocity;
private float yPos; // the y position of the mouse input
private float xPos; // the x position of the mouse input

public Bullet(float x, float y, float yPos, float xPos) {
    this.x = x;
    this.y = y;
    this.xPos = xPos;
    this.yPos = yPos;
    this.xVelocity = 0f;
    this.yVelocity = 0f;
    calcDirection();


    if (texture == null) {
        texture = new Texture(path + "Bullet.png");
    }
}

private void calcDirection() {
    float xDistanceFromTarget = Math.abs(xPos - x);
    float yDistanceFromTarget = Math.abs(yPos - y);
    float totalDistanceFromTarget = xDistanceFromTarget + yDistanceFromTarget;
    xVelocity = xDistanceFromTarget / totalDistanceFromTarget;
    yVelocity = yDistanceFromTarget / totalDistanceFromTarget;
    if (xPos < x) {
        xVelocity *= -1;
    }
    if (yPos < y) {
        yVelocity *= -1;
    }
}

public void update(float deltaTime) {
    if (x > 0 && y > 0) {
        x += xVelocity * SPEED * deltaTime;
        y += yVelocity * SPEED * deltaTime;
    } else if (x < 0 && y > 0) {
        x -= xVelocity * SPEED * deltaTime;
        y += yVelocity * SPEED * deltaTime;
    } else if (x > 0 && y < 0) {
        x += xVelocity * SPEED * deltaTime;
        y -= yVelocity * SPEED * deltaTime;
    } else if (x < 0 && y < 0) {
        x -= xVelocity * SPEED * deltaTime;
        y -= yVelocity * SPEED * deltaTime;
    }
}

public void render(SpriteBatch batch) {
    batch.draw(texture, x, y);
}
private static final int SPEED=500;
私有静态纹理;
String path=“C:\\Users\\minicodcraft\\Downloads\\game\\core\\assets\\”;
专用浮点x,y;//起始位置
私人浮动xVelocity,yVelocity;
私人浮动YPO;//鼠标输入的y位置
私有浮动XPO;//鼠标输入的x位置
公共项目符号(浮动x、浮动y、浮动YPO、浮动XPO){
这个.x=x;
这个。y=y;
this.xPos=xPos;
this.yPos=yPos;
这是0.x速度=0f;
这是。yVelocity=0f;
calcDirection();
如果(纹理==null){
纹理=新纹理(路径+“Bullet.png”);
}
}
私有void calcDirection(){
float xDistanceFromTarget=Math.abs(xPos-x);
float yddistance fromtarget=Math.abs(yPos-y);
浮点数totalDistanceFromTarget=XDDistanceFromTarget+yDistanceFromTarget;
xVelocity=xDistanceFromTarget/距离目标的总距离;
yVelocity=Ydistance from目标/总距离from目标;
如果(xPos0&&y>0){
x+=x速度*速度*增量时间;
y+=y斜率*速度*增量时间;
}如果(x<0&&y>0){
x-=x速度*速度*增量时间;
y+=y斜率*速度*增量时间;
}否则如果(x>0&&y<0){
x+=x速度*速度*增量时间;
y-=y斜率*速度*增量时间;
}else if(x<0&&y<0){
x-=x速度*速度*增量时间;
y-=y斜率*速度*增量时间;
}
}
公共无效渲染(SpriteBatch批处理){
批量绘制(纹理,x,y);
}

以下代码给出了从玩家位置到鼠标位置的速度:

float diffX = mouse.x - player.x;
float diffY = mouse.y - player.y;
float angle = (float) Math.atan2(diffY, diffX);
float velX = (float) (Math.cos(angle));
float velY = (float) (Math.sin(angle));
Vector2 velocity = new Vector2(velX, -velY);
velocity.nor();
velocity.scl(magnitudeSpeed);
velocity.scl(deltaTime);

然后
velocity.x
是速度的x分量。分别代表y。无需再次乘以速度和deltaTime,以上已完成。

谢谢!现在我只是在做y+=velY*SPEED*deltaTime;x+=velX*SPEED*deltaTime;我应该如何使用向量去定位?我已经为delta时间添加了一个刻度。向量的分量是你的速度。像以前一样将它们添加到位置。