Tutorial
Simple collision
Here's the code:
Put the MC instance name on your stage into an array and test
var balla:array = [Ball1, Ball2, Ball3, BALL4, BALL5];
Random initialization of the speed and direction of each MC
for (var i = 0; i<balla.length; i++) {
BALLA.DX = 11*rand1_1 ();
Balla.dy = 7*rand1_1 ();
}
Randomly generated 1 or-1
function Rand1_1 (): number {
return random (2)? -1:1;
}
Stage.scalemode = "Noscale";
Stage.align = "TL";
var sw:number = stage.width;
var sh:number = stage.height;
var stagelistener:object = new Object ();
When the stage size changes, reset SW, SH
Stagelistener.onresize = function () {
SW = Stage.width;
SH = stage.height;
};
Stage.addlistener (Stagelistener);
Onenterframe = function () {
for (var i = 0; i<balla.length; i++) {
Check if there is any collision between the MC, change the "increment" direction according to the situation
for (var j = i+1 j<balla.length; J + +) {
if (Balla.hittest (Balla[j])) {
MC12 (BallA, balla[j]);
Balla._x + = BALLA.DX;
Balla[j]._x + = BALLA[J].DX;
Balla._y + = Balla.dy;
Balla[j]._y + = Balla[j].dy;
}
}
If we get to the boundary, we reflect.
if ((BALLA._X<BALLA._WIDTH/2 && balla.dx<0) or (BALLA._X>SW-BALLA._WIDTH/2 && balla.dx>0) ) {
BALLA.DX *=-1;
}
if (BALLA._Y<BALLA._HEIGHT/2 && balla.dy<0) or (BALLA._Y>SH-BALLA._HEIGHT/2 && balla.dy> 0)) {
Balla.dy *=-1;
}
Move an "increment"
Balla._x + = BALLA.DX;
Balla._y + = Balla.dy;
}
};
The collision function synthesizes the new increment value according to the direction of collision and the direction of motion of two spheres.
function MC12 (Mc1:movieclip, Mc2:movieclip) {
var ang:number = math.atan2 (mc2._y-mc1._y, mc2._x-mc1._x);
var ang1:number = math.atan2 (Mc1.dy, MC1.DX);
var ang2:number = math.atan2 (Mc2.dy, MC2.DX);
var r1:number = (Mc1.dy/math.sin (ang1)) *math.cos (ANG-ANG1);
var r2:number = (Mc2.dy/math.sin (ang2)) *math.cos (ANG-ANG2);
var dx1:number = R1*math.cos (ANG) +r2*math.cos (ANG);
var dy1:number = R1*math.sin (ANG) +r2*math.sin (ANG);
MC1.DX =-mc1.dx+dx1;
Mc1.dy =-mc1.dy+dy1;
MC2.DX =-mc2.dx+dx1;
Mc2.dy =-mc2.dy+dy1;
}
Add rebound effect
Code:
Put the MC instance name on your stage into an array and test
var balla:array = [Ball1, Ball2, Ball3, BALL4, BALL5];
Random initialization of the speed and direction of each MC
for (var i = 0; i<balla.length; i++) {
BALLA[I].DX = 5*rand1_1 ();
Balla[i].dy = 5*rand1_1 ();
Balla[i]. N=i;
}
Randomly generated 1 or-1
function Rand1_1 (): number {
return random (2)? -1:1;
}
Stage.scalemode = "Noscale";
Stage.align = "TL";
var sw:number = stage.width;
var sh:number = stage.height;
var stagelistener:object = new Object ();
When the stage size changes, reset SW, SH
Stagelistener.onresize = function () {
SW = Stage.width;
SH = stage.height;
};
Stage.addlistener (Stagelistener);
Onenterframe = function () {
for (var i = 0; i<balla.length; i++) {
If we get to the boundary, we reflect.
if (BALLA[I]._X<BALLA[I]._WIDTH/2 && balla[i].dx<0) or (BALLA[I]._X>SW-BALLA[I]._WIDTH/2 & & balla[i].dx>0)) {
BALLA[I].DX *=-1;
}
if (BALLA[I]._Y<BALLA[I]._HEIGHT/2 && balla[i].dy<0) or (BALLA[I]._Y>SH-BALLA[I]._HEIGHT/2 & & balla[i].dy>0)) {
Balla[i].dy *=-1;
}
Check if there is any collision between the MC, change the "increment" direction according to the situation
for (var j = i+1 j<balla.length; J + +) {
if (Myhittest (Balla[i],balla[j])) {
if (Balla[i].hittest (Balla[j])) {
MC12 (Balla[i], balla[j]);
Balla[i]._x + = BALLA[I].DX;
Balla[j]._x + = BALLA[J].DX;
Balla[i]._y + = Balla[i].dy;
Balla[j]._y + = Balla[j].dy;
}
}
Move an "increment"
Balla[i]._x + = BALLA[I].DX;
Balla[i]._y + = Balla[i].dy;
}
Updateafterevent ();
};
The collision function synthesizes the new increment value according to the direction of collision and the direction of motion of two spheres.
function MC12 (Mc1:movieclip, Mc2:movieclip) {
Collision Angle
var ang:number = math.atan2 (mc2._y-mc1._y, mc2._x-mc1._x);
Motion Angle
var ang1:number = math.atan2 (Mc1.dy, MC1.DX);
var ang2:number = math.atan2 (Mc2.dy, MC2.DX);
Angle of Reflection
var _ang1:number = 2*ang-ang1-math.pi;
var _ang2:number = 2*ang-ang2-math.pi;
Motion Vector
var r1:number=math.sqrt (Mc1.dx*mc1.dx+mc1.dy*mc1.dy);
var r2:number=math.sqrt (Mc2.dx*mc2.dx+mc2.dy*mc2.dy);
Collision Vector
var a1:number = (Mc1.dy/math.sin (ang1)) *math.cos (ANG-ANG1);
var a2:number = (Mc2.dy/math.sin (ang2)) *math.cos (ANG-ANG2);
Collision Vector Synthesis
var dx1:number = A1*math.cos (ANG) +a2*math.cos (ANG);
var dy1:number = A1*math.sin (ANG) +a2*math.sin (ANG);
Post-collision increments
MC1.DX = R1*math.cos (_ang1) +dx1;
Mc1.dy = R1*math.sin (_ang1) +dy1;
MC2.DX = R2*math.cos (_ang2) +dx1;
Mc2.dy = R2*math.sin (_ang2) +dy1;
}
Collision detection
function Myhittest (mc1:movieclip,mc2:movieclip): boolean{
var a:number=math.sqrt ((mc1._x-mc2._x) * (mc1._x-mc2._x) + (mc1._y-mc2._y) * (mc1._y-mc2._y));
if (a-5<= (mc1._width+mc2._width)/2) {
return true;
}else{
return false;
}
}
With rotation effect:
Code:
Put the MC instance name on your stage into an array and test
var balla:array = [Ball1, Ball2, Ball3, BALL4, BALL5];
Random initialization of the speed, direction and rotation of each MC
for (var i = 0; i<balla.length; i++) {
BALLA[I].DX = 5*rand1_1 ();
Balla[i].dy = 5*rand1_1 ();
BALLA[I].DR = 0;//Increase Rotation properties
Balla[i]. n=i;//Ball Number
}
Randomly generated 1 or-1
function Rand1_1 (): number {
return random (2)? -1:1;
}
Stage properties
Stage.scalemode = "Noscale";
Stage.align = "TL";
var sw:number = stage.width;
var sh:number = stage.height;
var stagelistener:object = new Object ();
When the stage size changes, reset SW, SH
Stagelistener.onresize = function () {
SW = Stage.width;
SH = stage.height;
};
Stage.addlistener (Stagelistener);
//------
Onenterframe = function () {
for (var i = 0; i<balla.length; i++) {
If we get to the boundary, we reflect.
if (BALLA[I]._X<BALLA[I]._WIDTH/2 && balla[i].dx<0) {
BALLA[I].DX *=-1;
Balla[i].dr + = Balla[i].dy;
}
if (BALLA[I]._X>SW-BALLA[I]._WIDTH/2 && balla[i].dx>0) {
BALLA[I].DX *=-1;
Balla[i].dr + =-balla[i].dy;
}
if (BALLA[I]._Y<BALLA[I]._HEIGHT/2 && balla[i].dy<0) {
Balla[i].dy *=-1;
Balla[i].dr + =-BALLA[I].DX;
}
if (BALLA[I]._Y>SH-BALLA[I]._HEIGHT/2 && balla[i].dy>0) {
Balla[i].dy *=-1;
Balla[i].dr + = BALLA[I].DX;
}
Check if there is any collision between the MC, change the "increment" direction according to the situation
for (var j = i+1 j<balla.length; J + +) {
if (Myhittest (Balla[i],balla[j])) {
if (Balla[i].hittest (Balla[j])) {
MC12 (Balla[i], balla[j]);
Balla[i]._x + = BALLA[I].DX;
Balla[j]._x + = BALLA[J].DX;
Balla[i]._y + = Balla[i].dy;
Balla[j]._y + = Balla[j].dy;
}
}
Move an "increment"
Balla[i]._x + = BALLA[I].DX;
Balla[i]._y + = Balla[i].dy;
Balla[i]._rotation + = balla[i].dr;
BALLA[I].DR-= balla[i].dr*0.01;
}
};
The collision function synthesizes the new increment value according to the direction of collision and the direction of motion of two spheres.
function MC12 (Mc1:movieclip, Mc2:movieclip) {
Collision Angle
var ang:number = math.atan2 (mc2._y-mc1._y, mc2._x-mc1._x);
Motion Angle
var ang1:number = math.atan2 (Mc1.dy, MC1.DX);
var ang2:number = math.atan2 (Mc2.dy, MC2.DX);
Angle of Reflection
var _ang1:number = 2*ang-ang1-math.pi;
var _ang2:number = 2*ang-ang2-math.pi;
Motion Vector
var r1:number=math.sqrt (Mc1.dx*mc1.dx+mc1.dy*mc1.dy);
var r2:number=math.sqrt (Mc2.dx*mc2.dx+mc2.dy*mc2.dy);
Collision Vector
var a1:number = (Mc1.dy/math.sin (ang1)) *math.cos (ANG-ANG1);
var a2:number = (Mc2.dy/math.sin (ang2)) *math.cos (ANG-ANG2);
Collision Vector Synthesis
var dx1:number = A1*math.cos (ANG) +a2*math.cos (ANG);
var dy1:number = A1*math.sin (ANG) +a2*math.sin (ANG);
Rotation vector, there is still a problem, the direction of rotation is sometimes wrong.
Mc1.dr + + r1*math.sin (ang-ang1);
Mc2.dr + + r2*math.sin (ang-ang2);
Post-collision increments
MC1.DX = R1*math.cos (_ang1) +dx1;
Mc1.dy = R1*math.sin (_ang1) +dy1;
MC2.DX = R2*math.cos (_ang2) +dx1;
Mc2.dy = R2*math.sin (_ang2) +dy1;
}
Collision detection
function Myhittest (mc1:movieclip,mc2:movieclip): boolean{
var a:number=math.sqrt ((mc1._x-mc2._x) * (mc1._x-mc2._x) + (mc1._y-mc2._y) * (mc1._y-mc2._y));
if (a-2<= (mc1._width+mc2._width)/2) {
return true;
}else{
return false;
}
}