增加球的重量

时间:2013-04-21 21:54:29

标签: kineticjs gravity

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

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

    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

1 个答案:

答案 0 :(得分:1)

您的“bouncingLevel”通常称为“恢复原状”,并应用于碰撞的对象。

恢复通常介于0.0 - 1.0之间。

1.0意味着物体绝对有弹性 - 因此它在碰撞过程中不会失去速度。

0.0表示物体在碰撞过程中失去了所有速度 - 因此它“splats”并且根本不会反弹。

以下是你为碰撞添加恢复原状的方法:

警告:我还没有尝试过我的代码......只是在我的头顶 - 你可能需要调试!

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

}