-1

我有一个球的重量问题。我可以应用重力和弹跳水平,但我不知道如何增加重量。我想做一个“摇滚球”和一个“橡胶球”,来比较它在视觉上的弹跳。

我编写了每半秒调用一次的函数:

    this.applyGravity = function(gravity, bouncingLevel){
        this.ySpeed += gravity;
        this.move(0, this.ySpeed);

        if(this.getY() + this.getHeight() > this.bottomBoundary)
        {
            this.setY(this.bottomBoundary - this.getHeight());
            this.ySpeed = 0;
            this.counter = 0;
        }
    }

感谢您的帮助和时间,GAL

4

1 回答 1

1

您的“bouncingLevel”通常被称为“restitution”,适用于正在碰撞的对象。

恢复原状的范围通常为 0.0 – 1.0。

1.0 表示物体绝对有弹性——因此它在碰撞过程中不会失去速度。

0.0 表示物体在碰撞过程中失去了所有的速度——所以它“飞溅”并且根本不反弹。

以下是如何为碰撞添加恢复原状:

警告:我没有在下面尝试过我的代码......只是在我的脑海中 - 你可能需要调试!

// create a flag to tell us whether we are currently colliding
var isColliding=false;

// Create a "squash"
// When an object collides, it can get shorter/fatter
// This squash variable simulates the object as it un-squashes
var squash=0;

this.applyGravity = function(gravity, bouncingLevel){

if(isColliding){

    // un-squash the object at ySpeed
    // note: ySpeed should be negative at this point
    squash += this.ySpeed;

    // if we're all un-squashed, show the object's motion again
    if(squash<0){
        // since squash<0 the object will now rise
        // above the boundary and start moving upward
        this.setY(this.getHeight+squash);
        // all done colliding...clear the flag
        isColliding=false;
    }

    return;
}

this.ySpeed += gravity;
this.move(0, this.ySpeed);

if(this.getY() + this.getHeight() > this.bottomBoundary)
{
    // set the new after-collision speed
    this.ySpeed = -this.ySpeed*bouncingLevel;

    // set the collision flag
    isColliding=true;

    // calculate squash:
    // == how far the object's momentum carried it into the boundary
    squash = this.getY() + this.getHeight();

    // visually set the object on bottomBoundary until it "rebounds"
    // alternatively, you could let it visually fall under the boundary
    this.setY(this.bottomBoundary - this.getHeight());

    this.counter = 0;
}

}

于 2013-04-21T23:54:27.307 回答