/**
* Copyright john0 ( http://wonderfl.net/user/john0 )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/AgFm
*/
package
{
import flash.display.Sprite
import flash.events.Event;
import flash.geom.Vector3D;
[SWF(frameRate = "30")]
public class Main extends Sprite
{
private var canvas:Sprite;
private var cylinder:Cylinder;
public function Main():void
{
canvas = new Sprite();
cylinder = new Cylinder(10, 10);
canvas.graphics.beginFill(0xFF0000);
canvas.graphics.drawCircle(100, 100, 10);
canvas.graphics.endFill();
addChild(canvas);
addEventListener(Event.ENTER_FRAME, OnEnterFrame);
}
private function OnEnterFrame(event:Event):void
{
cylinder.SetForce(new Vector3D(0.0, -10.0, 0.0));
cylinder.Update();
canvas.x += 2
canvas.y = 0
}
}
}
import flash.geom.Vector3D;
class RigidBody
{
// constant quantities---------------
// mass
protected var _mass:Number;
protected var _massinv:Number;
// moment of inertia
protected var _I:Number;
protected var _Iinv:Number;
// state variables-------------------
// positio of center of mass
protected var _x:Vector3D = new Vector3D(0.0, 0.0, 0.0);
public function GetPos():Vector3D
{
return _x;
}
// angle
protected var _a:Number;
// liner momentum
protected var _p:Vector3D = new Vector3D(0.0, 0.0, 0.0);;
// angular momentum
protected var _L:Number;
// computed quantities---------------
protected var _force:Vector3D = new Vector3D(0.0, 0.0, 0.0);
public function SetForce(force:Vector3D) : void { _force = force; }
protected var _torque:Vector3D = new Vector3D(0.0, 0.0, 0.0);
}
class Cylinder extends RigidBody
{
public function Cylinder(mass:Number, radius:Number) : void
{
_mass = mass;
_massinv = 1 / mass;
// compute moment of inertia of cylinder
_I = 0.5 * mass * radius * radius;
_Iinv = 1 / _I;
}
public function Update() : void
{
var p:Vector3D = new Vector3D();
p = _p;
var force:Vector3D = new Vector3D();
force = _force;
// p1 = p0 + force * dt
force.scaleBy(1.0);
//p += force;
_p = p;
/*
// v = p/m = mv / m
p.scaleBy(_massinv);
var v:Vector3D = p;
// x1 = x0 + v *dt
v.scaleBy(1.0);
_x += v;
*/
}
}