目录
问题内容
解决方法
首页 Java Java 球运动:边界反射的意外行为

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

Feb 22, 2024 pm 01:30 PM

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

问题内容

`问题:

我正在开发一个 java 程序,在其中模拟球在盒子内的运动。使用矢量反射公式预计球会从盒子的边界反射。然而,我遇到了意想不到的行为,球不仅超出了边界,而且还奇怪地改变了位置。

问题描述:

我使用基本直线方程(x=x0+at、y=y0+bt、z=z0+ct)实现了球的运动。我怀疑问题出在代码中负责检测碰撞和更新球路径的部分。`

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

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

解决方法

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

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

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.能量晶体解释及其做什么(黄色晶体)
4 周前 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳图形设置
4 周前 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.如果您听不到任何人,如何修复音频
4 周前 By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25:如何解锁Myrise中的所有内容
1 个月前 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)