首页  >  文章  >  Java 球运动:边界反射的意外行为

Java 球运动:边界反射的意外行为

王林
王林转载
2024-02-22 13:30:081148浏览

php小编西瓜为您介绍Java问答系列,本期探讨Java球运动中边界反射的意外行为。在编程过程中,我们经常面临各种意想不到的情况,尤其涉及到物理模拟的问题更加复杂。本篇文章将为您解析Java球运动中边界反射时可能出现的意外行为,帮助您更好地理解和处理这类问题,提升编程技能。

问题内容

`问题:

我正在开发一个 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 编程语言 使用基本直线方程计算球的运动 碰撞处理的矢量反射公式`

解决方法

我想我修复了这里的代码是固定代码

import java.util.Scanner;
import java.lang.Math;

public class 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();
   }
}}

以上是Java 球运动:边界反射的意外行为的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文转载于:stackoverflow.com。如有侵权,请联系admin@php.cn删除