码迷,mamicode.com
首页 > 其他好文 > 详细

弹动的公式和检测矩形边界的公式

时间:2016-12-06 09:42:44      阅读:166      评论:0      收藏:0      [点我收藏+]

标签:边界检测   ring   i++   key   nbsp   fixed   sqrt   距离   精简   

重要公式:

 

(1)简单缓动

dx = targetX - object.x;

dy = targetY - object.y;

vx = dx * easing;

vy = dy * easing;

object.x += vx;

object.y += vy;

可精简:

vx = (targetX - object.x) * easing;

vy = (targetY - object.y) * easing;

object.x += vx;

object.y += vy;

再精简:

object.x += (targetX - object.x) * easing;

object.y += (targetY - object.y) * easing;

 

(2)简单弹动

ax = (targetX - object.x) * spring;

ay = (targetY - object.y) * spring;

vx += ax;

vy += ay;

vx *= friction;

vy *= friction;

object.x += vx;

object.y += vy;

可精简:

vx += (targetX - object.x) * spring;

vy += (targetY - object.y) * spring;

vx *= friction;

vy *= friction;

object.x += vx;

object.y += vy;

再精简:

vx += (targetX - object.x) * spring;

vy += (targetY - object.y) * spring;

object.x += (vx *= friction);

object.y += (vy *= friction);

 

(3)有偏移的弹动

dx = object.x - fixedX;

dy = object.y - fixedY;

targetX = fixedX + Math.cos(angle) * springLength;

targetY = fixedY + Math.sin(angle) * springLength;

边界检测重要公式:

(1)矩形边界碰撞检测

tool.intersects = function(bodyA,bodyB){

  return !(bodyA.x + bodyA.width < bodyB.x ||

          bodyB.x + bodyB.width < bodyA.x ||

          bodyA.y + bodyA.height < bodyB.y ||

          bodyB.y + bodyB.height < bodyA.y);

};

 

(2)基于距离的碰撞检测

dx = objectB.x - objectA.x;

dy = objectB.y - objectA.y;

dist = Math.sqrt(dx * dx + dy * dy);

if(dist < objectA.radius + objectB.radius){}

 

(3)多物体碰撞检测

for(var i = 0; i < objects.length; i++){

  var objectA = objects[i];

  for(var j = i + 1; j < objects.length; j++){

    var objectB = objects[j];

    if(tool.intersects(objectA,objectB){}

  }

};

弹动的公式和检测矩形边界的公式

标签:边界检测   ring   i++   key   nbsp   fixed   sqrt   距离   精简   

原文地址:http://www.cnblogs.com/MyZsy/p/6136225.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!