java - 加工重力(楼层)

标签 java processing game-physics

我正在使用processing 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 中的一些示例代码。这就解决了它。在下面提供的解决方案中,由于 Canvas 以像素(整数值)为单位进行测量,因此使用浮点来指定坐标会导致处理中的图形故障。因此,我实现了一个系统,其中浮点值被四舍五入,小数值被添加到累加器(“xRounder”和“yRounder”),当大于-1或1时,四舍五入并添加到球的当前位置。这给了我发言权!

最终代码:

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

最佳答案

我不是 100% 确定,但我认为,当球实际到达底线下方几个像素时,通过将球重置到底线,您可以精确地补偿弹性系数,以便球无限期地弹跳。

尝试删除行yPosition=(displayHeight-20);,然后它应该工作得更好。

这样,球最终会静止下来,但这仍然不正确,因为球落入地面的时间根本没有被计算在内。

更新 好的,我想现在我已经明白了。以下是相关内容:

// first update position, then update velocity
yPosition=yVelocity+yPosition;
if (yPosition >= floor) {
    // invert velocity and position w/ elasticity applied
    yVelocity = -yVelocity * elasticity;
    yPosition = floor - (yPosition - floor) * elasticity;
}
if(Math.abs(yVelocity) <= 2 && Math.abs(yPosition - floor) <= 2){
    // stop everything when close to rest
    yPosition=floor;
    yVelocity=0; 
} else {
    // otherwise accelerate, even after bounce
    yVelocity=yVelocity+yAccelleration;
}

所以主要的变化是你的代码是:

  1. 首先更新位置,然后更新速度
  2. 不是将球重置到地板上,而是恢复其从地板下落的量,并对其应用弹性系数
  3. 始终加速球,即使球从地板上弹起

这是它的样子(下限为 500)。不完美,但接近。

enter image description here

更新 2:刚刚发现 this Processing example ,这实际上非常接近您的原始代码,不同之处在于它们总是应用加速,并且它们碰撞检查之前这样做。看来仅此一点就可以解决问题。

关于java - 加工重力(楼层),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12340511/

相关文章:

java - Java 中的 ArrayList 是否有其他方法可以使用 Stream() 来实现?

java - 是否可以从类型为 Class 的 ArrayList 中调用方法?

java - 弹跳球,努力获得超过 1 个球(正在处理)

serial-port - 从 Processing 到 Arduino 的串行写入发送损坏的数据

delphi - 在 Delphi 和 openGL 中按下多个箭头键来移动对象

计算二维加速对象的截距

java - 我如何在 android 中启动应用程序信息屏幕?

java - 如何从不同的类访问变量

java - 为什么内部类不能使用静态初始化器?

swift - 在没有物理或 SKActions 的情况下使用 SpriteKit