Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package;
- import flash.display.BitmapData;
- import flash.events.TimerEvent;
- import flash.geom.Point;
- import flash.Lib;
- import flash.utils.Timer;
- class Main {
- var sw:Int;
- var sh:Int;
- var particles:Array<Particle>;
- var comx:Float;
- var comy:Float;
- var qs:Array<Point>;
- var angles:Array<Float>;
- function new(stage:flash.display.Stage) {
- sw = stage.stageWidth;
- sh = stage.stageHeight;
- particles = [];
- // initialise particles in a square
- var len = 60;
- var x = sw/2;
- var y = 200;
- particles.push(new Particle(x, y));
- particles.push(new Particle(x+len, y));
- particles.push(new Particle(x+len, y+len));
- particles.push(new Particle(x, y+len));
- // initial center of mass
- comx = comy = 0;
- for (p in particles) {
- comx += p.x;
- comy += p.y;
- }
- comx /= particles.length;
- comy /= particles.length;
- // displacement vectors
- qs = [];
- for (p in particles) {
- qs.push(new Point(p.x-comx, p.y-comy));
- }
- // initial angle displacements from center of mass
- angles = [];
- for (i in 0...particles.length) {
- angles[i] = Math.atan2(particles[i].y-comy, particles[i].x-comx);
- }
- var timer:Timer = new Timer(1000/60);
- timer.addEventListener(TimerEvent.TIMER, step);
- timer.start();
- }
- function step(e:TimerEvent):Void {
- var p = particles[0];
- p.x -= (p.x-Lib.current.mouseX);
- p.y -= (p.y-Lib.current.mouseY);
- // new center of mass
- comx = comy = 0;
- for (p in particles) {
- comx += p.x;
- comy += p.y;
- }
- comx /= particles.length;
- comy /= particles.length;
- // average angle
- var angleDelta = 0.;
- for (i in 0...particles.length) {
- var px = particles[i].x-comx;
- var py = particles[i].y-comy;
- var angle = Math.atan2(py, px);
- var deltaTemp = angle-angles[i];
- if (deltaTemp < -Math.PI) deltaTemp += 2*Math.PI;
- if (deltaTemp > Math.PI) deltaTemp -= 2*Math.PI;
- angleDelta += deltaTemp;
- }
- angleDelta /= particles.length;
- // rotate particles around initial displacement by the angle
- var cos = Math.cos(angleDelta);
- var sin = Math.sin(angleDelta);
- for (i in 0...particles.length) {
- var p = particles[i];
- var q = qs[i];
- var gx = cos*q.x-sin*q.y;
- var gy = sin*q.x+cos*q.y;
- gx += comx;
- gy += comy;
- p.x = gx;
- p.y = gy;
- // p.x += (gx-p.x)*.1;
- // p.y += (gy-p.y)*.1;
- }
- // render
- Lib.current.graphics.clear();
- Lib.current.graphics.lineStyle(0);
- for (p in particles) {
- Lib.current.graphics.beginFill(0);
- Lib.current.graphics.drawCircle(p.x, p.y, 2);
- Lib.current.graphics.endFill();
- }
- Lib.current.graphics.lineStyle(0, 0, .5);
- Lib.current.graphics.moveTo(particles[0].x, particles[0].y);
- Lib.current.graphics.lineTo(comx, comy);
- Lib.current.graphics.lineStyle(0, 0, 0);
- Lib.current.graphics.beginFill(0xFF0000);
- Lib.current.graphics.drawCircle(comx, comy, 3);
- Lib.current.graphics.endFill();
- }
- static function main() {
- new Main(Lib.current.stage);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement