Everybody! This is important. In a few days, these forums will be moving over to using the totally sweet Discourse platform. To ensure this migration happens smoothly with no loss of content, these forums are currently in a read-only mode. I do apologize for the inconvenience.

There is never a good time to turn the forums off for an extended period of time, but I promise the new forums will be a billion times better. I'm pretty sure of it.

See you all on the other side in a few days, and if you have any (non-technical) questions, please e-mail me at kirupa@kirupa.com. For technical questions, try to find a tutorial that corresponds to what you are looking for and post in the comments section of that page.

Cheers,
Kirupa

Results 1 to 3 of 3

Thread: How to handle collisions....

  1. #1

    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. #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. #3
    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

Thread Information

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
  •  

Home About kirupa.com Meet the Moderators Advertise

 Link to Us

 Credits

Copyright 1999 - 2012