处理物体超出画布时的三种基本状态,复位,移除,反弹
(1)检测是否越界的核心算法
if( object.x - object.width / 2 > right ||
object.x + object.width / 2 < left ||
object.y - object.height / 2 > bottom ||
object.y + object.height / 2 < top){}
(2)求摩擦力(精度算法)
var v = Math.sqrt( vx * vx + vy * vy );
var angle = Math.atan2(vy,vx);
if(v > f){
v -= f;
}else{
v = 0;
};
vx = Math.cos(angle) * v;
vy = Math.sin(angle) * v;
(3)摩擦力(约等方法,因为精度限制,最后会变为零)
vx *= friction;
vy *= friction;