forked from: flash on 2011-2-24
forked from flash on 2011-2-24 (diff: 113)
ActionScript3 source code
/**
* Copyright fakestar0826 ( http://wonderfl.net/user/fakestar0826 )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/uHsu
*/
// forked from fakestar0826's flash on 2011-2-24
package {
import flash.events.Event;
import flash.display.Sprite;
public class FlashTest extends Sprite
{
private var ball0:Ball;
private var ball1:Ball;
private var bounce:Number = -1.0;
public function FlashTest()
{
// write as3 code here..
ball0 = new Ball(40, 0xFF0000);
ball0.mass = 2;
ball0.x = 300;
ball0.y = 300;
ball0.vx = Math.random() * 10 - 5;
ball0.vy = Math.random() * 10 - 5;
addChild(ball0);
ball1 = new Ball(60, 0x00FF00);
ball1.mass = 1;
ball1.x = 100;
ball1.y = 100;
ball1.vx = Math.random() * 10 - 5;
ball1.vy = Math.random() * 10 - 5;
addChild(ball1);
addEventListener(Event.ENTER_FRAME, onEnterFrame);
}
private function onEnterFrame(e:Event):void
{
ball0.x += ball0.vx;
ball0.y += ball0.vy;
ball1.x += ball1.vx;
ball1.y += ball1.vy;
checkCollision(ball0, ball1);
checkWalls(ball0);
checkWalls(ball1);
}
private function checkWalls(ball:Ball):void
{
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;
}
}
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);
var x0:Number = 0;
var y0:Number = 0;
var x1:Number = dx * cos + dy * sin;
var y1:Number = dy * cos - dx * sin;
var vx0:Number = ball0.vx * cos + ball0.vy * sin;
var vy0:Number = ball0.vy * cos - ball0.vx * sin;
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 vx:Number = 0;
public var vy:Number = 0;
public var mass:Number = 1;
public var radius:Number = 30
public var color:uint = 0xFF0000;
public function Ball(r:Number = 10, c:uint = 0xFF0000)
{
radius = r;
color = c;
graphics.beginFill(c);
graphics.drawCircle(0, 0, r);
graphics.endFill();
}
}
