游戏动画必备干货——跟动画有关的数学和物理公式

(收藏自周文彬)

买了一本《html5+javascript动画基础》来看,里面很多数学和物理知识,跟着里面的例子学习了一遍。国庆回家的时候,看到我两年前买的一本《Flash ActionSctipt 3.0动画教程》,里面的内容差不多,作者也一样,只是都没有看过,于是又把这本书的例子又练习了一遍,加深印象。我觉得动画里面最难的就是
那些公式吧,语言都有相似之处,可能过几年作者还会写一本《processing动画基础》也说不定。思路是核心,其他都换汤不换药,所以我打算把跟动画有关的数学和物理知识整理下,几种不同的语言整理在一起方便比较,也方便以后用的时候查找。

角度与弧度互转

js

radians=degrees*Math.PI/180
degrees=radians*180/Math.PI

as

radians=degrees*Math.PI/180
degrees=radians*180/Math.PI
processing

radians=degrees*PI/180
degrees=radians*180/PI

朝鼠标指针(或任意一点)旋转

js

dx=mouse.x-object.x;
dy=mouse.y-boject.y;
object.rotation=Math.atan2(dy,dx)*180/Math.PI;

demo

as

dx=mouseX-sprite.x;
dy=mouseY-sprite.y;
sprite.rotation=Math.atan2(dy,dx)*180/Math.PI;

processing

dx=mouseX-sprite.x;
dy=mousyY-sprite.y;
object.rotation=atan2(dy,dx)*180/PI;

创建波

js

(function(){
  window.requestAnimationFrame(drawFrame,canvas);
  value=center+Math.sin(angle)+range;
  angle+=speed;
}());

demo

as

public function onEnterFrame(event:Event){
  value=center+Math.sin(angle)*range;
  range+=speed;
}

processing

void draw(){
  value=center+sin(angle)*range;
  range+=speed;
}

创建圆形

js

(function(){
  window.requestAnimationFrame(drawFrame,canvas);
  xposition=centerX + Math.cos(angle) * radius;
  yposition=center + Math.sin(angle) * radius;
  angle += speed;
}());

demo

as

public function onEnterFrame(event:Event){
  xposition = centerX + Math.cos(angle) * radius;
  yposition = centerY + Math.sin(angle) * radius;
  angle += speed;
}

processing

void draw(){
  xposition = centerX + cos(angle) * radius;
  yposition = centerY + sin(angle) * radius;
  angle += speed;
}

创建椭圆

js

(function(){
  window.requestAnimationFrame(drawFrame,canvas);
  xposition=centerX + Math.cos(angle) * radiusX;
  yposition=center + Math.sin(angle) * radiusY;
  angle += speed;
}());

demo

as

public function onEnterFrame(event:Event){
  xposition = centerX + Math.cos(angle) * radiusX;
  yposition = centerY + Math.sin(angle) * radiusY;
  angle += speed;
}

processing

void draw(){
  xposition = centerX + cos(angle) * radiusX;
  yposition = centerY + sin(angle) * radiusY;
  angle += speed;
}

获得两点间的距离

js

dx = x2 - x1;
dy = y2 - y1;
dist = Math.sqrt(dx * dx + dy * dy);

demo

as

dx = x2 - x1;
dy = y2 - y1;
dist = Math.sqrt(dx * dx + dy * dy);

processing

dx = x2 - x1;
dy = y2 - y1;
dist = sqrt(dx * dx + dy * dy);

绘制一条穿越莫个点的曲线

js

x1 = xt * 2 - (x0 + x2) / 2;
y1 = yt * 2 - (y0 + y2) / 2;
context.moveTo(x0, y0);
context.quadraticCurveTo(x1, y1, x2, y2);

demo

as

x1 = xt * 2 - (x0 + x2) / 2;
y1 = yt * 2 - (y0 + y2) / 2;
moveTo(x0, y0);
CurveTo(x1, y1, x2, y2);

processing

x1 = xt * 2 - (x0 + x2) / 2;
y1 = yt * 2 - (y0 + y2) / 2;
vertex(x0, y0);
quadraticVertex(x1, y1, x2, y2);

将角速度分解为x、y轴上的速度向量

js

vx = speed * Math.cos(angle);
vy = speed * Math.sin(angle);

demo

as

vx = speed * Math.cos(angle);
vy = speed * Math.sin(angle);

processing

vx = speed * cos(angle);
vy = speed * sin(angle);

将角加速度(作用于物体上的力)分解为x、y轴上的加速度

js

ax = force * Math.cos(angle);
ay = force * Math.sin(agnle);

demo

as

ax = force * Math.cos(angle);
ay = force * Math.sin(agnle);

processing

ax = force * cos(angle);
ay = force * sin(agnle);

将加速度加入速度向量

js

vx += ax;
vy += ay;

as

vx += ax;
vy += ay;

processing

vx += ax;
vy += ay;

将速度向量加入位置坐标

js

object.x += vx;
object.y += vy;

as

sprite.x += vx;
sprite.y += vy;

processing

object.x += vx;
object.y += vy;

移除越界物体

js

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){

}

demo

as

if(sprite.x - sprite.width /2 > right ||
   sprite.x + sprite.width /2 < left ||
   sprite.y - sprite.height /2 > bottom ||
   sprite.y + sprite.height /2 < top){

}

processing

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){

}

重置越界物体

js

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){

}

demo

as

if(sprite.x - sprite.width /2 > right ||
   sprite.x + sprite.width /2 < left ||
   sprite.y - sprite.height /2 > bottom ||
   sprite.y + sprite.height /2 < top){

}

processing

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){

}

屏幕环绕越界物体

js

if(object.x - object.width /2 > right){
  object.x = left - object.width / 2;
}else if(object.x + object.width /2 < left){
  object.x = right + object.width /2;
}
if(object.y - object.height / 2 > bottom){
  object.y = top -  object.height / 2;
}else if(object.y + object.height / 2 < top){
  object.y = bottom + object.height /2;
}

demo

as

if(sprite.x - sprite.width /2 > right){
  sprite.x = left - sprite.width / 2;
}else if(sprite.x + sprite.width /2 < left){
  sprite.x = right + sprite.width /2;
}
if(sprite.y - sprite.height / 2 > bottom){
  sprite.y = top -  sprite.height / 2;
}else if(sprite.y + sprite.height / 2 < top){
  sprite.y = bottom + sprite.height /2;
}

processing

class="brush:js">
if(object.x - object.width /2 > right){
  object.x = left - object.width / 2;
}else if(object.x + object.width /2 < left){
  object.x = right + object.width /2;
}
if(object.y - object.height / 2 > bottom){
  object.y = top -  object.height / 2;
}else if(object.y + object.height / 2 < top){
  object.y = bottom + object.height /2;
}

应用摩擦力(正确方法)

js

speed = Math.sqrt(vx * vx + vy * vy);
angle = Math.atan2(vy,vx);
if(speed > friction){
  speed -= friction;
}else{
  speed = 0;
}
vx = Math.cos(angle) * speed;
vy = Math.sin(angle) * speed;

demo

as

speed = Math.sqrt(vx * vx + vy * vy);
angle = Math.atan2(vy,vx);
if(speed > friction){
  speed -= friction;
}else{
  speed = 0;
}
vx = Math.cos(angle) * speed;
vy = Math.sin(angle) * speed;

processing

speed = sqrt(vx * vx + vy * vy);
angle = atan2(vy,vx);
if(speed > friction){
  speed -= friction;
}else{
  speed = 0;
}
vx = cos(angle) * speed;
vy = sin(angle) * speed;

应用摩擦力(简便方法)

js

vx *= friction;
vy *= friction;

demo

as

vx *= friction;
vy *= friction;

processing

vx *= friction;
vy *= friction;

简单缓动

js

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

demo

as

sprite.x += (targetX - sprite.x) * easing;
sprite.y += (targetY - sprite.y) * easing;

processing

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

简单弹动

js

vx += (targetX - object.x) * spring;
vy += (targetY - object.y) * spring;
object.x += (vx *= friction);
object.y += (vy *= friction);

demo

as

vx += (targetX - sprite.x) * spring;
vy += (targetY - sprite.y) * spring;
sprite.x += (vx *= friction);
sprite.y += (vy *= friction);

processing

vx += (targetX - object.x) * spring;
vy += (targetY - object.y) * spring;
object.x += (vx *= friction);
object.y += (vy *= friction);

有偏移量的弹动

js

var dx = object.x - fixedX,
    dy = object.y - fixedY,
    angle = Math.atan2(dy,dx),
    targetX = fixedX + Math.cos(angle) * springLength,
    targetY = fixedX + Math.sin(angle) * springLength;

demo

as

var dx:Number = sprite.x - fixedX;
var dy:Number = sprite.y - fixedY;
var angle:Number = Math.atan2(dy,dx);
var targetX:Number = fixedX + Math.cos(angle) * springLength;
var targetY:Number = fixedX + Math.sin(angle) * springLength;

processing

int dx = object.x - fixedX;
int dy = object.y - fixedY;
int angle = Math.atan2(dy,dx);
int targetX = fixedX + Math.cos(angle) * springLength;
int targetY = fixedX + Math.sin(angle) * springLength;

基于距离的碰撞检测

js

var dx = objectB.x - objectA.x,
    dy = objectB.y - objectB.y,
    dist = Math.sqrt(dx * dx + dy * dy);

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

}

demo

as

var dx:Number = spriteB.x - spriteA.x;
var dy:Number = spriteB.y - spriteB.y;
var dist:Number = Math.sqrt(dx * dx + dy * dy);

if(dist < spriteA.radius + spriteB.radius){

}

processing

int dx = objectB.x - objectA.x;
int dy = objectB.y - objectB.y;
int dist = sqrt(dx * dx + dy * dy);

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

}

多物理碰撞检测

js

objects.forEach(function(objectA, i){
  for(var j = i + 1; j < objects.length; j++){
    var objectB = objects[j];
    //执行碰撞检测,在objectA和objectB之间。
  }
});

demo

as

var numObjects:unit = 10;
for(var i:uint = 0; i < numObjects -1; i++){
  var ObjectA = objects[i];
  for(var j:uint = i + 1; j < numObjects; j++){
    var objectB = objects[j];
    //执行碰撞检测,在objectA和objectB之间。
  }
});
}

processing

int numObjects = 10;
for(int i = 0; i < numObjects -1; i++){
  int ObjectA = objects[i];
  for(int j = i + 1; j < numObjects; j++){
    int objectB = objects[j];
    //执行碰撞检测,在objectA和objectB之间。
  }
});
}

坐标旋转

js

x1 = x * Math.cos(rotation) - y * Math.sin(rotation);
y1 = y * Math.cos(rotation) + x * Math.sin(rotation);

demo

as

x1 = x * Math.cos(rotation) - y * Math.sin(rotation);
y1 = y * Math.cos(rotation) + x * Math.sin(rotation);

processing

x1 = x * cos(rotation) - y * sin(rotation);
y1 = y * cos(rotation) + x * sin(rotation);

反向坐标旋转

js

x1 = x * Math.cos(rotation) + y * Math.sin(rotation);
y1 = y * Math.cos(rotation) - x * Math.sin(rotation);

as

x1 = x * Math.cos(rotation) + y * Math.sin(rotation);
y1 = y * Math.cos(rotation) - x * Math.sin(rotation);

processing

x1 = x * cos(rotation) + y * sin(rotation);
y1 = y * cos(rotation) - x * sin(rotation);

动量守恒

js

var vxTotal = vx0 -vx1;
vx0 = ((ball0.mass -ball1.mass) * vx0 + 2 * ball1.mass * vx1) / (ball0.mass + ball1.mass);
vx1 = vxTotal + vx0;

demo

as

var vxTotal:Number = vx0 -vx1;
vx0 = ((ball0.mass -ball1.mass) * vx0 + 2 * ball1.mass * vx1) / (ball0.mass + ball1.mass);
vx1 = vxTotal + vx0;

processing

int vxTotal = vx0 -vx1;
vx0 = ((ball0.mass -ball1.mass) * vx0 + 2 * ball1.mass * vx1) / (ball0.mass + ball1.mass);
vx1 = vxTotal + vx0;

万有引力

js

function gravitate(partA, partB){
  var dx = partB.x - partA.x;
      dy = partB.y - partA.y;
      distSQ =  dx * dx + dy * dy;
      dist = Math.sqrt(distSQ);
      force = partA.mass * partB.mass / distSQ;
      ax = force * dx /dist;
      ay = force * dy / dist;

  partA.vx += ax / partA.mass;
  partA.vy += ax / partA.mass;
  partB.vx -= ax / partB.mass;
  partB.vy -= ax / partB.mass;
}

demo

as

void gravitate(partA, partB){
  var dx:Number = partB.x - partA.x;
  var dy:Number = partB.y - partA.y;
  var distSQ:Number =  dx * dx + dy * dy;
  var dist:Number = sqrt(distSQ);
  var force:Number = partA.mass * partB.mass / distSQ;
  var ax:Number = force * dx /dist;
  var ay:Number = force * dy / dist;

  partA.vx += ax / partA.mass;
  partA.vy += ax / partA.mass;
  partB.vx -= ax / partB.mass;
  partB.vy -= ax / partB.mass;
}

processing

void gravitate(partA, partB){
  float dx = partB.x - partA.x;
  float dy = partB.y - partA.y;
  float distSQ =  dx * dx + dy * dy;
  float dist = sqrt(distSQ);
  float force = partA.mass * partB.mass / distSQ;
  float ax = force * dx /dist;
  float ay = force * dy / dist;

  partA.vx += ax / partA.mass;
  partA.vy += ax / partA.mass;
  partB.vx -= ax / partB.mass;
  partB.vy -= ax / partB.mass;
}

余弦定理

js

var A = Math.acos((b * b + c * c - a * a) / (2 * b * c));
var B = Math.acos((a * a + c * c - b * b) / (2 * a * c));
var C = Math.acos((a * a + b * b - c * c) / (2 * a * b));

demo

as

A = Math.acos((b * b + c * c - a * a) / (2 * b * c));
B = Math.acos((a * a + c * c - b * b) / (2 * a * c));
C = Math.acos((a * a + b * b - c * c) / (2 * a * b));

processing

float A = acos((b * b + c * c - a * a) / (2 * b * c));
float B = acos((a * a + c * c - b * b) / (2 * a * c));
float C = acos((a * a + b * b - c * c) / (2 * a * b));

基本透视图

js

scale = fl / (fl + zpos);
object.scaleX = object.scaleY = scale;
object.alpha = scale;
object.x = vanishingPointX + xpos * scale;
object.y = vanishingPointY + ypos * scale;

demo

as

scale = fl / (fl + zpos);
sprite.scaleX = sprite.scaleY = scale;
sprite.alpha = scale;
sprite.x = vanishingPointX + xpos * scale;
sprite.y = vanishingPointY + ypos * scale;

processing

processing 3d有其他方法

Z排序

js

function zSort(a, b){
  return (b.zpos - a.pos);
}
objects.sort(zsort);

demo

as

objectArray.sortOn("zpos",Array.DESCENDING | Array.NUMERIC);
for(var i:uint =0; i < numObjects; i++){
  setChildIndex(objectArray[i], i);
}

processing

processing 3d有其他方法

坐标旋转

js

x1 = xpos * cos(angleZ) - ypos * sin(angleZ);
y1 = ypos * cos(angleZ) - xpos * sin(angleZ);

x1 = xpos * cos(angleY) - zpos * sin(angleY);
z1 = zpos * cos(angleY) + xpos * sin(angleY);

y1 = ypos * cos(angleX) - zpos * sin(angleX);
z1 = zpos * cos(angleX) + ypos * sin(angleX);

demo

as

x1 = xpos * cos(angleZ) - ypos * sin(angleZ);
y1 = ypos * cos(angleZ) + xpos * sin(angleZ);

x1 = xpos * cos(angleY) - zpos * sin(angleY);
z1 = zpos * cos(angleY) + xpos * sin(angleY);

y1 = ypos * cos(angleX) - zpos * sin(angleX);
z1 = zpos * cos(angleX) + ypos * sin(angleX);

processing

processing 3d有其他方法

三维距离

js

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

demo

as

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

processing

dist = sqrt(dx * dx + dy * dy + dz * dz);
    原文作者:kidsamong
    原文地址: https://segmentfault.com/a/1190000002454590
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞