力的算法
1)力作用于速度,而不是距离:
vx += ax;
vy += ay;
ball.x += vx;
ball.y += vy;
2) 反弹:
if(ball.x + ball.radius > right){
ball.x = right - ball.radius;
vx *= -1; (无能量损失,如果有能量损失vx *= -0.7)
}
if(ball.x - ball.radius > left){
ball.x = left + ball.radius;
vx *= -1; (无能量损失,如果有能量损失vx *= -0.7)
}
3) 摩擦力:
vx *= 0.9;
vy *= 0.9;
ball.x += vx;
ball.y += vy;
4) 结合加速度和摩擦力:
vx += ax;
vy += ay;
vx *= friction;
vy *= friction;
ball.x += vx;
ball.y += vy;