Buggy bouncing balls

╄→гoц情女王★ 提交于 2019-12-05 23:20:02

Nice animation, the issue is cased by the method bounce of class particle:

When the "ball" doesn't leave the wall in one step, then it sticks.
Note, if e.g the condition (this.xpos - this.size/2.0) < 0 is fulfilled twice, the the direction is inverted twice (this.getDX()*-1). This causes that the ball bounce into the wall continuously and leads to a trembling movement along the wall.

 public void bounce(){

     if((this.xpos - this.size/2.0) < 0 || (this.xpos + this.size/2.0) > width){
         this.setDX(this.getDX()*-1);
     }
     if((this.ypos - this.size/2.0) < 0 || (this.ypos + this.size/2.0) > height){
         this.setDY(this.getDY()*-1);
     }
 }

You have to ensure, the the direction (delx, dely) always points away from the wall, if the ball is to near to it:

public void bounce(){

    if( this.xpos - this.size/2.0 < 0 ) {
        this.setDX( Math.abs(this.getDX()) );
    } else if( this.xpos + this.size/2.0 > width ) {
        this.setDX( -Math.abs(this.getDX()) );
    }

    if( this.ypos - this.size/2.0 < 0 ) {
        this.setDY( Math.abs(this.getDY()) );
    } else if( this.ypos + this.size/2.0 > height ) {
        this.setDY( -Math.abs(this.getDY()) );
    }
}

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!