#include "Physics.as"
_root.onLoad = function() {
ball1.CreatePhysicsFeature(1, 0, 0, 8, 6, 15);
ball2.CreatePhysicsFeature(1, 0, 0, -7, 5, 15);
ball3.CreatePhysicsFeature(1, 0, 0, 8, -9, 15);
ball4.CreatePhysicsFeature(1, 0, 0, 5, -5, 15);
ball5.CreatePhysicsFeature(1, 0, 0, 5, -2, 15);
ball6.CreatePhysicsFeature(1, 0, 0, 6, -6, 15);
ball7.CreatePhysicsFeature(1, 0, 0, 7, -6, 15);
for (var i = 1; i<8; i++) {//创建用于标识二球间状况的二维数组
eval("ball"+i).n(i);
}
_global.a = new Array();
for (var i = 1; i<7; i++) {
a[i] = new Array();
for (var j = i+1; j<8; j++) {
a[i][j] = 0;
}
}
};
///***Fundamental Function***///
function PreciseCollisionRender(mc1, mc2) {// 确定是否分开
if(Math.sqrt((mc1._x-mc2._x)*(mc1._x-mc2._x)+(mc1._y-mc2._y)*(mc1._y-mc2._y))>mc1.r+mc2.r+1) {
a[mc1.n][mc2.n] = 0;
}
}
function collisionx(mc1, mc2) {
var v1temp = mc1.vx;
var v2temp = mc2.vx;
mc1.vx=((mc1.m-e*mc2.m)/(mc1.m+mc2.m))*v1temp+((1+e)*mc2.m/(mc1.m+mc2.m))*v2temp;
mc2.vx = ((1+e)*mc1.m/(mc1.m+mc2.m))*v1temp-((e*mc1.m-mc2.m)/(mc1.m+mc2.m))*v2temp;
}
function collisiony(mc1, mc2) {
var v1temp = mc1.vy;
var v2temp = mc2.vy;
mc1.vy= ((mc1.m-e*mc2.m)/(mc1.m+mc2.m))*v1temp+((1+e)*mc2.m/(mc1.m+mc2.m))*v2temp;
mc2.vy = ((1+e)*mc1.m/(mc1.m+mc2.m))*v1temp-((e*mc1.m-mc2.m)/(mc1.m+mc2.m))*v2temp;
}
MovieClip.prototype.Move = function(vx, vy) {
// 构造专属的move函数。
this._x += vx;
this._y += vy;
};
function bordercollisiondetect(mc) {//碰边检测
if (mc.getBounds(_root).xMin<1) {
mc._x = 1+mc.r;
mc.vx *= -1;
}
if (mc.getBounds(_root).xMax>399) {
mc._x = 399-mc.r;
mc.vx *= -1;
}
if (mc.getBounds(_root).yMin<1) {
mc._y = 1+mc.r;
mc.vy *= -1;
}
if (mc.getBounds(_root).yMax>399) {
mc._y = 399-mc.r;
mc.vy *= -1;
}
}
//**Combine Function**//
function collisiondetect(mc1, mc2) {
if (a[mc1.n][mc2.n] == 1) {
PreciseCollisionRender(mc1, mc2);
}
elseif(Math.sqrt((mc1._x-mc2._x)*(mc1._x-mc2._x)+(mc1._y-mc2._y)*(mc1._y-mc2._y))<=mc1.r+mc.r) {
collisionx(mc1, mc2);
collisiony(mc1, mc2);
a[mc1.n][mc2.n] = 1;
}
}
function SumCollision() {
for (var i = 1; i<7; i++) {
for (var j = i+1; j<8; j++) {
collisiondetect(eval("ball"+i), eval("ball"+j));
}
}
for (var i = 1; i<8; i++) {
bordercollisiondetect(eval("ball"+i));
}
}
function SumMove() {
for (var i = 1; i<8; i++) {
eval("ball"+i).Move(eval("ball"+i).vx, eval("ball"+i).vy);
}
}
_root.onEnterFrame = function() {
SumCollision();
SumMove();
};