# Thread: How to handle collisions....

1. 2
posts
Registered User

## How to handle collisions....

Hi Guys.

I've been learning flash over the last couple of weeks (using AS3) and have tried to write a game in it. However I've started to stumble on a problem I'm finding really confusing now. I've been making a bubble game where you shoot bubbles out and other bubbles and when you get a group of 3 or more bubbles, they explode (not a great explanation I know but the link to the swf should give you an idea). Right now it's at the start of development, you can shoot bubbles upwards, but none will explode and there is only one bubble type. However I'm trying to get the physics right and the collisions are confusing me. Bubbles are colliding into each other in a funny way and I can't figure out exactly how would be a good way of doing it.

Anyways heres a link to the swf:

http://www.imanagerlive.com/bubble.swf

And here's a link to the fla file

http://www.imanagerlive.com/bubbles1.zip

I know I've done it a bit stupidly without using classes properly and having functions each on different layers, I think I just find it less confusing this way whilst im developing stuff...

Anyway I believe this to be where I've got big problems (The FLA file has all this code in and it's probably easier to understand looking at that than this)..

Code:
```function doBallMovement() /* When a ball is shot, this function is called */
{
var futureX=ballToFly.x+(sine[ballAngle]*ballSpeed);
var futureY=ballToFly.y+(cosine[ballAngle]*ballSpeed);

// Get what current array tile we're in - roughly
var xPos = Math.round(((futureX-gridStartX)/squareWidth));
var yPos = Math.round((futureY-accumulatedMovement-gridStartY-(squareHeight/2))/squareHeight);
var yPosTip = Math.round((ballToFly.y-accumulatedMovement-gridStartY-(squareHeight/2))/squareHeight);

if(((yPos>0)&&(yPos<gridHeight+extraVerticalBuffer))&& /* if we can collide, place it here THIS PART IS WRONG!! */
((xPos>0)&&(xPos<gridWidth)))
{
for(var i=xPos-1;i<xPos+1;i++)
{
if(bubbleGrid[i][yPos]!=null)
{
if(bubbleGrid[i][yPos].hitTestPoint(futureX,futureY))
{
if(bubbleGrid[i][yPos+1]==null)
{
bubbleGrid[i][yPos+1] = ballToFly;
snapToTile(i,yPos+1,bubbleGrid[i][yPos+1]);
}
else
if(bubbleGrid[i][yPos+2]==null)
{
bubbleGrid[i][yPos+2] = ballToFly;
snapToTile(i,yPos+2,bubbleGrid[i][yPos+2]);
}
setNext();
}
}
}
}

if(futureX<gridStartX) /*Bounce off the left */
{
ballAngle = 180-(ballAngle % 180);
while(ballAngle<0)
ballAngle+=360;
}
if(futureX>gridStartX+(gridWidth*squareWidth-(squareWidth/2))) /*Bounce off the right*/
{
ballAngle = 360-ballAngle;
while(ballAngle<0)
ballAngle+=360;
}

if(yPos<0)
return;
ballToFly.x=futureX;
ballToFly.y=futureY;
}```
Many thanks in advance for any ideas people might have, I figure it can't be too hard as there's so many of these games around on the web...

2. Hi,

The links don't work (404 error), but it's could be my firewall. So I can't really comment on your game--yet--but here is what I take from your code, which could be totally wrong.

First off, I would put that for-loop in a separate function because you ened up running hitTest against yourself when you set:
for(var i=xPos-1;i<xPos+1;i++) <- runs when i = xPos -1, i = xPos

I believe you want to test when i = xPos-1 AND xPos+1 (adjacent blocks).

Also, after you hit test against one ( Left or Right) you probably don't need that other hitTest since it's done already.

As I said, I am babbling without seeing your game so ignore me if it doesn't apply.

3. 2
posts
Registered User
Thanks for the reply, my friend deleted the things I put up on their server Sorry about that. I decided using hit test was stupid and I should do the whole collision thing using current grid position and future grid position and figuring out whether I need to stop a ball where it is or not. Anyways thanks for taking a look at the code and giving me some tips Game is pretty much finished now

##### Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•