
Код AS3:
package gerich.verlet
{
import flash.display.Shape;
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
public class Main extends Sprite
{
private var pt:Array = new Array();
private var joints:Array = new Array();
private var mousejoints:Array = new Array();
private var particles:Sprite = new Sprite();
private var canvas:Shape = new Shape();
private var grav:Number = 100;
private var timeSqr:Number;
// ========================================================== //
public function Main():void
{
stage.frameRate = 40;
timeSqr=1/stage.frameRate;timeSqr*=timeSqr;
addChild(canvas);
addChild(particles);
pt.push(new Particle(270,150));
pt.push(new Particle(270,175));
pt.push(new Particle(270,200));
pt.push(new Particle(270,225));
pt.push(new Particle(270,250));
pt.push(new Particle(270,275));
pt.push(new Particle(255,285));
pt.push(new Particle(285,285));
joints.push(new MouseJoint(pt[0], pt[1], 25));
joints.push(new Joint(pt[1], pt[2], 25));
joints.push(new Joint(pt[2], pt[3], 25));
joints.push(new Joint(pt[3], pt[4], 25));
joints.push(new Joint(pt[4], pt[5], 25));
joints.push(new Joint(pt[5], pt[6], 25));
joints.push(new Joint(pt[5], pt[7], 25));
joints.push(new Joint(pt[6], pt[7], 25));
for (var i in joints)
{
if(joints[i] is MouseJoint)
mousejoints.push(joints[i]);
}
for (var i in pt)
{
particles.addChild(pt[i]);
}
addEventListener(Event.ENTER_FRAME, checkJoints);
addEventListener(Event.ENTER_FRAME, ptMouse);
}
// ========================================================== //
private function checkJoints(event:Event):void
{
var dx:Number;
var dy:Number;
canvas.graphics.clear();
// Gravity
for (var i in pt)
{
dx = (pt[i].xx - pt[i].old_x);
dy = (pt[i].yy - pt[i].old_y);
pt[i].old_x = pt[i].xx;
pt[i].old_y = pt[i].yy;
pt[i].xx += dx;
pt[i].yy += dy+grav*timeSqr;
}
// Joints
for (var z:int=0; z<5; z++)
{
for (var i in joints)
{
joints[i].Apply();
}
}
for (var i in pt)
{
pt[i].x = pt[i].xx;
pt[i].y = pt[i].yy;
}
// Lines
for (var i in joints)
{
joints[i].Draw(canvas);
}
}
// ========================================================== //
private function ptMouse(event:Event):void
{
for (var i in mousejoints)
{
mousejoints[i].SetMouse( mouseX,mouseY);
mousejoints[i].Draw(canvas);;
}
}
}
}
// ***** [ Particle Class ] ***************************************** //
import flash.display.Sprite;
internal class Particle extends Sprite
{
public var old_x:Number;
public var old_y:Number;
public var xx:Number;
public var yy:Number;
// ========================================================== //
public function Particle(_x:int, _y:int):void
{
x = _x;
y = _y;
xx = _x;
yy = _y;
old_x = _x;
old_y = _y;
graphics.lineStyle(1, 0x000000);
graphics.beginFill(0xFFD22B);
graphics.drawCircle(0,0,5);
graphics.endFill();
}
// ========================================================== //
}
import flash.display.Shape;
internal class Joint
{
public var p1:Particle;
public var p2:Particle;
public var len:Number;
// ========================================================== //
public function Joint(_p1:Particle, _p2:Particle, _len:Number):void
{
p1=_p1;
p2=_p2;
len=_len;
}
// ========================================================== //
public function Draw(canvas:Shape):void
{
canvas.graphics.lineStyle(1,0x000000);
canvas.graphics.moveTo(p1.x, p1.y);
canvas.graphics.lineTo(p2.x, p2.y);
}
// ========================================================== //
public function Apply():void
{
var dx:Number;
var dy:Number;
var d_len:Number;
var diff:Number;
dx = p2.xx-p1.xx;
dy = p2.yy-p1.yy;
d_len = Math.sqrt(dx*dx + dy*dy);
diff = (d_len-len)/d_len;
dx *= 0.5*diff;
dy *= 0.5*diff;
p2.xx -= dx;
p2.yy -= dy;
p1.xx += dx;
p1.yy += dy;
}
// ========================================================== //
}
internal class MouseJoint extends Joint
{
// ========================================================== //
public function MouseJoint(_p1:Particle, _p2:Particle, _len:Number):void
{
super(_p1,_p2,_len);
}
// ========================================================== //
override public function Apply():void
{
var dx:Number;
var dy:Number;
var d_len:Number;
var diff:Number;
dx = p2.xx-p1.xx;
dy = p2.yy-p1.yy;
d_len = Math.sqrt(dx*dx + dy*dy);
diff = (d_len-len)/d_len;
dx *= diff;
dy *= diff;
p2.xx -= dx;
p2.yy -= dy;
}
// ========================================================== //
public function SetMouse(mousex:Number,mousey:Number):void
{
p1.x =mousex;
p1.xx=mousex;
p1.y =mousey;
p1.yy=mousey;
}
// ========================================================== //
}
P.S. Одна из первых прог с классами на AS3.. лёгкий язык.. очень)
Я убрал трение вроде.. как я понял оно у тя было
ну ещё я ведь добавил время... а вообще спасибо тебе) прочитал статейку про верлета, лень было самому всё писать, а тут бац почти всё готово)