一维动量守恒
1.构建小球精灵
Ball.as
package
{
import flash.display.Sprite;
public class Ball extends Sprite
{
public var radius:Number;
public var color:uint;
public var vx:Number=0;
public var vy:Number=0;
public function Ball (radius:Number,color:uint)
{
this.radius=radius;
this.color=color;
Init ();
}
public function Init ():void
{
graphics.beginFill (color);
graphics.drawCircle (0,0,radius);
graphics.endFill ();
}
}
}
2.构建一维动量守恒动画
package
{
import flash.display.Sprite;
public class Ball extends Sprite
{
public var radius:Number;
public var color:uint;
public var vx:Number=0;
public var vy:Number=0;
public function Ball (radius:Number,color:uint)
{
this.radius=radius;
this.color=color;
Init ();
}
public function Init ():void
{
graphics.beginFill (color);
graphics.drawCircle (0,0,radius);
graphics.endFill ();
}
}
}
Billiard1.as
package
{
import flash.events.Event;
import flash.display.Sprite;
public class Billiard1 extends Sprite
{
private var ball0:Ball;
private var ball1:Ball;
private var mass0:Number;
private var mass1:Number;
public function Billiard1()
{
Init();
}
private function Init():void
{
ball0=new Ball(20,0xFFcc00);
mass0=ball0.radius;
ball0.vx=10;
ball0.x=40;
ball0.y=200;
addChild(ball0);
ball1=new Ball(30,0xFF0000);
mass1=ball1.radius;
ball1.vx=-10;
ball1.x=500;
ball1.y=200;
addChild(ball1);
addEventListener(Event.ENTER_FRAME,EnterFrame);
}
private function EnterFrame(e:Event):void
{
ball0.x+=ball0.vx;
ball1.x+=ball1.vx;
var distance:Number=ball1.x-ball0.x;
if(Math.abs(distance)<ball0.radius+ball1.radius)
{
var dv:Number=ball0.vx-ball1.vx;
ball0.vx=((mass0-mass1)*ball0.vx+2*mass1*ball1.vx)/(mass0+mass1);
ball1.vx=dv+ball0.vx;
ball0.x+=ball0.vx;
ball1.x+=ball1.vx;
}
}
}
}
package
{
import flash.events.Event;
import flash.display.Sprite;
public class Billiard1 extends Sprite
{
private var ball0:Ball;
private var ball1:Ball;
private var mass0:Number;
private var mass1:Number;
public function Billiard1()
{
Init();
}
private function Init():void
{
ball0=new Ball(20,0xFFcc00);
mass0=ball0.radius;
ball0.vx=10;
ball0.x=40;
ball0.y=200;
addChild(ball0);
ball1=new Ball(30,0xFF0000);
mass1=ball1.radius;
ball1.vx=-10;
ball1.x=500;
ball1.y=200;
addChild(ball1);
addEventListener(Event.ENTER_FRAME,EnterFrame);
}
private function EnterFrame(e:Event):void
{
ball0.x+=ball0.vx;
ball1.x+=ball1.vx;
var distance:Number=ball1.x-ball0.x;
if(Math.abs(distance)<ball0.radius+ball1.radius)
{
var dv:Number=ball0.vx-ball1.vx;
ball0.vx=((mass0-mass1)*ball0.vx+2*mass1*ball1.vx)/(mass0+mass1);
ball1.vx=dv+ball0.vx;
ball0.x+=ball0.vx;
ball1.x+=ball1.vx;
}
}
}
}