Java中简单的弹跳球程序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19588605/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 18:34:45  来源:igfitidea点击:

Simple bouncing ball program in Java

javabounce

提问by user2919717

I am trying to code a simple bouncing ball program. It has a rectangle and two balls in it. The balls have to bounce off the walls of the rectangle (I have coded that succesfull) and they also have to bounce off of each other. This is where I need your help. When I give the balls the same speed, they bounce fine and the program works, but I have to give the balls random speeds. When I do this the balls won't bounce anymore and just move through each other.

我正在尝试编写一个简单的弹跳球程序。它有一个矩形和两个球。球必须从矩形的壁上弹开(我已经成功地编写了代码),并且它们还必须相互弹开。这就是我需要你帮助的地方。当我给球相同的速度时,它们弹得很好并且程序可以运行,但是我必须给球随机速度。当我这样做时,球将不再弹跳,而是相互穿过。

import java.applet.*; 
import java.awt.*; 
import javax.swing.*;
import javax.swing.event.*;

public class BallApplet2 extends Applet implements Runnable 
{ 
  // Begin variabelen
  int x_pos1 = 150; 
  int y_pos1 = 200; 
  int radius1 = 20; 

  int x_pos2 = 250; 
  int y_pos2 = 200; 
  int radius2 = 20;

  private float ballspeedx1 = -3;   
  private float ballspeedy1 = 0;

  private float ballspeedx2 = 3;   
  private float ballspeedy2 = 0; 


  public void init() {} 

  public void start() { 
  Thread th = new Thread (this); 
   th.start (); } 
   public void stop() {} 
  public void destroy() {} 
  public void run () {
Thread.currentThread().setPriority(Thread.MIN_PRIORITY); 
while (true) 
{ 
  x_pos1 += ballspeedx1;
  y_pos1 += ballspeedy1;
  x_pos2 += ballspeedx2;
  y_pos2 += ballspeedy2;  


  repaint();
  // als x_pos < 100 is draait de richting van de bal om
  if (x_pos1  < 100) {
    ballspeedx1 = -ballspeedx1; 
    x_pos1 = 100; 
  } 
  if (x_pos2  < 100) {
    ballspeedx2 = -ballspeedx2; 
    x_pos2 = 100; 
  }  
  // als x_pos > 300 is draait de richting van de bal om
  if (x_pos1  > 300) {
    ballspeedx1 = -ballspeedx1; 
    x_pos1 = 300; 
  } 
  if (x_pos2  > 300) {
    ballspeedx2 = -ballspeedx2; 
    x_pos2 = 300; 
  }                                       
  // als de x van de rode bal gelijk is aan de x en twee keer de straal van de blauwe bal, draaien beide ballen om.
  if (x_pos1 == x_pos2 + 40) {
    ballspeedx1 = -ballspeedx1;
    ballspeedx2 = -ballspeedx2;
  }
  // als de x van de blauwe bal gelijk is aan de x van de rode bal, draaien beide ballen om.
  if (x_pos2 == x_pos1 ) {
    ballspeedx1 = -ballspeedx1;
    ballspeedx2 = -ballspeedx2;
  }
  // als de x en twee keer de straal van de rode bal gelijk is aan de x van de blauwe bal, draaien beide ballen om.
  if (x_pos1 + 40 == x_pos2) {
    ballspeedx1 = -ballspeedx1;
    ballspeedx2 = -ballspeedx2;  
  }
  // als de x en twee keer de straal    
  if (x_pos2 + 40 == x_pos1) {
    ballspeedx1 = -ballspeedx1;
    ballspeedx2 = -ballspeedx2;  
  }

  try { Thread.sleep (20); } 



  catch (InterruptedException ex) {} 

Thread.currentThread().setPriority(Thread.MAX_PRIORITY); }} 

public void paint (Graphics g) {

g.setColor (Color.red); 
g.fillOval (x_pos1 - radius1, y_pos1 - radius1, 2 * radius1, 2 * radius1); 

g.setColor (Color.blue); 
g.fillOval (x_pos2 - radius2, y_pos2 - radius2, 2 * radius2, 2 * radius2); 


g.setColor(Color.black);
g.drawLine(80,80,80,320); // lijn links
g.drawLine(320,80,320,320); // lijn rechts
g.drawLine(80,80,320,80);  // lijn boven
g.drawLine(80,320,320,320); // lijn onder
  }




  // Einde eventmethoden


} 

Does anyone have a solution? If so try to keep it as simple as possible please :)

有没有人有办法解决吗?如果是这样,请尽量保持简单:)

采纳答案by Richard Tingle

These 4 conditions appear to concern themselves with detecting a collision

这 4 种情况似乎与检测碰撞有关

  if (x_pos1 == x_pos2 + 40) {
    ballspeedx1 = -ballspeedx1;
    ballspeedx2 = -ballspeedx2;
  }
  if (x_pos2 == x_pos1 ) {
    ballspeedx1 = -ballspeedx1;
    ballspeedx2 = -ballspeedx2;
  }
  if (x_pos1 + 40 == x_pos2) {
    ballspeedx1 = -ballspeedx1;
    ballspeedx2 = -ballspeedx2;  
  }  
  if (x_pos2 + 40 == x_pos1) {
    ballspeedx1 = -ballspeedx1;
    ballspeedx2 = -ballspeedx2;  
  }

But they all ask if objects are at exact positions relative to other objects rather than within a range. Remember because this sort of program runs in time jumps an object may be partially within annother when the collision occurs, Changing all 4 of these conditions to just this one condition solves the problem

但他们都问物体是否处于相对于其他物体的准确位置,而不是在一个范围内。请记住,因为此类程序会及时运行,因此当碰撞发生时,对象可能部分位于另一个对象内,将所有 4 个条件更改为仅此一个条件即可解决问题

  if (Math.abs(x_pos2-x_pos1)<radius1+radius2){
    ballspeedx1 = -ballspeedx1;
    ballspeedx2 = -ballspeedx2;
  }

This rather than asking if the balls at specific distances from each other asks if then are overlapping at all, which provides a much safer test.

这不是询问彼此相距特定距离的球是否完全重叠,这提供了更安全的测试。

Obviously you know that these simple reverse velocity collisions are not physically correct, but an additional thing to watch out for is if a collision occurs in an overlap and (for whatever reason) the two objects aren't brought out of collision in the next time step, then annother collision occures, once this happens the two balls seem to "stick together". This can be resolved by physically moving the objects out of collision, then applying the velocity changes. This problem happens a lot with non elastic collisions but can also occure when multiple bodies collide at the same time.

显然,您知道这些简单的反向速度碰撞在物理上是不正确的,但要注意的另一件事是碰撞是否发生重叠并且(无论出于何种原因)这两个物体在下一次不会脱离碰撞一步,然后发生另一次碰撞,一旦发生这种情况,两个球似乎“粘在一起”。这可以通过将物体从碰撞中物理移动,然后应用速度变化来解决。这个问题在非弹性碰撞中经常发生,但在多个物体同时碰撞时也会发生。