Game Development Stack Exchange is a question and answer site for professional and independent game developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

This photo illustrates the environment:

I'll shoot the cannon, it'll bounce off the wall and it's SUPPOSED to stick to the bubble. It does at pretty much every other angle. The problem is always reproduced here, when hit off the wall into those bubbles. It also exists in other cases, but I'm not sure what triggers it.

What actually happens: The ball will sometimes set to the wrong cell, and my "dropping" code will detect it as a loner and drop it off the stage.

*There are many implementations of "Frozen Bubble" on the web, but I can't for the life of me find a good explanation as to how the algorithm for the "Bubble Sticking" works. *

I see this:

But I can't figure out the algorithims... could anyone explain possibly the general idea behind getting the balls to stick?

Code in question:

    //Counstruct our bounding rectangle for use
    var nX = currentBall.x + ballvX * gameTime;
    var nY = currentBall.y - ballvY * gameTime;
    var movingRect = new BoundingRectangle(nX, nY, 32, 32);
    var able = false;

    //Iterate over the cells and draw our bubbles
    for (var x = 0; x < 8; x++) {
        for (var y = 0; y < 12; y++) {
            //Get the bubble at this layout
            var bubble = bubbleLayout[x][y];
            var rowHeight = 27;

            //If this slot isn't empty, draw
            if (bubble != null) {
                var bx = 0, by = 0;

                if (y % 2 == 0) {

                    bx = x * 32 + 270;
                    by = y * 32 + 45;

                } else {
                    bx = x * 32 + 270 + 16;
                    by = y * 32 + 45;

                var targetBox = new BoundingRectangle(bx, by, 32, 32);
                if (targetBox.intersects(movingRect)) {
                    able = true;




   cellY = Math.round((currentBall.y - 45) / 32);

if (cellY % 2 == 0)
    cellX = Math.round((currentBall.x - 270) / 32);
    cellX = Math.round((currentBall.x - 270 - 16) / 32);

Any ideas are very much welcome.

Things I've tried:

Flooring and Ceiling values Changing the wall bounce to a lower value Slowing down the ball

None of these seem to affect it. Is there something in my math I'm not getting?

share|improve this question

closed as too localized by Sean Middleditch, bummzack, Trevor Powell, Ali.S, API-Beast Mar 16 '13 at 0:10

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.If this question can be reworded to fit the rules in the help center, please edit the question.

up vote 6 down vote accepted

Model the playing field as a hexagon grid. Each bubble is actually a hexagon shape for its connections:

enter image description here

Each flat side is where a bubble can connect. When a bubble touches another bubble, snap the bubble in motion to the nearest hexagon grid location.

Finding the nearest is essentially a picking operation, so the math for finding where the bubble will stick is the same. You can find some great answers for that here.

share|improve this answer
I learned this a while back (this was asked in August of last year) and this was pretty much the solution. Flooring the coordinate was simply not enough, the math was off. Thanks for taking the time to reply, anyway. I'm sure it'll be helpful to someone in the future who was a bit naive like me. :) – Vaughan Hilts Mar 15 '13 at 20:14
No worries, I saw that it was unanswered, so I took a shot at it. I figured you'd already solved it, you should answer it yourself! You can still provide another answer with how you actually solved it. – Byte56 Mar 15 '13 at 23:58

Not the answer you're looking for? Browse other questions tagged or ask your own question.