首頁  >  文章  >  Java 球運動:邊界反射的意外行為

Java 球運動:邊界反射的意外行為

王林
王林轉載
2024-02-22 13:30:081103瀏覽

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刪除
上一篇:活動未創建下一篇:活動未創建