advanced as3 animation
DESCRIPTION
keith peters :: www.bit-101.comTRANSCRIPT
![Page 1: Advanced AS3 Animation](https://reader033.vdocuments.us/reader033/viewer/2022061119/546ab4b3af795967298b4888/html5/thumbnails/1.jpg)
ADVANCED AS3
ANIMATION
keith peters :: www.bit-101.com
![Page 2: Advanced AS3 Animation](https://reader033.vdocuments.us/reader033/viewer/2022061119/546ab4b3af795967298b4888/html5/thumbnails/2.jpg)
Foundation ActionScript Animation:
Making Things Move!
![Page 3: Advanced AS3 Animation](https://reader033.vdocuments.us/reader033/viewer/2022061119/546ab4b3af795967298b4888/html5/thumbnails/3.jpg)
![Page 4: Advanced AS3 Animation](https://reader033.vdocuments.us/reader033/viewer/2022061119/546ab4b3af795967298b4888/html5/thumbnails/4.jpg)
AdvancED ActionScript 3.0 Animation
![Page 5: Advanced AS3 Animation](https://reader033.vdocuments.us/reader033/viewer/2022061119/546ab4b3af795967298b4888/html5/thumbnails/5.jpg)
NUMERICAL INTEGRATION
![Page 6: Advanced AS3 Animation](https://reader033.vdocuments.us/reader033/viewer/2022061119/546ab4b3af795967298b4888/html5/thumbnails/6.jpg)
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](https://reader033.vdocuments.us/reader033/viewer/2022061119/546ab4b3af795967298b4888/html5/thumbnails/7.jpg)
Quadratic formula
s(t) = –16t2 + v0t + h0
![Page 8: Advanced AS3 Animation](https://reader033.vdocuments.us/reader033/viewer/2022061119/546ab4b3af795967298b4888/html5/thumbnails/8.jpg)
![Page 9: Advanced AS3 Animation](https://reader033.vdocuments.us/reader033/viewer/2022061119/546ab4b3af795967298b4888/html5/thumbnails/9.jpg)
![Page 10: Advanced AS3 Animation](https://reader033.vdocuments.us/reader033/viewer/2022061119/546ab4b3af795967298b4888/html5/thumbnails/10.jpg)
EULER INTEGRATION
![Page 11: Advanced AS3 Animation](https://reader033.vdocuments.us/reader033/viewer/2022061119/546ab4b3af795967298b4888/html5/thumbnails/11.jpg)
Leonhard Euler
(Oiler not You-ler)
Smart old guy
![Page 12: Advanced AS3 Animation](https://reader033.vdocuments.us/reader033/viewer/2022061119/546ab4b3af795967298b4888/html5/thumbnails/12.jpg)
Euler Integration
Add acceleration to velocity
Add velocity to position
Repeat
![Page 13: Advanced AS3 Animation](https://reader033.vdocuments.us/reader033/viewer/2022061119/546ab4b3af795967298b4888/html5/thumbnails/13.jpg)
![Page 14: Advanced AS3 Animation](https://reader033.vdocuments.us/reader033/viewer/2022061119/546ab4b3af795967298b4888/html5/thumbnails/14.jpg)
![Page 15: Advanced AS3 Animation](https://reader033.vdocuments.us/reader033/viewer/2022061119/546ab4b3af795967298b4888/html5/thumbnails/15.jpg)
![Page 16: Advanced AS3 Animation](https://reader033.vdocuments.us/reader033/viewer/2022061119/546ab4b3af795967298b4888/html5/thumbnails/16.jpg)
oops!
![Page 17: Advanced AS3 Animation](https://reader033.vdocuments.us/reader033/viewer/2022061119/546ab4b3af795967298b4888/html5/thumbnails/17.jpg)
![Page 18: Advanced AS3 Animation](https://reader033.vdocuments.us/reader033/viewer/2022061119/546ab4b3af795967298b4888/html5/thumbnails/18.jpg)
![Page 19: Advanced AS3 Animation](https://reader033.vdocuments.us/reader033/viewer/2022061119/546ab4b3af795967298b4888/html5/thumbnails/19.jpg)
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](https://reader033.vdocuments.us/reader033/viewer/2022061119/546ab4b3af795967298b4888/html5/thumbnails/20.jpg)
So what else is there?
Runge-Kutta Methods
![Page 21: Advanced AS3 Animation](https://reader033.vdocuments.us/reader033/viewer/2022061119/546ab4b3af795967298b4888/html5/thumbnails/21.jpg)
RUNGE-KUTTAWicked Smart Old Guys
Carl Runge Wilhelm Kutta
![Page 22: Advanced AS3 Animation](https://reader033.vdocuments.us/reader033/viewer/2022061119/546ab4b3af795967298b4888/html5/thumbnails/22.jpg)
People who did NOT invent a numerical integration method
Mark Zuckerberg
Grant Skinner
Nicolas Cannasse(not yet)
![Page 23: Advanced AS3 Animation](https://reader033.vdocuments.us/reader033/viewer/2022061119/546ab4b3af795967298b4888/html5/thumbnails/23.jpg)
RUNGE-KUTTA SECOND ORDER INTEGRATION
(or just RK2 if you are cool)
![Page 24: Advanced AS3 Animation](https://reader033.vdocuments.us/reader033/viewer/2022061119/546ab4b3af795967298b4888/html5/thumbnails/24.jpg)
![Page 25: Advanced AS3 Animation](https://reader033.vdocuments.us/reader033/viewer/2022061119/546ab4b3af795967298b4888/html5/thumbnails/25.jpg)
![Page 26: Advanced AS3 Animation](https://reader033.vdocuments.us/reader033/viewer/2022061119/546ab4b3af795967298b4888/html5/thumbnails/26.jpg)
![Page 27: Advanced AS3 Animation](https://reader033.vdocuments.us/reader033/viewer/2022061119/546ab4b3af795967298b4888/html5/thumbnails/27.jpg)
// 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](https://reader033.vdocuments.us/reader033/viewer/2022061119/546ab4b3af795967298b4888/html5/thumbnails/28.jpg)
// 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](https://reader033.vdocuments.us/reader033/viewer/2022061119/546ab4b3af795967298b4888/html5/thumbnails/29.jpg)
// 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](https://reader033.vdocuments.us/reader033/viewer/2022061119/546ab4b3af795967298b4888/html5/thumbnails/30.jpg)
// 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](https://reader033.vdocuments.us/reader033/viewer/2022061119/546ab4b3af795967298b4888/html5/thumbnails/31.jpg)
![Page 32: Advanced AS3 Animation](https://reader033.vdocuments.us/reader033/viewer/2022061119/546ab4b3af795967298b4888/html5/thumbnails/32.jpg)
// 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](https://reader033.vdocuments.us/reader033/viewer/2022061119/546ab4b3af795967298b4888/html5/thumbnails/33.jpg)
![Page 34: Advanced AS3 Animation](https://reader033.vdocuments.us/reader033/viewer/2022061119/546ab4b3af795967298b4888/html5/thumbnails/34.jpg)
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](https://reader033.vdocuments.us/reader033/viewer/2022061119/546ab4b3af795967298b4888/html5/thumbnails/35.jpg)
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](https://reader033.vdocuments.us/reader033/viewer/2022061119/546ab4b3af795967298b4888/html5/thumbnails/36.jpg)
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](https://reader033.vdocuments.us/reader033/viewer/2022061119/546ab4b3af795967298b4888/html5/thumbnails/37.jpg)
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](https://reader033.vdocuments.us/reader033/viewer/2022061119/546ab4b3af795967298b4888/html5/thumbnails/38.jpg)
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](https://reader033.vdocuments.us/reader033/viewer/2022061119/546ab4b3af795967298b4888/html5/thumbnails/39.jpg)
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](https://reader033.vdocuments.us/reader033/viewer/2022061119/546ab4b3af795967298b4888/html5/thumbnails/40.jpg)
RUNGE-KUTTA FOURTH ORDER INTEGRATION
(RK4)
![Page 41: Advanced AS3 Animation](https://reader033.vdocuments.us/reader033/viewer/2022061119/546ab4b3af795967298b4888/html5/thumbnails/41.jpg)
RK4: LIKE RK2, BUT WORSE
![Page 42: Advanced AS3 Animation](https://reader033.vdocuments.us/reader033/viewer/2022061119/546ab4b3af795967298b4888/html5/thumbnails/42.jpg)
// 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](https://reader033.vdocuments.us/reader033/viewer/2022061119/546ab4b3af795967298b4888/html5/thumbnails/43.jpg)
// 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](https://reader033.vdocuments.us/reader033/viewer/2022061119/546ab4b3af795967298b4888/html5/thumbnails/44.jpg)
// 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](https://reader033.vdocuments.us/reader033/viewer/2022061119/546ab4b3af795967298b4888/html5/thumbnails/45.jpg)
// 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](https://reader033.vdocuments.us/reader033/viewer/2022061119/546ab4b3af795967298b4888/html5/thumbnails/46.jpg)
// 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](https://reader033.vdocuments.us/reader033/viewer/2022061119/546ab4b3af795967298b4888/html5/thumbnails/47.jpg)
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](https://reader033.vdocuments.us/reader033/viewer/2022061119/546ab4b3af795967298b4888/html5/thumbnails/48.jpg)
BEHOLD THE AMAZING RESULTS!!!
![Page 49: Advanced AS3 Animation](https://reader033.vdocuments.us/reader033/viewer/2022061119/546ab4b3af795967298b4888/html5/thumbnails/49.jpg)
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](https://reader033.vdocuments.us/reader033/viewer/2022061119/546ab4b3af795967298b4888/html5/thumbnails/50.jpg)
VERLET INTEGRATION
![Page 51: Advanced AS3 Animation](https://reader033.vdocuments.us/reader033/viewer/2022061119/546ab4b3af795967298b4888/html5/thumbnails/51.jpg)
Loup Verlet Old guys FTW!
![Page 52: Advanced AS3 Animation](https://reader033.vdocuments.us/reader033/viewer/2022061119/546ab4b3af795967298b4888/html5/thumbnails/52.jpg)
Verlet Integration
Developed for molecular interactions.
Extremely stable.
Velocity is not explicitly stored.
![Page 53: Advanced AS3 Animation](https://reader033.vdocuments.us/reader033/viewer/2022061119/546ab4b3af795967298b4888/html5/thumbnails/53.jpg)
Advanced Character
Physicsby Thomas Jakobsen
available atwww.gamasutra.com
![Page 54: Advanced AS3 Animation](https://reader033.vdocuments.us/reader033/viewer/2022061119/546ab4b3af795967298b4888/html5/thumbnails/54.jpg)
Verlet System
Points
Constraints
Sticks
Structures
Hinges
![Page 55: Advanced AS3 Animation](https://reader033.vdocuments.us/reader033/viewer/2022061119/546ab4b3af795967298b4888/html5/thumbnails/55.jpg)
VERLET POINTS
![Page 56: Advanced AS3 Animation](https://reader033.vdocuments.us/reader033/viewer/2022061119/546ab4b3af795967298b4888/html5/thumbnails/56.jpg)
It moved this far
in the last frame.
Velocity
![Page 57: Advanced AS3 Animation](https://reader033.vdocuments.us/reader033/viewer/2022061119/546ab4b3af795967298b4888/html5/thumbnails/57.jpg)
So we’ll move it that far
on the next frame.
![Page 58: Advanced AS3 Animation](https://reader033.vdocuments.us/reader033/viewer/2022061119/546ab4b3af795967298b4888/html5/thumbnails/58.jpg)
Thus, any change in position
becomes a change in velocity.
![Page 59: Advanced AS3 Animation](https://reader033.vdocuments.us/reader033/viewer/2022061119/546ab4b3af795967298b4888/html5/thumbnails/59.jpg)
temp = currentPos
velocity = currentPos - oldPos
currentPos += velocity
oldPos = temp
![Page 60: Advanced AS3 Animation](https://reader033.vdocuments.us/reader033/viewer/2022061119/546ab4b3af795967298b4888/html5/thumbnails/60.jpg)
VerletPointCLASS
![Page 61: Advanced AS3 Animation](https://reader033.vdocuments.us/reader033/viewer/2022061119/546ab4b3af795967298b4888/html5/thumbnails/61.jpg)
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](https://reader033.vdocuments.us/reader033/viewer/2022061119/546ab4b3af795967298b4888/html5/thumbnails/62.jpg)
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](https://reader033.vdocuments.us/reader033/viewer/2022061119/546ab4b3af795967298b4888/html5/thumbnails/63.jpg)
Verlet Constraints
![Page 64: Advanced AS3 Animation](https://reader033.vdocuments.us/reader033/viewer/2022061119/546ab4b3af795967298b4888/html5/thumbnails/64.jpg)
Verlet Constraints
![Page 65: Advanced AS3 Animation](https://reader033.vdocuments.us/reader033/viewer/2022061119/546ab4b3af795967298b4888/html5/thumbnails/65.jpg)
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](https://reader033.vdocuments.us/reader033/viewer/2022061119/546ab4b3af795967298b4888/html5/thumbnails/66.jpg)
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](https://reader033.vdocuments.us/reader033/viewer/2022061119/546ab4b3af795967298b4888/html5/thumbnails/67.jpg)
VerletPoint
Demo
![Page 68: Advanced AS3 Animation](https://reader033.vdocuments.us/reader033/viewer/2022061119/546ab4b3af795967298b4888/html5/thumbnails/68.jpg)
Stick
Verlet Sticks
![Page 69: Advanced AS3 Animation](https://reader033.vdocuments.us/reader033/viewer/2022061119/546ab4b3af795967298b4888/html5/thumbnails/69.jpg)
Verlet Sticks
![Page 70: Advanced AS3 Animation](https://reader033.vdocuments.us/reader033/viewer/2022061119/546ab4b3af795967298b4888/html5/thumbnails/70.jpg)
VerletStickCLASS
![Page 71: Advanced AS3 Animation](https://reader033.vdocuments.us/reader033/viewer/2022061119/546ab4b3af795967298b4888/html5/thumbnails/71.jpg)
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](https://reader033.vdocuments.us/reader033/viewer/2022061119/546ab4b3af795967298b4888/html5/thumbnails/72.jpg)
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](https://reader033.vdocuments.us/reader033/viewer/2022061119/546ab4b3af795967298b4888/html5/thumbnails/73.jpg)
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](https://reader033.vdocuments.us/reader033/viewer/2022061119/546ab4b3af795967298b4888/html5/thumbnails/74.jpg)
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](https://reader033.vdocuments.us/reader033/viewer/2022061119/546ab4b3af795967298b4888/html5/thumbnails/75.jpg)
VerletStick
Demo
![Page 76: Advanced AS3 Animation](https://reader033.vdocuments.us/reader033/viewer/2022061119/546ab4b3af795967298b4888/html5/thumbnails/76.jpg)
Verlet Structures
![Page 77: Advanced AS3 Animation](https://reader033.vdocuments.us/reader033/viewer/2022061119/546ab4b3af795967298b4888/html5/thumbnails/77.jpg)
Triangle
Demo
![Page 78: Advanced AS3 Animation](https://reader033.vdocuments.us/reader033/viewer/2022061119/546ab4b3af795967298b4888/html5/thumbnails/78.jpg)
ITERATIONDealing With Multiple Constraints
![Page 79: Advanced AS3 Animation](https://reader033.vdocuments.us/reader033/viewer/2022061119/546ab4b3af795967298b4888/html5/thumbnails/79.jpg)
![Page 80: Advanced AS3 Animation](https://reader033.vdocuments.us/reader033/viewer/2022061119/546ab4b3af795967298b4888/html5/thumbnails/80.jpg)
![Page 81: Advanced AS3 Animation](https://reader033.vdocuments.us/reader033/viewer/2022061119/546ab4b3af795967298b4888/html5/thumbnails/81.jpg)
![Page 82: Advanced AS3 Animation](https://reader033.vdocuments.us/reader033/viewer/2022061119/546ab4b3af795967298b4888/html5/thumbnails/82.jpg)
![Page 83: Advanced AS3 Animation](https://reader033.vdocuments.us/reader033/viewer/2022061119/546ab4b3af795967298b4888/html5/thumbnails/83.jpg)
updatePoints()
for(var i = 0; i < iter; i++){
handleConstraints() updateSticks()
}
![Page 84: Advanced AS3 Animation](https://reader033.vdocuments.us/reader033/viewer/2022061119/546ab4b3af795967298b4888/html5/thumbnails/84.jpg)
Verlet Hinges
![Page 85: Advanced AS3 Animation](https://reader033.vdocuments.us/reader033/viewer/2022061119/546ab4b3af795967298b4888/html5/thumbnails/85.jpg)
Verlet Builder
http://www.bit-101.com/verletbuilder/
http://www.bit-101.com/verletbuilder/models.html