Mohammad Haseeb’s Blog | Archive | Graphics

This little app is to show how you can create artificial physics in Flash using formulas. The principal is quite simple: A guiding hook has several springs attached to it. Each spring is attached to its parent, and thus if the parent spring is moved, the resulting physics is perpetuated to its children.

Here’s the code:

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
import flash.display.*; public class Spring extends Shape{var XVelocity:Number, YVelocity:Number = 0.0;var Gravity:Number = 6.0;var Mass:Number = 2.0;var Radius:Number = 2.0;var Stiffness:Number = 0.2;var Damping:Number = 0.7; public function Spring(XPosition_:Number, YPosition_:Number, Mass_:Number, Gravity_:Number){XVelocity = 0.0;YVelocity = 0.0;Radius = 10.0;Stiffness= 0.5;Damping = 0.7;x = XPosition_;y = YPosition_;Mass = Mass_;Gravity = Gravity_;} public function UpdatePosition(XTarget:Number, YTarget:Number):void{var XForce:Number = (XTarget - x) * Stiffness;var XFactor:Number = XForce / Mass; XVelocity = Damping * (XVelocity + XFactor);x += XVelocity; var YForce:Number = (YTarget - y) * Stiffness;YForce += Gravity; var YFactor:Number = YForce / Mass; YVelocity = Damping * (YVelocity + YFactor);y += YVelocity;} public function RenderPosition(XNew:Number, yNew:Number){graphics.clear();graphics.beginFill(0xf5f4ea);graphics.lineStyle(3,0xdfdfdf);graphics.drawEllipse(0,0,Radius * 2,Radius * 2);//graphics.moveTo(XNew,YNew);//graphics.lineTo(x, y);graphics.endFill();}}

Grab the sources below:

chain.zip