衝突
/**
* Copyright takenosin ( http://wonderfl.net/user/takenosin )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/5xqM
*/
// forked from takenosin's 万有引力
// forked from takenosin's パーティクル
package {
import flash.display.Sprite;
import flash.events.Event;
import flash.geom.Point;
public class ClassName extends Sprite {
private var Particles:Array;
private var numParticles:uint = 30;
public function ClassName() {
// write as3 code here..
Particles = new Array();
for(var i:uint = 0; i < numParticles; i++){
var size:Number = Math.random() * 25 + 5;
var Particle:Ball = new Ball(size);
Particle.x = Math.random() * stage.stageWidth;
Particle.y = Math.random() * stage.stageHeight;
Particle.mass = size;
addChild(Particle);
Particles.push(Particle);
}
addEventListener(Event.ENTER_FRAME,onEnterFrame);
}
private function onEnterFrame(event:Event):void{
for(var i:uint = 0; i < numParticles; i++){
var Particle:Ball = Particles[i];
Particle.x += Particle.vx;
Particle.y += Particle.vy;
}
for(i = 0; i < numParticles - 1; i++){
var PartA:Ball = Particles[i];
for(var j:uint = i + 1; j < numParticles; j++){
var PartB:Ball = Particles[j];
checkCollision(PartA,PartB);
gravitate(PartA,PartB);
}
}
}
private function gravitate(PartA:Ball,PartB:Ball):void{
var dx:Number = PartB.x - PartA.x;
var dy:Number = PartB.y - PartA.y;
var distSQ:Number = dx * dx + dy * dy;
var dist:Number = Math.sqrt(distSQ);
var force:Number = PartA.mass * PartB.mass / distSQ;
var ax:Number = force * dx / dist;
var ay:Number = force * dy / dist;
PartA.vx += ax / PartA.mass;
PartA.vy += ay / PartA.mass;
PartB.vx += ax / PartB.mass;
PartB.vy += ay / PartB.mass;
}
private function checkCollision(ball0:Ball,ball1:Ball):void{
var dx:Number = ball1.x - ball0.x;
var dy:Number = ball1.y - ball0.y;
var dist:Number = Math.sqrt(dx * dx + dy * dy);
if(dist < ball0.radius + ball1.radius){
//角度とサイン、コサインの計算
var angle:Number = Math.atan2(dy,dx);
var sin:Number = Math.sin(angle);
var cos:Number = Math.cos(angle);
//ball0の位置の回転
var x0:Number = 0;
var y0:Number = 0;
//ball1の位置の回転
var x1:Number = dx * cos + dy * sin;
var y1:Number = dy * cos - dx * sin;
//ball0の速度の回転
var vx0:Number = ball0.vx * cos + ball0.vy * sin;
var vy0:Number = ball0.vy * cos - ball0.vx * sin;
//ball1の速度の回転
var vx1:Number = ball1.vx * cos + ball1.vy * sin;
var vy1:Number = ball1.vy * cos - ball1.vx * sin;
//衝突反応
var vxTotal:Number = vx0 - vx1;
vx0 = ((ball0.mass - ball1.mass) * vx0 + 2 * ball1.mass * vx1)/(ball0.mass + ball1.mass);
vx1 = vxTotal + vx0;
x0 += vx0;
x1 += vx1;
//位置の回転
var x0Final:Number = x0 * cos - y0 * sin;
var y0Final:Number = y0 * cos + x0 * sin;
var x1Final:Number = x1 * cos - y1 * sin;
var y1Final:Number = y1 * cos + x1 * sin;
//実際の画面位置への調整
ball1.x = ball0.x + x1Final;
ball1.y = ball0.y + y1Final;
ball0.x = ball0.x + x0Final;
ball0.y = ball0.y + y0Final;
//速度の回転
ball0.vx = vx0 * cos - vy0 * sin;
ball0.vy = vy0 * cos + vx0 * sin;
ball1.vx = vx1 * cos - vy1 * sin;
ball1.vy = vy1 * cos + vx1 * sin;
}
}
}
}
import flash.display.Sprite;
class Ball extends Sprite{
public var radius:Number;
private var color:uint;
public var vx:Number = 0;
public var vy:Number = 0;
public var mass:Number = 1;
public function Ball(radius:Number=40,color:uint=0x66ffff){
this.radius = radius;
this.color = color;
graphics.beginFill(color);
graphics.drawCircle(0,0,radius);
graphics.endFill();
}
}