Useful Formulas

Throughout the book, Ive presented various formulas relating to the different motion and effects youve been creating. Ive tried to distill the most useful, and most used, formulas, equations, and code snippets, and list these at the end of each chapter. I also thought it would be useful to have these all in one place, so Ive gathered them all together here as a one-shot reference to those things I think youll need to use the most. I know that I, for one, will have a bookmark on this page.

Chapter 3

Basic trigonometric functions:

 sine of angle = opposite / hypotenuse cosine of angle = adjacent / hypotenuse tangent of angle = opposite / adjacent 

Radian/degree conversion:

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

Rotating to the mouse (or any point):

 // substitute _xmouse, _ymouse with the x, y point to rotate to var dx = _xmouse - movieclip._x; var dy = _ymouse - movieclip._y; movieclip._rotation = Math.atan2(dy, dx) * 180 / Math.PI; 

Waves:

 // assign value to _x, _y or other property of movie clip, // use as drawing coordinates, etc. // note: angle does not have to be zero, // but must be defined as something prior to adding speed angle = 0; onEnterFrame = function(){    value = center + Math.sin(angle) * range;    angle += speed; } 

Circles:

 // assign position to _x and _y of movie clip, // use as drawing coordinates, etc. onEnterFrame = function(){    xposition = centerX + Math.cos(angle) * radius;    yposition = centerY + Math.sin(angle) * radius;    angle += speed; } 

Ovals:

 // assign position to _x and _y of movie clip, // use as drawing coordinates, etc. onEnterFrame = function(){    xposition = centerX + Math.cos(angle) * radiusX;    yposition = centerY + Math.sin(angle) * radiusY;    angle += speed; } 

Distance between two points:

 // points are x1,y1 and x2,y2 // can be movie clip positions, mouse coordinates, etc. dx = x2  x1; dy = y2  y1; dist = Math.sqrt(dx*dx + dy*dy); 

Chapter 4

Converting hex to decimal:

 trace(hexValue); 

Converting decimal to hex:

 trace(decimalValue.toString(16)); 

Combining component colors:

 color24 = red << 16  green << 8  blue; 

Extracting component colors:

 red = color24 >> 16; green = color24 >> 8 & 0xFF; blue = color24 & 0xFF; 

Drawing a curve through a point:

 // xt, yt is the point you want to draw through // x0, y0 and x2, y2 are the end points of the curve x1 = xt * 2  (x0 + x2) / 2; y1 = yt * 2  (y0 + y2) / 2; moveTo(x0, y0); curveTo(x1, y1, x2, y2); 

Chapter 5

Convert angular velocity to x, y velocity:

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

Convert angular acceleration (any force acting on an object) to x, y acceleration:

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

Add acceleration to velocity:

 vx += ax; vy += ay; 

Add velocity to position:

 movieclip._x += vx; movieclip._y += vy; 

Chapter 6

Remove an out-of-bounds object:

 if(mc._x - mc._width / 2 > right     mc._x + mc._width / 2 < left     mc._y  mc._height / 2 > bottom     mc._y + mc._height / 2 < top) {       mc.removeMovieClip(); } 

Regenerate an out-of-bounds object:

 if(mc._x - mc._width / 2 > right     mc._x + mc._width / 2 < left     mc._y  mc._height / 2 > bottom     mc._y + mc._height / 2 < top) {       // reset mc position and velocity. } 

Screen wrapping for an out-of-bounds object:

 if(mc._x - mc._width / 2 > right) {    mc._x = left - mc._width / 2; } else if(mc._x + mc._width / 2 < left) {    mc._x = right + mc._width / 2; } if(mc._y  mc._height / 2 > bottom) {    mc._y = top  mc._height / 2; } else if(mc._y + mc._height / 2 < top) {    mc._y = bottom + mc._height / 2; } 

Apply friction (the correct way):

 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; 

Apply friction (the easy way):

 vx *= friction; vy *= friction; 

Chapter 8

Simple easing, long form:

 var dx:Number = targetX - movieclip._x; var dy:Number = targetY - movieclip._y; vx = dx * easing; vy = dy * easing; movieclip._x += vx; movieclip._y += vy; 

Simple easing, abbreviated form:

 vx = (targetX - movieclip._x) * easing; vy = (targetY - movieclip._y) * easing; movieclip._x += vx; movieclip._y += vy; 

Simple easing, short form:

 movieclip._x += (targetX - movieclip._x) * easing; movieclip._y += (targetY - movieclip._y) * easing; 

Simple spring, long form:

 var ax:Number = (targetX - movieclip._x) * spring; var ay:Number = (targetY - movieclip._y) * spring; vx += ax; vy += ay; vx *= friction; vy *= friction; movieclip._x += vx; movieclip._y += vy; 

Simple spring, abbreviated form:

 vx += (targetX - movieclip._x) * spring; vy += (targetY - movieclip._y) * spring; vx *= friction; vy *= friction; movieclip._x += vx; movieclip._y += vy; 

Simple spring, short form:

 vx += (targetX - movieclip._x) * spring; vy += (targetY - movieclip._y) * spring; movieclip._x += (vx *= friction); movieclip._y += (vy *= friction); 

Offset spring:

 var dx:Number = movieclip._x - fixedX; var dy:Number = movieclip._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; // spring to targetX, targetY as above 

Chapter 9

Distance-based collision detection:

 // starting with mcA and mcB var dx:Number = mcB._x  mcA._x; var dy:Number = mcB._y  mcA._y; var dist:Number = Math.sqrt(dx*dx+dy*dy); if(dist < mcA._width / 2 + mcB._width / 2) {       // handle collision } 

Multiple object collision detection:

 var numObjects:Number = 10; // for example for(var i:Number = 0; i<numObjects-1; i++) {       // evaluate reference using variable i. For example:      var objectA = this["object" + i];       for(var j:Number = i+1; j<numObjects;j++)      {            // evaluate reference using j. For example:            var objectB = this["object" + j];            // perform collision detection            // between objectA and objectB      } } 

Chapter 10

Coordinate rotation:

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

Reverse coordinate rotation:

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

Chapter 11

Of course, the big one is the formula for conservation of momentum. In straight mathematical terms:

 (m0  m1) * v0 + 2 * m1 * v1 v0Final = ----------------------------                  m0 + m1           (m1  m0) * v1 + 2 * m0 * v0 v1Final = ----------------------------                  m0 + m1 

And in ActionScript, with a shortcut:

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

Chapter 12

Basic formula for gravity:

 force = G * m1 * m2 / distance  2  

ActionScript-friendly implementation in full:

 function gravitate(partA:MovieClip, partB:MovieClip):Void {       var dx:Number = partB._x - partA._x;       var dy:Number = partB._y - partA._y;       var distSQ:Number = dx*dx + dy*dy;       var dist:Number = Math.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 += ay / partA.mass;       partB.vx -= ax / partB.mass;       partB.vy -= ay / partB.mass; } 

Chapter 14

Law of Cosines:

 a  2  = b  2  + c  2    2 * b * c * cos A b  2  = a  2  + c  2    2 * a * c * cos B c  2  = a  2  + b  2    2 * a * b * cos C 

And in ActionScript:

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

Chapter 15

Basic perspective:

 scale = fl / (fl + z); mc._xscale = mc._yscale = scale * 100; mc._alpha = scale * 100;      // optional mc._x = vanishingPointX + x * scale; mc._y = vanishingPointY + y * scale; mc.swapDepths(-z);            // optional 

Coordinate rotation:

 x1 = cos(angleZ) * x  sin(angleZ) * y; y1 = cos(angleZ) * y + sin(angleZ) * x; x1 = cos(angleY) * x  sin(angleY) * z; z1 = cos(angleY) * z + sin(angleY) * x; y1 = cos(angleX) * y  sin(angleX) * z; z1 = cos(angleX) * z + sin(angleX) * y; 

3D distance:

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


Foundation ActionScript. Animation. Making Things Move
Foundation Actionscript 3.0 Animation: Making Things Move!
ISBN: 1590597915
EAN: 2147483647
Year: 2005
Pages: 137
Authors: Keith Peters

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net