BOUNDING BALLS Main メインクラス. kazy forked:1favorite:1lines:88license : MIT License modified : 2009-06-11 22:49:16 Embed Tweet package { import flash.display.Sprite; import flash.events.Event; /** * Main * メインクラス. */ public class Main extends Sprite { private var balls:Array; //ボールの数 private var numBalls:Number = 30; private var bounce:Number = -0.9; private var spring:Number = 0.05; private var gravity:Number = 0.1; /** * コンストラクタ. */ public function Main() { init(); } /** * コンストラクタ. */ private function init():void { balls = new Array(); for (var i:uint = 0; i < numBalls; i++) { //ボール生成 var ball:Ball = new Ball(Math.random() * 30 + 20, Math.random() * 0xff0000); //初期位置 ball.x = Math.random() * stage.stageWidth; ball.y = Math.random() * stage.stageHeight; //ボール初速度 ball.vx = Math.random() * 6 - 3; ball.vy = Math.random() * 6 - 3; //表示 addChild(ball); //配列追加 balls.push(ball); } //ENTER_FRAMEイベントをリスナーに追加 addEventListener(Event.ENTER_FRAME, onEnterFrame); } private function onEnterFrame(event:Event):void { for (var i:uint = 0; i < numBalls - 1; i++) { //対象のボール var ball0:Ball = balls[i]; for (var j:uint = i + 1; j < numBalls; j++) { //被対象のボール var ball1:Ball = balls[j]; //両ボール座標の相対距離 var dx:Number = ball1.x - ball0.x; var dy:Number = ball1.y - ball0.y; //3平方の定理から直線距離を出す var dist:Number = Math.sqrt(dx * dx + dy * dy); //両ボールの半径の合計を最小距離に var minDist:Number = ball0.radius + ball1.radius; //最小距離より縮まった場合 if (dist < minDist) { //x座標系の最小距離 var tx:Number = ball0.x + dx / dist * minDist; //y座標系の最小距離 var ty:Number = ball0.y + dy / dist * minDist; //加速度 var ax:Number = (tx - ball1.x) * spring; var ay:Number = (ty - ball1.y) * spring; //それぞれ反対側へ反発 ball0.vx -= ax; ball0.vy -= ay; ball1.vx += ax; ball1.vy += ay; } } } for (i = 0; i < numBalls; i++) { //基本の動き var ball:Ball = balls[i]; move(ball); } } /** * 基本の動き. */ private function move(ball:Ball):void { //重力 ball.vy += gravity; //加速 ball.x += ball.vx; ball.y += ball.vy; //壁で減速してはね返る if (ball.x + ball.radius > stage.stageWidth) { ball.x = stage.stageWidth - ball.radius; ball.vx *= bounce; } else if (ball.x - ball.radius < 0) { ball.x = ball.radius; ball.vx *= bounce; } if (ball.y + ball.radius > stage.stageHeight) { ball.y = stage.stageHeight - ball.radius; ball.vy *= bounce; } else if (ball.y - ball.radius < 0) { ball.y = ball.radius; ball.vy *= bounce; } } } } /** * Ball * ボール生成クラス. */ class Ball extends flash.display.Sprite { public var radius:Number; private var color:uint; public var vx:Number = 0; public var vy:Number = 0; /** * コンストラクタ. */ public function Ball(radius:Number=40, color:uint=0x990033) { this.radius = radius; this.color = color; init(); } /** * 初期化. */ public function init():void { graphics.beginFill(color); graphics.drawCircle(0, 0, radius); graphics.endFill(); } } Code Fullscreen Preview Fullscreen idettman : bouncinggravityphysics bouncing gravity physics Math.sqrt addEventListener push addChild Event.ENTER_FRAME Array Math.random Event uint Sprite Number sort new page view favorite forked pv1421 forked from: BOUNDING BALLS kazy forked:1 favorite:1lines:88 (diff:4)