我正在使用处理java库来模拟使用牛顿物理学在两堵墙之间弹跳的球;水平速度是一个常数,因为没有加速度。我让模拟工作,但是我想指定地板的高度。然而,在我的方程式中,当球距离屏幕底部 10 像素时尝试改变方向时,每次反弹后,球在“地板”下方越来越低。如果我将地板提高到 >20 像素,球不会停止,而是无限期地反弹。这是相关代码: 请注意,处理的坐标系从顶部开始,向下和向右运行。我在这里先向您的帮助表示感谢。
public class Testing extends PApplet {
boolean inFloor=false; 
float xPosition=500;
float yPosition=200;
float xVelocity=25;
float yVelocity=-80.0f;
float yAccelleration=+10.0f;
float elasticity=0.80f;
public void setup (){
  size(displayWidth,displayHeight);
  noStroke();
  ellipseMode(RADIUS);
  frameRate(35);
}
 public boolean sketchFullScreen() {
  return true;
 }
public void draw(){
    background(0);
    //Changes direction of motion when hitting a wall
    if(xPosition>=displayWidth||xPosition<0){
        xVelocity=-xVelocity;
    }
    //supposed to change direction of motion when the ball hits the floor
    if(yPosition>=displayHeight-20){
        yPosition=(displayHeight-20);    
        yVelocity=-(yVelocity)*elasticity;
        if(yVelocity>=-1 && yVelocity<=0){
            xVelocity=xVelocity*elasticity;
            yVelocity=0; 
            yAccelleration=0;
        }    
    }
    else{
        yVelocity=yVelocity+yAccelleration;
    }
    yPosition=yVelocity+yPosition;
    xPosition=xPosition+xVelocity;
    ellipse(xPosition,yPosition,10,10);
}
编辑:这可能是时间问题吗?
编辑:感谢您的所有回复。不幸的是,我不能对其中任何一个进行投票(只有 6 个代表)。我结合了@tobius_k 的答案、@Roberto_Mereghetti 的答案以及来自OpenProcessing.org的一些示例代码,从而解决了它。在下面提供的解决方案中,由于画布以像素(整数值)为单位进行测量,因此使用浮点数来指定坐标会导致处理过程中出现图形故障。所以我实现了一个系统,其中浮点值被四舍五入,十进制值被添加到累加器(“xRounder”和“yRounder”),当大于 -1 或 1 时被四舍五入并添加到 Ball 的当前位置。这给了我一个地板!
最终代码:
    import processing.core.*; 
    //import processing.xml.*; 
    import java.applet.*; 
    import java.awt.Dimension; 
    import java.awt.Frame; 
    import java.awt.event.MouseEvent; 
    import java.awt.event.KeyEvent; 
    import java.awt.event.FocusEvent; 
    import java.awt.Image; 
    import java.io.*; 
    import java.net.*; 
    import java.text.*; 
    import java.util.*; 
    import java.util.zip.*; 
    import java.util.regex.*; 
    public class Testing extends PApplet {
    int xPosition=500;
    int yPosition=200;
    float xRounder=0;
    float yRounder=0;
    float xVelocity=25;
    float yVelocity=-80.0f;
    float yAccelleration=+10.0f;
    float elasticity=0.80f;
    public void setup (){
    size(displayWidth,displayHeight);
    noStroke();
    ellipseMode(RADIUS);
    frameRate(15);
    }
    public boolean sketchFullScreen() {
      return true;
    }
     /* (non-Javadoc)
     * @see processing.core.PApplet#draw()
     */
    public void draw(){
    background(0);
    yPosition=round(yVelocity)+yPosition;
    yRounder+=(yVelocity-round(yVelocity));
    xPosition=round(xVelocity)+xPosition;
    xRounder+=(xVelocity-round(xVelocity));
    if(xRounder>=1||xRounder<=-1){
        xPosition=xPosition+round(xRounder);
        xRounder=xRounder-round(xRounder);
    }
    if(yRounder>=1||yRounder<=-1){
        yPosition+=round(yRounder); 
        yRounder=yRounder-round(yRounder);
    }
    if(yPosition>displayHeight-50 && yVelocity>0){
        yPosition=displayHeight-50;
        yVelocity=-(yVelocity)*elasticity;  
        xVelocity=xVelocity*elasticity;
    }
    if(xPosition>=displayWidth||xPosition<0){
    xVelocity=-xVelocity;
    }
    yVelocity=yVelocity+yAccelleration;  
    ellipse(xPosition,yPosition,10,10);
    }
        static public void main(String args[]) {
           PApplet.main(new String[] { "--bgcolor=#ECE9D8", "Testing" });
    //      new Testing().setVisible(true);
        }
    }
