2013-04-21 63 views
-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; 
     } 
    } 

感谢您的帮助和时间, 加仑

+0

您要添加的“质量”,然后就大规模插入到任何你用它来确定速度等课程的基本牛顿方程,因为大家谁已通过高中物理的人都知道,物体的质量不会影响其上的重力。 – 2013-04-21 22:09:26

+0

对不起,我从来没有在学校看到过物理学......我们在阿根廷有(也许有)一个糟糕的教育体系。所以,我会读一读。 – gal007 2013-04-24 19:09:47

回答

1

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

恢复原因的范围通常为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; 
} 

}