Hi, i need some help regarding collission detection.
My project: I have some cells in a tank. They start from positions defined in an array and move to new positions, again defined in an array.
Now, whats important is their Y position, not their X. They should always float up or sink down to their Y position. (I have this working fine). I dont mind what their x position is, but i would like for them to avoid eachother. So when they move to their new position they need to float around eachother to get to their new position.
I want them to stay on screen, so when they hit the edge of the stage they need to reverse their direction (but still aim for their Y destination). I understand how to detect if they have crossed a boundary, to reposition the MC back to the edge and then move it away, to stop getting that effect where it lookslike its shaking.
I have tried to apply this so when they hit eachother they do this as well but it wont wrk. I am a newb at this and have been regulary posting in this forum for the past week or so and my project is so nearly finished.
i am desperate for someone to help me solve this one last problem.
var originalYposition: Number = 500;
var currentYposition:Array = ["100", "100", "100", "100", "100", "100", "100", "100", "100", "100"];
var newLocation:Array = ["300", "320", "340", "360", "380", "400", "420", "430", "440", "450"];
var depth = 0;
var easing:Number = 0.02;
top = 0;
left = 0;
bottom = Stage.height;
right = Stage.width;
function initialisecell() {
for (i=0; i<32; i++) {
var cell = this.attachMovie("cell"+i, "cell"+i, depth++);
cell._x = Math.round(Math.random()*520+20);
cell._y = currentYposition*;
cell._width = cell._height = 22;
}
}
initialisecell();
function onEnterFrame(Void):Void {
for (i=0; i<32; i++) {
var vy:Number = (newLocation* - this["cell"+i]._y) * easing;
this["cell"+i]._y += vy;
//following checks if the cells have hit the edge of the tank.
if (this["cell"+i]._x + this["cell"+i]._width / 2 > right)
{
this["cell"+i]._x = right - this["cell"+i]._width / 2;
vx *= -1;
}
else if (this["cell"+i]._x - this["cell"+i]._width / 2 < left)
{
this["cell"+i]._x = left + this["cell"+i]._width / 2;
vx *= -1;
}
if (this["cell"+i]._y + this["cell"+i]._height / 2 > bottom)
{
this["cell"+i]._y = bottom - this["cell"+i]._height / 2;
vy *= -1;
}
else if (this["cell"+i]._y - this["cell"+i]._height / 2 < top)
{
this["cell"+i]._y = top + this["cell"+i]._height / 2;
vy *= -1;
}
}
// following compares all cells to eachother. If they have collided, reposition it back and reverse its position
for(var i=0;i<10-1;i++) {
var ballA:MovieClip = this["cell" + i];
for(var j=i+1;j<10;j++)
{
var ballB:MovieClip = this["cell" + j];
var dx:Number = ballB._x - ballA._x;
var dy:Number = ballB._y - ballA._y;
var dist:Number = Math.sqrt(dx*dx + dy*dy);
var minDist:Number = ballA._width / 2 + ballB._width / 2;
if(dist < minDist)
{
ballA._x = ballB._x - ballA.width/2;
vx *= -1;
}
}
}
}