资讯专栏INFORMATION COLUMN

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

CNZPH / 1537人阅读

摘要:收藏自周文彬买了一本动画基础来看,里面很多数学和物理知识,跟着里面的例子学习了一遍。思路是核心,其他都换汤不换药,所以我打算把跟动画有关的数学和物理知识整理下,几种不同的语言整理在一起方便比较,也方便以后用的时候查找。

(收藏自周文彬)

买了一本《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);

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/85463.html

相关文章

  • 从小数学就不及格我,竟然用极坐标系表白了我女神!(附代码)

    摘要:在两点间的关系用夹角和距离很容易表示时,极坐标系便显得尤为有用而在平面直角坐标系中,这样的关系就只能使用三角函数来表示。对于很多类型的曲线,极坐标方程是最简单的表达形式,甚至对于某些曲线来说,只有极坐标方程能够表示。 欢迎大家前往腾讯云+社区,获取更多腾讯海量技术实践干货哦~ 本文由郭诗雅发表于云+社区专栏 在数学中,极坐标系(英语:Polar coordinate system)是...

    yagami 评论0 收藏0
  • 【Hello CSS】第三章-浏览器视图与坐标

    摘要:本篇则会介绍浏览器的视图与坐标。返回值为视觉视口的缩放比例视觉视口宽度,返回值为像素值。那么接下来我们来了解一下浏览器中的坐标系系统。在数学里,笛卡尔坐标系英语,也称直角坐标系,是一种正交坐标系。 作者:陈大鱼头 github: KRISACHAN 在上一篇【Hello CSS】的第二章第二章-CSS的逻辑属性与盒子模型中提了个问题: 为什么Flex box跟Grid box的是...

    Jeffrrey 评论0 收藏0
  • JavaScript是如何工作: CSS JS 动画底层原理及如何优化它们性能

    摘要:贝塞尔曲线贝塞尔曲线是应用于二维图形应用程序的数学曲线。通过调整控制点,贝塞尔曲线的形状会发生变化。让我们看看贝塞尔曲线的工作原理。贝塞尔曲线需要四个值,或者更准确地说它需要两对数字。每对描述立方贝塞尔曲线控制点的和坐标。 这是专门探索 JavaScript 及其所构建的组件的系列文章的第 13 篇。 如果你错过了前面的章节,可以在这里找到它们: JavaScript 是如何工作的:...

    darcrand 评论0 收藏0
  • 【翻译】javascript 动画原理浅析

    摘要:动画原文通常,框架会为你处理动画。整个的动画过程被分成了很小的步骤,每一个步骤被定时器调用。因为定时器的周期非常短,所以动画看起来是连续的。已经过去的动画时间作为分子,计算每一帧通过公式。线性的使动画以固定的速度进行。 动画 原文:http://javascript.info/tutori... 通常,框架会为你处理动画。但是,你可能想知道仅仅用javascript怎么来实现动画,和可...

    Worktile 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<