目錄
問題內容
解決方法
首頁 Java Java 球運動:邊界反射的意外行為

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

Feb 22, 2024 pm 01:30 PM

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中文網其他相關文章!

本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

AI Hentai Generator

AI Hentai Generator

免費產生 AI 無盡。

熱門文章

R.E.P.O.能量晶體解釋及其做什麼(黃色晶體)
3 週前 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳圖形設置
3 週前 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.如果您聽不到任何人,如何修復音頻
3 週前 By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25:如何解鎖Myrise中的所有內容
4 週前 By 尊渡假赌尊渡假赌尊渡假赌

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)