`问题:
我正在开发一个 Java 程序,在其中模拟球在盒子内的运动。使用矢量反射公式预计球会从盒子的边界反射。然而,我遇到了意想不到的行为,球不仅超出了边界,而且还奇怪地改变了位置。
问题描述:
我使用基本直线方程(X=x0+at、Y=y0+bt、Z=z0+ct)实现了球的运动。我怀疑问题出在代码中负责检测碰撞和更新球路径的部分。`
import java.util.Scanner;
import java.lang.Math;
public class test {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Get initial positions for ball 1
System.out.print("Enter ball 1's initial x-coordinate (x): ");
double x1 = scanner.nextDouble();
System.out.print("Enter ball 1's initial y-coordinate (y): ");
double y1 = scanner.nextDouble();
System.out.print("Enter ball 1's initial z-coordinate (z): ");
double z1 = scanner.nextDouble();
System.out.print("Enter ball 1's radius(r): ");
double r1 = scanner.nextDouble();
// Get direction vectorfor ball 1
System.out.print("Enter direction in x-coordinate (a): ");
double a = scanner.nextDouble();
System.out.print("Enter direction in y-coordinate (b): ");
double b = scanner.nextDouble();
System.out.print("Enter direction in z-coordinate (c): ");
double c = scanner.nextDouble();
double rt = 0;
double t1 = 0;
double t2 = 0;
while(true) {
double X1 = x1 + a * t1;
double Y1 = y1 + b * t1;
double Z1 = z1 + c * t1;
//**collision**
//This is the part that detects the collision and makes a new path for the ball
if (X1-r1 < -25){
a = -a;
x1 = -x1;
System.out.printf("ball hit\n");
}
else if (X1+r1 > 25){
a = -a;
x1 = -x1;
System.out.printf("ball hit\n");
System.out.printf("%f%f%f\n", a, b, c);
}
else if (Y1-r1 < -25){
b = -b;
y1 = -y1;
System.out.printf("ball hit\n");
System.out.printf("%f%f%f\n", a, b, c);
}
else if (Y1+r1 > 25){
b = -b;
y1 = -y1;
System.out.printf("ball hit\n");
System.out.printf("%f%f%f\n", a, b, c);
}
else if (Z1-r1 < -25){
c = -c;
z1 = -z1;
System.out.printf("ball hit\n");
System.out.printf("%f%f%f\n", a, b, c);
}
else if (Z1+r1 > 25){
c = -c;
z1 = -z1;
System.out.printf("ball hit\n");
System.out.printf("%f%f%f\n", a, b, c);
}
else{
System.out.printf("ball in boundary\n");
}
System.out.printf("Ball 1 at t=%.2f seconds: (%.2f, %.2f, %.2f)%n", rt, X1, Y1, Z1);
rt += 1;
t1 += s1;
// t2 += s2;
if (rt>10){
break;
}
}
// Close the scanner
scanner.close();
}
}
预期行为:
我希望球能够正确地反射出边界并留在盒子内。
观察到的行为:
然而,球却出乎意料地超出了边界并改变了位置。
问题:
您能否检查一下我代码中的碰撞检测和反射部分并提出任何更正建议? 是否有更有效的方法来处理盒子内边界外的球反射? 附加信息:
Java编程语言 使用基本直线方程计算球的运动 碰撞处理的矢量反射公式`
我想我修复了这里的代码是固定代码 导入java.util.Scanner; 导入 java.lang.Math;
公共课 BallExample{
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Get initial positions for ball 1
System.out.print("Enter ball 1's initial x-coordinate (x): ");
double x1 = scanner.nextDouble();
System.out.print("Enter ball 1's initial y-coordinate (y): ");
double y1 = scanner.nextDouble();
System.out.print("Enter ball 1's initial z-coordinate (z): ");
double z1 = scanner.nextDouble();
System.out.print("Enter ball 1's radius(r): ");
double r1 = scanner.nextDouble();
// Get direction vectorfor ball 1
System.out.print("Enter direction in x-coordinate (a): ");
double a = scanner.nextDouble();
System.out.print("Enter direction in y-coordinate (b): ");
double b = scanner.nextDouble();
System.out.print("Enter direction in z-coordinate (c): ");
double c = scanner.nextDouble();
// Get speed
System.out.print("Enter speed for ball 1(units per second): ");
double s1 = scanner.nextDouble();
double rt = 0;
double t1 = 0;
while(true) {
double X1 = x1 + a * t1;
double Y1 = y1 + b * t1;
double Z1 = z1 + c * t1;
System.out.printf("Ball 1 at t=%.2f seconds: (%.2f, %.2f, %.2f)%n", rt, X1, Y1, Z1);
if (X1+r1>=25||X1-r1<=-25){
a=-a;
System.out.printf("HIT\n");
}
if (Y1+r1>=25||Y1-r1<=-25){
b=-b;
System.out.printf("HIT\n");
}
if (Z1+r1>=25||Z1-r1<=-25){
c=-c;
System.out.printf("HIT\n");
}
x1=X1;
y1=Y1;
z1=Z1;
t1 = s1;
rt += 1;
if(rt>10){
break;
}
// Close the scanner
scanner.close();
}
}}