advanced as3 animation

85
ADVANCED AS3 ANIMATION keith peters :: www.bit-101.com

Upload: delfi-ramirez

Post on 18-Nov-2014

514 views

Category:

Documents


1 download

DESCRIPTION

keith peters :: www.bit-101.com

TRANSCRIPT

Page 1: Advanced AS3 Animation

ADVANCED AS3

ANIMATION

keith peters :: www.bit-101.com

Page 2: Advanced AS3 Animation

Foundation ActionScript Animation:

Making Things Move!

Page 3: Advanced AS3 Animation
Page 4: Advanced AS3 Animation

AdvancED ActionScript 3.0 Animation

Page 5: Advanced AS3 Animation

NUMERICAL INTEGRATION

Page 6: Advanced AS3 Animation

Typical Precalculus

Word ProblemAn object is launched

directly upward at 64 feet per second from a platform

80 feet high. What will be the object's maximum height?

When will it attain this height? When will it hit the

ground?

Page 7: Advanced AS3 Animation

Quadratic formula

s(t) = –16t2 + v0t + h0

Page 8: Advanced AS3 Animation
Page 9: Advanced AS3 Animation
Page 10: Advanced AS3 Animation

EULER INTEGRATION

Page 11: Advanced AS3 Animation

Leonhard Euler

(Oiler not You-ler)

Smart old guy

Page 12: Advanced AS3 Animation

Euler Integration

Add acceleration to velocity

Add velocity to position

Repeat

Page 13: Advanced AS3 Animation
Page 14: Advanced AS3 Animation
Page 15: Advanced AS3 Animation
Page 16: Advanced AS3 Animation

oops!

Page 17: Advanced AS3 Animation
Page 18: Advanced AS3 Animation
Page 19: Advanced AS3 Animation

So what else is there?Euler

Backwards Euler

Semi-implicit Euler

Verlet

Velocity Verlet

Beeman’s Algorithm

Midpoint Method

Heun’s Method

Newmark-beta Method

Leapfrog Integration

Monte Carlo Integration

Runge-Kutta Methods

Page 20: Advanced AS3 Animation

So what else is there?

Runge-Kutta Methods

Page 21: Advanced AS3 Animation

RUNGE-KUTTAWicked Smart Old Guys

Carl Runge Wilhelm Kutta

Page 22: Advanced AS3 Animation

People who did NOT invent a numerical integration method

Mark Zuckerberg

Grant Skinner

Nicolas Cannasse(not yet)

Page 23: Advanced AS3 Animation

RUNGE-KUTTA SECOND ORDER INTEGRATION

(or just RK2 if you are cool)

Page 24: Advanced AS3 Animation
Page 25: Advanced AS3 Animation
Page 26: Advanced AS3 Animation
Page 27: Advanced AS3 Animation

// pos1 is current position of object

// vel1 is current velocity of object

accel1 = acceleration(pos1, vel1)

pos2 = pos1 + vel1 * time

vel2 = vel1 + accel1 * time

accel2 = acceleration(pos2, vel2)

pos1 += (vel1 + vel2) / 2 * time

vel1 += (accel1 + accel2) / 2 * time

Page 28: Advanced AS3 Animation

// pos1 is current position of object

// vel1 is current velocity of object

accel1 = acceleration(pos1, vel1)

pos2 = pos1 + vel1 * time

vel2 = vel1 + accel1 * time

accel2 = acceleration(pos2, vel2)

pos1 += (vel1 + vel2) / 2 * time

vel1 += (accel1 + accel2) / 2 * time

Page 29: Advanced AS3 Animation

// pos1 is current position of object

// vel1 is current velocity of object

accel1 = acceleration(pos1, vel1)

pos2 = pos1 + vel1 * time

vel2 = vel1 + accel1 * time

accel2 = acceleration(pos2, vel2)

pos1 += (vel1 + vel2) / 2 * time

vel1 += (accel1 + accel2) / 2 * time

Page 30: Advanced AS3 Animation

// pos1 is current position of object

// vel1 is current velocity of object

accel1 = acceleration(pos1, vel1)

pos2 = pos1 + vel1 * time

vel2 = vel1 + accel1 * time

accel2 = acceleration(pos2, vel2)

pos1 += (vel1 + vel2) / 2 * time

vel1 += (accel1 + accel2) / 2 * time

Page 31: Advanced AS3 Animation
Page 32: Advanced AS3 Animation

// pos1 is current position of object

// vel1 is current velocity of object

accel1 = acceleration(pos1, vel1)

pos2 = pos1 + vel1 * time

vel2 = vel1 + accel1 * time

accel2 = acceleration(pos2, vel2)

pos1 += (vel1 + vel2) / 2 * time

vel1 += (accel1 + accel2) / 2 * time

Page 33: Advanced AS3 Animation
Page 34: Advanced AS3 Animation

var time:int = getTimer();var elapsed:Number = (time - _oldTime) / 1000;_oldTime = time;

var accel1:Point = acceleration(_position, _velocity);

var position2:Point = new Point();position2.x = _position.x + _velocity.x * elapsed;position2.y = _position.y + _velocity.y * elapsed;

var velocity2:Point = new Point();velocity2.x = _velocity.x + accel1.x * elapsed;velocity2.y = _velocity.y + accel1.x * elapsed;

var accel2:Point = acceleration(position2, velocity2);

_position.x += (_velocity.x + velocity2.x) / 2 * elapsed;_position.y += (_velocity.y + velocity2.y) / 2 * elapsed;

_velocity.x += (accel1.x + accel2.x) / 2 * elapsed;_velocity.y += (accel1.y + accel2.y) / 2 * elapsed;

Page 35: Advanced AS3 Animation

var time:int = getTimer();var elapsed:Number = (time - _oldTime) / 1000;_oldTime = time;

var accel1:Point = acceleration(_position, _velocity);

var position2:Point = new Point();position2.x = _position.x + _velocity.x * elapsed;position2.y = _position.y + _velocity.y * elapsed;

var velocity2:Point = new Point();velocity2.x = _velocity.x + accel1.x * elapsed;velocity2.y = _velocity.y + accel1.x * elapsed;

var accel2:Point = acceleration(position2, velocity2);

_position.x += (_velocity.x + velocity2.x) / 2 * elapsed;_position.y += (_velocity.y + velocity2.y) / 2 * elapsed;

_velocity.x += (accel1.x + accel2.x) / 2 * elapsed;_velocity.y += (accel1.y + accel2.y) / 2 * elapsed;

Page 36: Advanced AS3 Animation

var time:int = getTimer();var elapsed:Number = (time - _oldTime) / 1000;_oldTime = time;

var accel1:Point = acceleration(_position, _velocity);

var position2:Point = new Point();position2.x = _position.x + _velocity.x * elapsed;position2.y = _position.y + _velocity.y * elapsed;

var velocity2:Point = new Point();velocity2.x = _velocity.x + accel1.x * elapsed;velocity2.y = _velocity.y + accel1.x * elapsed;

var accel2:Point = acceleration(position2, velocity2);

_position.x += (_velocity.x + velocity2.x) / 2 * elapsed;_position.y += (_velocity.y + velocity2.y) / 2 * elapsed;

_velocity.x += (accel1.x + accel2.x) / 2 * elapsed;_velocity.y += (accel1.y + accel2.y) / 2 * elapsed;

Page 37: Advanced AS3 Animation

var time:int = getTimer();var elapsed:Number = (time - _oldTime) / 1000;_oldTime = time;

var accel1:Point = acceleration(_position, _velocity);

var position2:Point = new Point();position2.x = _position.x + _velocity.x * elapsed;position2.y = _position.y + _velocity.y * elapsed;

var velocity2:Point = new Point();velocity2.x = _velocity.x + accel1.x * elapsed;velocity2.y = _velocity.y + accel1.x * elapsed;

var accel2:Point = acceleration(position2, velocity2);

_position.x += (_velocity.x + velocity2.x) / 2 * elapsed;_position.y += (_velocity.y + velocity2.y) / 2 * elapsed;

_velocity.x += (accel1.x + accel2.x) / 2 * elapsed;_velocity.y += (accel1.y + accel2.y) / 2 * elapsed;

Page 38: Advanced AS3 Animation

var time:int = getTimer();var elapsed:Number = (time - _oldTime) / 1000;_oldTime = time;

var accel1:Point = acceleration(_position, _velocity);

var position2:Point = new Point();position2.x = _position.x + _velocity.x * elapsed;position2.y = _position.y + _velocity.y * elapsed;

var velocity2:Point = new Point();velocity2.x = _velocity.x + accel1.x * elapsed;velocity2.y = _velocity.y + accel1.x * elapsed;

var accel2:Point = acceleration(position2, velocity2);

_position.x += (_velocity.x + velocity2.x) / 2 * elapsed;_position.y += (_velocity.y + velocity2.y) / 2 * elapsed;

_velocity.x += (accel1.x + accel2.x) / 2 * elapsed;_velocity.y += (accel1.y + accel2.y) / 2 * elapsed;

Page 39: Advanced AS3 Animation

var time:int = getTimer();var elapsed:Number = (time - _oldTime) / 1000;_oldTime = time;

var accel1:Point = acceleration(_position, _velocity);

var position2:Point = new Point();position2.x = _position.x + _velocity.x * elapsed;position2.y = _position.y + _velocity.y * elapsed;

var velocity2:Point = new Point();velocity2.x = _velocity.x + accel1.x * elapsed;velocity2.y = _velocity.y + accel1.x * elapsed;

var accel2:Point = acceleration(position2, velocity2);

_position.x += (_velocity.x + velocity2.x) / 2 * elapsed;_position.y += (_velocity.y + velocity2.y) / 2 * elapsed;

_velocity.x += (accel1.x + accel2.x) / 2 * elapsed;_velocity.y += (accel1.y + accel2.y) / 2 * elapsed;

Page 40: Advanced AS3 Animation

RUNGE-KUTTA FOURTH ORDER INTEGRATION

(RK4)

Page 41: Advanced AS3 Animation

RK4: LIKE RK2, BUT WORSE

Page 42: Advanced AS3 Animation

// pos1 is current position of object// vel1 is current velocity of objectacc1 = acceleration(pos1, vel1)

pos2 = pos1 + vel1 / 2 * timevel2 = vel1 + acc1 / 2 * timeacc2 = acceleration(pos2, vel2)

pos3 = pos1 + vel2 / 2 * timevel3 = vel1 + acc2 / 2 * timeacc3 = acceleration(pos3, vel3)

pos4 = pos1 + vel3 * timevel4 = vel1 + acc3 * timeacc3 = acceleration(pos4, vel4)

pos1 += (vel1 + vel2 * 2 + vel3 * 2 + vel4) / 6 * timevel1 += (acc1 + acc2 * 2 + acc3 * 2 + acc4) / 6 * time

Page 43: Advanced AS3 Animation

// pos1 is current position of object// vel1 is current velocity of objectacc1 = acceleration(pos1, vel1)

pos2 = pos1 + vel1 / 2 * timevel2 = vel1 + acc1 / 2 * timeacc2 = acceleration(pos2, vel2)

pos3 = pos1 + vel2 / 2 * timevel3 = vel1 + acc2 / 2 * timeacc3 = acceleration(pos3, vel3)

pos4 = pos1 + vel3 * timevel4 = vel1 + acc3 * timeacc3 = acceleration(pos4, vel4)

pos1 += (vel1 + vel2 * 2 + vel3 * 2 + vel4) / 6 * timevel1 += (acc1 + acc2 * 2 + acc3 * 2 + acc4) / 6 * time

Page 44: Advanced AS3 Animation

// pos1 is current position of object// vel1 is current velocity of objectacc1 = acceleration(pos1, vel1)

pos2 = pos1 + vel1 / 2 * timevel2 = vel1 + acc1 / 2 * timeacc2 = acceleration(pos2, vel2)

pos3 = pos1 + vel2 / 2 * timevel3 = vel1 + acc2 / 2 * timeacc3 = acceleration(pos3, vel3)

pos4 = pos1 + vel3 * timevel4 = vel1 + acc3 * timeacc3 = acceleration(pos4, vel4)

pos1 += (vel1 + vel2 * 2 + vel3 * 2 + vel4) / 6 * timevel1 += (acc1 + acc2 * 2 + acc3 * 2 + acc4) / 6 * time

Page 45: Advanced AS3 Animation

// pos1 is current position of object// vel1 is current velocity of objectacc1 = acceleration(pos1, vel1)

pos2 = pos1 + vel1 / 2 * timevel2 = vel1 + acc1 / 2 * timeacc2 = acceleration(pos2, vel2)

pos3 = pos1 + vel2 / 2 * timevel3 = vel1 + acc2 / 2 * timeacc3 = acceleration(pos3, vel3)

pos4 = pos1 + vel3 * timevel4 = vel1 + acc3 * timeacc3 = acceleration(pos4, vel4)

pos1 += (vel1 + vel2 * 2 + vel3 * 2 + vel4) / 6 * timevel1 += (acc1 + acc2 * 2 + acc3 * 2 + acc4) / 6 * time

Page 46: Advanced AS3 Animation

// pos1 is current position of object// vel1 is current velocity of objectacc1 = acceleration(pos1, vel1)

pos2 = pos1 + vel1 / 2 * timevel2 = vel1 + acc1 / 2 * timeacc2 = acceleration(pos2, vel2)

pos3 = pos1 + vel2 / 2 * timevel3 = vel1 + acc2 / 2 * timeacc3 = acceleration(pos3, vel3)

pos4 = pos1 + vel3 * timevel4 = vel1 + acc3 * timeacc3 = acceleration(pos4, vel4)

pos1 += (vel1 + vel2 * 2 + vel3 * 2 + vel4) / 6 * timevel1 += (acc1 + acc2 * 2 + acc3 * 2 + acc4) / 6 * time

Page 47: Advanced AS3 Animation

var time:int = getTimer();var elapsed:Number = (time - _oldTime) / 1000;_oldTime = time;

var accel1:Point = acceleration(_position, _velocity);

var position2:Point = new Point();position2.x = _position.x + _velocity.x / 2 * elapsed;position2.y = _position.y + _velocity.y / 2 * elapsed;

var velocity2:Point = new Point();velocity2.x = _velocity.x + accel1.x / 2 * elapsed;velocity2.y = _velocity.y + accel1.x / 2 * elapsed;

var accel2:Point = acceleration(position2, velocity2);

var position3:Point = new Point();position3.x = _position.x + velocity2.x / 2 * elapsed;position3.y = _position.y + velocity2.y / 2 * elapsed;

var velocity3:Point = new Point();velocity3.x = _velocity.x + accel2.x / 2 * elapsed;velocity3.y = _velocity.y + accel2.y / 2 * elapsed;

var accel3:Point = acceleration(position3, velocity3);

var position4:Point = new Point();position4.x = _position.x + velocity3.x * elapsed;position4.y = _position.y + velocity3.y * elapsed;

var velocity4:Point = new Point();velocity4.x = _velocity.x + accel3.x * elapsed;velocity4.y = _velocity.y + accel3.y * elapsed;

var accel4:Point = acceleration(position4, velocity4);

_position.x += (_velocity.x + 2 * velocity2.x + 2 * velocity3.x + velocity4.x) / 6 * elapsed;_position.y += (_velocity.y + 2 * velocity2.y + 2 * velocity3.y + velocity4.y) / 6 * elapsed;

_velocity.x += (accel1.x + 2 * accel2.x + 2 * accel3.x + accel4.x) / 6 * elapsed;_velocity.y += (accel1.y + 2 * accel2.y + 2 * accel3.y + accel4.y) / 6 * elapsed;

Page 48: Advanced AS3 Animation

BEHOLD THE AMAZING RESULTS!!!

Page 49: Advanced AS3 Animation

Runge-Kutta Summary

Not 100% accurate, but probably more accurate than you’ll ever need.

You probably don’t need it.

If you do need it, you probably already know you need it.

If you are wondering whether you need it or not, you probably don’t.

If you do need it, you need it.

Page 50: Advanced AS3 Animation

VERLET INTEGRATION

Page 51: Advanced AS3 Animation

Loup Verlet Old guys FTW!

Page 52: Advanced AS3 Animation

Verlet Integration

Developed for molecular interactions.

Extremely stable.

Velocity is not explicitly stored.

Page 53: Advanced AS3 Animation

Advanced Character

Physicsby Thomas Jakobsen

available atwww.gamasutra.com

Page 54: Advanced AS3 Animation

Verlet System

Points

Constraints

Sticks

Structures

Hinges

Page 55: Advanced AS3 Animation

VERLET POINTS

Page 56: Advanced AS3 Animation

It moved this far

in the last frame.

Velocity

Page 57: Advanced AS3 Animation

So we’ll move it that far

on the next frame.

Page 58: Advanced AS3 Animation

Thus, any change in position

becomes a change in velocity.

Page 59: Advanced AS3 Animation

temp = currentPos

velocity = currentPos - oldPos

currentPos += velocity

oldPos = temp

Page 60: Advanced AS3 Animation

VerletPointCLASS

Page 61: Advanced AS3 Animation

package{ import flash.display.Graphics; import flash.geom.Rectangle;

public class VerletPoint { public var x:Number; public var y:Number; private var _oldX:Number; private var _oldY:Number;

public function VerletPoint(x:Number, y:Number) { setPosition(x, y); } public function update():void { var tempX:Number = x; var tempY:Number = y; x += vx; y += vy; _oldX = tempX; _oldY = tempY; } public function setPosition(x:Number, y:Number):void { this.x = _oldX = x; this.y = _oldY = y; } public function constrain(rect:Rectangle):void { x = Math.max(rect.left, Math.min(rect.right, x)); y = Math.max(rect.top, Math.min(rect.bottom, y)); } public function set vx(value:Number):void { _oldX = x - value; }

public function get vx():Number { return x - _oldX; } public function set vy(value:Number):void { _oldY = y - value; }

public function get vy():Number { return y - _oldY; } public function render(g:Graphics):void { g.beginFill(0); g.drawCircle(x, y, 4); g.endFill(); }

}}

Page 62: Advanced AS3 Animation

public function update():void

{

var tempX:Number = x;

var tempY:Number = y;

x += vx;

y += vy;

_oldX = tempX;

_oldY = tempY;

}

Page 63: Advanced AS3 Animation

Verlet Constraints

Page 64: Advanced AS3 Animation

Verlet Constraints

Page 65: Advanced AS3 Animation

package{ import flash.display.Graphics; import flash.geom.Rectangle;

public class VerletPoint { public var x:Number; public var y:Number; private var _oldX:Number; private var _oldY:Number;

public function VerletPoint(x:Number, y:Number) { setPosition(x, y); } public function update():void { var tempX:Number = x; var tempY:Number = y; x += vx; y += vy; _oldX = tempX; _oldY = tempY; } public function setPosition(x:Number, y:Number):void { this.x = _oldX = x; this.y = _oldY = y; } public function constrain(rect:Rectangle):void { x = Math.max(rect.left, Math.min(rect.right, x)); y = Math.max(rect.top, Math.min(rect.bottom, y)); } public function set vx(value:Number):void { _oldX = x - value; }

public function get vx():Number { return x - _oldX; } public function set vy(value:Number):void { _oldY = y - value; }

public function get vy():Number { return y - _oldY; } public function render(g:Graphics):void { g.beginFill(0); g.drawCircle(x, y, 4); g.endFill(); }

}}

Page 66: Advanced AS3 Animation

public function constrain(rect:Rectangle):void{ x = Math.max(rect.left, Math.min(rect.right, x)); y = Math.max(rect.top, Math.min(rect.bottom, y));}

Page 67: Advanced AS3 Animation

VerletPoint

Demo

Page 68: Advanced AS3 Animation

Stick

Verlet Sticks

Page 69: Advanced AS3 Animation

Verlet Sticks

Page 70: Advanced AS3 Animation

VerletStickCLASS

Page 71: Advanced AS3 Animation

package

{

import flash.display.Graphics;

public class VerletStick

{

private var _pointA:VerletPoint;

private var _pointB:VerletPoint;

private var _length:Number;

public function VerletStick(pointA:VerletPoint,

pointB:VerletPoint,

length:Number = -1)

{

_pointA = pointA;

_pointB = pointB;

if(length == -1)

{

var dx:Number = _pointA.x - _pointB.x;

var dy:Number = _pointA.y - _pointB.y;

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

}

else

{

_length = length;

}

}

public function update():void { var dx:Number = _pointB.x - _pointA.x; var dy:Number = _pointB.y - _pointA.y; var dist:Number = Math.sqrt(dx * dx + dy * dy); var diff:Number = _length - dist; var offsetX:Number = (diff * dx / dist) / 2; var offsetY:Number = (diff * dy / dist) / 2; _pointA.x -= offsetX; _pointA.y -= offsetY; _pointB.x += offsetX; _pointB.y += offsetY; }

public function render(g:Graphics):void

{

g.lineStyle(0);

g.moveTo(_pointA.x, _pointA.y);

g.lineTo(_pointB.x, _pointB.y);

}

}

}

Page 72: Advanced AS3 Animation

public function update():void{ var dx:Number = _pointB.x - _pointA.x; var dy:Number = _pointB.y - _pointA.y; var dist:Number = Math.sqrt(dx * dx + dy * dy);

var diff:Number = _length - dist;

var offsetX:Number = (diff * dx / dist) / 2; var offsetY:Number = (diff * dy / dist) / 2;

_pointA.x -= offsetX; _pointA.y -= offsetY;

_pointB.x += offsetX; _pointB.y += offsetY;}

Page 73: Advanced AS3 Animation

public function update():void{ var dx:Number = _pointB.x - _pointA.x; var dy:Number = _pointB.y - _pointA.y; var dist:Number = Math.sqrt(dx * dx + dy * dy);

var diff:Number = _length - dist;

var offsetX:Number = (diff * dx / dist) / 2; var offsetY:Number = (diff * dy / dist) / 2;

_pointA.x -= offsetX; _pointA.y -= offsetY;

_pointB.x += offsetX; _pointB.y += offsetY;}

Page 74: Advanced AS3 Animation

public function update():void{ var dx:Number = _pointB.x - _pointA.x; var dy:Number = _pointB.y - _pointA.y; var dist:Number = Math.sqrt(dx * dx + dy * dy);

var diff:Number = _length - dist;

var offsetX:Number = (diff * dx / dist) / 2; var offsetY:Number = (diff * dy / dist) / 2;

_pointA.x -= offsetX; _pointA.y -= offsetY;

_pointB.x += offsetX; _pointB.y += offsetY;}

Page 75: Advanced AS3 Animation

VerletStick

Demo

Page 76: Advanced AS3 Animation

Verlet Structures

Page 77: Advanced AS3 Animation

Triangle

Demo

Page 78: Advanced AS3 Animation

ITERATIONDealing With Multiple Constraints

Page 79: Advanced AS3 Animation
Page 80: Advanced AS3 Animation
Page 81: Advanced AS3 Animation
Page 82: Advanced AS3 Animation
Page 83: Advanced AS3 Animation

updatePoints()

for(var i = 0; i < iter; i++){

handleConstraints() updateSticks()

}

Page 84: Advanced AS3 Animation

Verlet Hinges