gpt4 book ai didi

2d 中的 Java 运动

转载 作者:行者123 更新时间:2023-11-30 11:54:28 33 4
gpt4 key购买 nike

我以前在 2d 项目中使用过类似的东西来移动并且它总是有效。我现在正在使用它,它为我提供了一些输出的正确角度和错误的角度。我认为我的触发器中有一些错误,我弄错了。不过,我已经检查了大约一百万次。 Prolly 只是自动纠正我头脑中的错误。这是一些示例输出和恶意代码。

this(x,y): (500.0, 250.0)
dest(x,y): (400.0, 300.0)
DeltX: 100.0
DeltY: -50.0
Angle: 0.46364760900080615 //about 26.56°, should be 206.56° (26.56+pi) i think
XVELOC: 0.08944272
YVELOC: 0.04472136

public void Update(long deltaTime){
if(this.destination == null){
return;
}

this.x += this.x_velocity * deltaTime;
this.y += this.y_velocity * deltaTime;

if(Math.abs(this.x-destination.x) < 1 && Math.abs(this.y-destination.y) < 1){
destination.arrive(this);
}
}

public void setPath(){
if(this.destination == null){
return;
}

double delta_x = (double) (this.x-this.destination.x);
double delta_y = (double) (this.y-this.destination.y);
int sign_x = (int)Math.signum(delta_x);
int sign_y = (int)Math.signum(delta_y);
double radian_angle = Math.atan((delta_x/delta_y));
if(sign_x > 0 && sign_y > 0)
radian_angle += Math.PI;
else if(sign_x > 0 && sign_y < 0)
radian_angle += Math.PI/2;
else if(sign_x < 0 && sign_y > 0)
radian_angle += 3*Math.PI/2;

System.out.println("DeltX: "+delta_x);
System.out.println("DeltY: "+delta_y);
System.out.println("Angle: "+radian_angle);

this.x_velocity = this.max_velocity*(float)Math.cos(radian_angle);
this.y_velocity = this.max_velocity*(float)Math.sin(radian_angle);

System.out.println("XVELOC: "+x_velocity);
System.out.println("YVELOC: "+y_velocity);
}

最佳答案

尝试使用 atan2 - 它专为您处理此问题

double delta_x = (double) (this.x-this.destination.x);
double delta_y = (double) (this.y-this.destination.y);
double radian_angle = Math.atan2(delta_y,delta_x); // arguements are y,x not x,y

http://download.oracle.com/javase/6/docs/api/java/lang/Math.html#atan2%28double,%20double%29

关于2d 中的 Java 运动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5705397/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com