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:

[COLOR=#800080]http://www.imanagerlive.com/bubble.swf[/COLOR]

And here’s a link to the fla file

[COLOR=#0066cc]http://www.imanagerlive.com/bubbles1.zip[/COLOR]

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)…


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*[yPos]!=null)
    {
    if(bubbleGrid*[yPos].hitTestPoint(futureX,futureY))
     {
     if(bubbleGrid*[yPos+1]==null)
      {
      bubbleGrid*[yPos+1] = ballToFly;
      snapToTile(i,yPos+1,bubbleGrid*[yPos+1]);
      }
     else
     if(bubbleGrid*[yPos+2]==null)
      {
      bubbleGrid*[yPos+2] = ballToFly;
      snapToTile(i,yPos+2,bubbleGrid*[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…

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. :slight_smile:

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. :stuck_out_tongue:

Thanks for the reply, my friend deleted the things I put up on their server :frowning: 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 :slight_smile: Game is pretty much finished now :smiley: