Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Bouncing Balls Collision

Status
Not open for further replies.

zickler

Technical User
Mar 6, 2004
28
0
0
IE
Hi ! I have some bouncing balls that I am using as navigational link buttons. However at the moment some of the balls can be in front of another ball or balls. I would like to be able to make it that if the balls collide that they bounce off each other. On each ball there is three frames of actionscript as shown below, would it be difficult to add some script to make them collide. Any help would be appreciated. Thanks

********Frame 1********

//Set the bounds
bounds = new Object();
bounds.left = 50;
bounds.right = 730;
bounds.top = 100;
bounds.bottom = 488;

//Set initial speed vector
speed = new Object();
speed.x = 10;
speed.y = 10;

//Set friction to act on ball
friction = .98;

//Set boolean for drag property
drag = false;

//Gravity and Friction toggle variables
friction_t = true;
gravity_t = true;

********** Frame 2 ***********

if(drag) {

//If the ball is being dragged, recalculate it's speed
if(old_x <> this._x || old_y <> this._y) {
speed.x = (this._x - old_x) / 4;
speed.y = (this._y - old_y) / 4;
}
old_x = this._x;
old_y = this._y;

} else {

//move ball
this._x += speed.x;
this._y += speed.y;

//Check bounds
if(this._x < bounds.left) {
speed.x *= -1;
this._x = bounds.left;
} else if(this._x > bounds.right) {
speed.x *= -1;
this._x = bounds.right;
} else if(this._y < bounds.top) {
speed.y *= -1;
this._y = bounds.top;
} else if(this._y > bounds.bottom) {
speed.y *= -1;
this._y = bounds.bottom;
}

if(friction_t) {
//apply friction to horizontal plain
speed.x *= friction;

//apply y friction if gravity is off
if(!gravity_t){
speed.y *= friction;
}
}

if(gravity_t) {
//apply gravity to vertical plain
if(speed.y > 0){
speed.y *= 1.3;
} else if(speed.y < 0){
speed.y *= .68;
}

if((speed.y < 2) && (speed.y > -2) && (this._y < (bounds.bottom - 2))) {
speed.y = 2;
}

if((speed.y == 2) && (this._y > (bounds.bottom - 3))) {
speed.y = 0;
this._y = bounds.bottom;
}
}

}

***********Frame 3***********
gotoAndPlay(this._currentframe-1);
 
I would use the hittest method to test if any of the balls have collided. I believe you'll need to place the collision testing code in the parent timeline, since the tests are made BETWEEN movieclips...for instance testing collision between ballA_mc and ballB_mc might look like this:

Code:
// main movie timeline (frame 1)
if(ballA_mc.hitTest(ballB_mc)) {
   ballA_mc.gotoAndPlay(4);
   ballB_mc.gotoAndPlay(4);
}

// ball clip timeline ***Frame 4***

/******* one possible solution (unrealistic) ********/
speedx *= -1;
speedy *= -1;
/****************************************************/

// ball clip timeline ***Frame 5***

gotoAndPlay(2);

Keep in mind that each ball will have to test collision with each of the others, but reverse testing is unnecessary ( a.hitTest(b) == b.hitTest(a) ).

Also, the hitTest() method used this way tests against the bounding box and not the shape of the clips, so the circles will bounce off imaginary corners surrounding them.

Finally, the above speed adjustments are non-realistic as i'm sure you've realized, but they are a start, and solve your overlapping problem.

This is an interesting problem, and i'll probably tinker with it, and will post if i figure out anything useful.

In the meantime, please post if you have any difficulties with the above code, and good luck with your lively interface...=)

Mike
 
Hi Mike,

Just want to say thanks for taking the time to reply to my posting, will give it try later !

Zickler
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top