Java Ball Movement: Unexpected Behavior with Boundary Reflections

65 views Asked by At

`Question:

I'm working on a Java program where I simulate the movement of a ball within a box. The ball is expected to reflect off the boundaries of the box using the vector reflection formula. However, I'm encountering unexpected behavior where the ball not only goes outside the boundary but also changes its position strangely.

Problem Description:

I have implemented the ball movement using basic line equations (X=x0+at, Y=y0+bt, Z=z0+ct). The part of the code responsible for detecting collisions and updating the ball's path is where I suspect the issue lies.`

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


}

Expected Behavior:

I expect the ball to properly reflect off the boundaries and stay within the box.

Observed Behavior:

However, the ball is going outside the boundary and changing its position unexpectedly.

Question:

Can you please review the collision detection and reflection part of my code and suggest any corrections? Is there a more effective way to handle ball reflections off the boundaries within a box? Additional Information:

Java programming language Using basic line equations for ball movement Vector reflection formula for collision handling`

1

There are 1 answers

0
Subhraneel On

I think I fixed the code here is the fixed code

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