摘要:收藏自周文彬買了一本動畫基礎來看,里面很多數學和物理知識,跟著里面的例子學習了一遍。思路是核心,其他都換湯不換藥,所以我打算把跟動畫有關的數學和物理知識整理下,幾種不同的語言整理在一起方便比較,也方便以后用的時候查找。
(收藏自周文彬)
買了一本《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);
文章版權歸作者所有,未經允許請勿轉載,若此文章存在違規行為,您可以聯系管理員刪除。
轉載請注明本文地址:http://specialneedsforspecialkids.com/yun/85463.html
摘要:在兩點間的關系用夾角和距離很容易表示時,極坐標系便顯得尤為有用而在平面直角坐標系中,這樣的關系就只能使用三角函數來表示。對于很多類型的曲線,極坐標方程是最簡單的表達形式,甚至對于某些曲線來說,只有極坐標方程能夠表示。 歡迎大家前往騰訊云+社區,獲取更多騰訊海量技術實踐干貨哦~ 本文由郭詩雅發表于云+社區專欄 在數學中,極坐標系(英語:Polar coordinate system)是...
摘要:本篇則會介紹瀏覽器的視圖與坐標。返回值為視覺視口的縮放比例視覺視口寬度,返回值為像素值。那么接下來我們來了解一下瀏覽器中的坐標系系統。在數學里,笛卡爾坐標系英語,也稱直角坐標系,是一種正交坐標系。 作者:陳大魚頭 github: KRISACHAN 在上一篇【Hello CSS】的第二章第二章-CSS的邏輯屬性與盒子模型中提了個問題: 為什么Flex box跟Grid box的是...
摘要:貝塞爾曲線貝塞爾曲線是應用于二維圖形應用程序的數學曲線。通過調整控制點,貝塞爾曲線的形狀會發生變化。讓我們看看貝塞爾曲線的工作原理。貝塞爾曲線需要四個值,或者更準確地說它需要兩對數字。每對描述立方貝塞爾曲線控制點的和坐標。 這是專門探索 JavaScript 及其所構建的組件的系列文章的第 13 篇。 如果你錯過了前面的章節,可以在這里找到它們: JavaScript 是如何工作的:...
摘要:動畫原文通常,框架會為你處理動畫。整個的動畫過程被分成了很小的步驟,每一個步驟被定時器調用。因為定時器的周期非常短,所以動畫看起來是連續的。已經過去的動畫時間作為分子,計算每一幀通過公式。線性的使動畫以固定的速度進行。 動畫 原文:http://javascript.info/tutori... 通常,框架會為你處理動畫。但是,你可能想知道僅僅用javascript怎么來實現動畫,和可...
閱讀 3400·2021-11-24 10:30
閱讀 3269·2021-11-22 15:29
閱讀 3706·2021-10-28 09:32
閱讀 1255·2021-09-07 10:22
閱讀 3336·2019-08-30 15:55
閱讀 3619·2019-08-30 15:54
閱讀 3494·2019-08-30 15:54
閱讀 2833·2019-08-30 15:44