Java中的重力模拟器

Java中的重力模拟器,java,graphics,Java,Graphics,在这个程序中,我模拟重力。所有这些都可以工作,但当球不再移动时,它会根据我设置的重力值持续反弹1-5个像素。当能量损失时,我如何阻止球?我希望X速度变为0,球保持在固定位置 编辑:重力变量从1到100。用户可以改变重力 能量损耗=0.9,dt=0.2 // right and left wall collision if (x + xSpeed > this.getWidth() - radius - 1) { x = this.getWidth()

在这个程序中,我模拟重力。所有这些都可以工作,但当球不再移动时,它会根据我设置的重力值持续反弹1-5个像素。当能量损失时,我如何阻止球?我希望X速度变为0,球保持在固定位置

编辑:重力变量从1到100。用户可以改变重力

能量损耗=0.9,dt=0.2

// right and left wall collision
        if (x + xSpeed > this.getWidth() - radius - 1) {
            x = this.getWidth() - radius - 1;
            xSpeed = -xSpeed;
        } else if (x + xSpeed < 0 + radius) {
            x = 0 + radius;
            xSpeed = -xSpeed;
        } else
            x += xSpeed;

        if (y == this.getHeight() - radius - 1) {

        }

        if (y > this.getHeight() - radius - 1) {
            y = this.getHeight() - radius - 1;
            ySpeed *= energyLoss;
            ySpeed = -ySpeed;

            // friction with the ground
            xSpeed *= xFriction;
            if (Math.abs(xSpeed) < .4)
                xSpeed = 0;

        } else {
            ySpeed += gravity * dt; // velocity formula
            y += ySpeed * dt + .5 * gravity * dt * dt; // position formula
        }

        repaint();
//左右墙碰撞
如果(x+xSpeed>this.getWidth()-radius-1){
x=this.getWidth()-radius-1;
xSpeed=-xSpeed;
}否则如果(x+x速度<0+半径){
x=0+半径;
xSpeed=-xSpeed;
}否则
x+=x速度;
如果(y==this.getHeight()-radius-1){
}
如果(y>this.getHeight()-radius-1){
y=这个.getHeight()-radius-1;
Y速度*=无能量;
ySpeed=-ySpeed;
//与地面的摩擦
X速度*=X摩擦;
if(数学绝对速度(X速度)<.4)
xSpeed=0;
}否则{
ySpeed+=重力*dt;//速度公式
y+=y速度*dt+.5*重力*dt*dt;//位置公式
}
重新油漆();
过于简单的答案 在这一行之后:

ySpeed *= energyLoss;
ySpeed = -ySpeed;
更改此行:

ySpeed *= energyLoss;
ySpeed = -ySpeed;
对这样的事情:

if (ySpeed < SomeMinimumValue)
{
    ySpeed = 0;
}
else // invert speed, i.e. change direction.
{
    ySpeed = -ySpeed;
}
// right and left wall collision
    if (x + xSpeed > this.getWidth() - radius - 1) {
        x = this.getWidth() - radius - 1;
        xSpeed = -xSpeed;
    } else if (x + xSpeed < 0 + radius) {
        x = 0 + radius;
        xSpeed = -xSpeed;
    } else
        x += xSpeed;

    if (y == this.getHeight() - radius - 1 
        && ySpeed == 0) { // Check speed too here!
        // Do nothing for Y.
        // friction with the ground
        xSpeed *= xFriction;
        if (Math.abs(xSpeed) < .4)
            xSpeed = 0
    }
    else if (y > this.getHeight() - radius - 1) {
        y = this.getHeight() - radius - 1;
        ySpeed *= energyLoss;
        if (Math.abs(ySpeed) < SomeMinimumValue)
            ySpeed = 0;
        else // invert speed, i.e. change direction.
            ySpeed = -ySpeed;

        // friction with the ground
        xSpeed *= xFriction;
        if (Math.abs(xSpeed) < .4)
            xSpeed = 0;

    } else {
        ySpeed += gravity * dt; // velocity formula
        y += ySpeed * dt + .5 * gravity * dt * dt; // position formula
    }

    repaint();
if(ySpeed
编辑;第二次尝试:

if (Math.abs(ySpeed) < SomeMinimumValue)
... as above.
if(Math.abs(ySpeed)
少代码多说话
问题似乎源于球以较低的y或x速度撞击“地面”。如果是这种情况,当以足够低的y或x速度撞击“地面”时,需要将速度归零;当y速度足够低时为零y速度,当x速度足够低时为零x速度,不一定两者同时存在。您还需要确保您没有在y碰撞时将x速度调零,也没有在x碰撞时将y速度调零。

解决此问题的最简单方法是检测球以0(或非常小)y速度落在墙上的特殊情况。这基本上就是DwB的建议

然而,你需要更进一步,确保在这种情况下停止施加重力

大概是这样的:

if (ySpeed < SomeMinimumValue)
{
    ySpeed = 0;
}
else // invert speed, i.e. change direction.
{
    ySpeed = -ySpeed;
}
// right and left wall collision
    if (x + xSpeed > this.getWidth() - radius - 1) {
        x = this.getWidth() - radius - 1;
        xSpeed = -xSpeed;
    } else if (x + xSpeed < 0 + radius) {
        x = 0 + radius;
        xSpeed = -xSpeed;
    } else
        x += xSpeed;

    if (y == this.getHeight() - radius - 1 
        && ySpeed == 0) { // Check speed too here!
        // Do nothing for Y.
        // friction with the ground
        xSpeed *= xFriction;
        if (Math.abs(xSpeed) < .4)
            xSpeed = 0
    }
    else if (y > this.getHeight() - radius - 1) {
        y = this.getHeight() - radius - 1;
        ySpeed *= energyLoss;
        if (Math.abs(ySpeed) < SomeMinimumValue)
            ySpeed = 0;
        else // invert speed, i.e. change direction.
            ySpeed = -ySpeed;

        // friction with the ground
        xSpeed *= xFriction;
        if (Math.abs(xSpeed) < .4)
            xSpeed = 0;

    } else {
        ySpeed += gravity * dt; // velocity formula
        y += ySpeed * dt + .5 * gravity * dt * dt; // position formula
    }

    repaint();
//左右墙碰撞
如果(x+xSpeed>this.getWidth()-radius-1){
x=this.getWidth()-radius-1;
xSpeed=-xSpeed;
}否则如果(x+x速度<0+半径){
x=0+半径;
xSpeed=-xSpeed;
}否则
x+=x速度;
如果(y==this.getHeight()-radius-1
&&ySpeed==0){//这里也要检查速度!
//不要为你做任何事。
//与地面的摩擦
X速度*=X摩擦;
if(数学绝对速度(X速度)<.4)
xSpeed=0
}
否则,如果(y>this.getHeight()-radius-1){
y=这个.getHeight()-radius-1;
Y速度*=无能量;
if(数学绝对速度(ySpeed)
为了更快地获得更好的帮助,请发布一个。确保“(y>this.getHeight()-radius-1)”分支实际执行。它可能永远不会执行。另外,这个分支的另一部分可能是你想把速度归零的地方,问题是我的重力从0变到100。重力将由用户更改。如果要使用可变重力,则
SomeMinimumValue
将需要根据重力进行缩放-您需要随着重力的增加而增加该值。