Game Development Community

Collision callback on specific objects rather than all collide-ables?

by Joe Williams · in iTorque 2D · 05/17/2012 (9:53 pm) · 3 replies

I have a game that's sort of like pachinko. Balls fall down and bounce between diamond-shaped objects, and the player tries to get them to fall into specific spots at the bottom. I want the balls to combine when they hit, and that code is fine. However, the balls still trigger a collision callback when they hit the diamonds, as well. Is there any way to have them only trigger a callback when they hit each other?

Here's the code in question. The balls call the "evalCombine" function on whatever they hit. I could reverse it, and have them call it on themselves, but the problem would still be the same since I'd have to get some info from the object that was hit. Anyone have any ideas on how to solve this?

function fball::onCollision( %srcObject, %dstObject, %srcRef, %dstRef, %time, %normal, %contacts, %points )
{
    %dstObject.evalCombine( %srcObject.color1, %srcObject.color2, %srcObject.getPositionY() );
}

function fball::evalCombine( %this, %newCol1, %newCol2, %YPos )
{
    if( %yPos > %this.getPositionY() )
    {
        %this.goHome();
    }
    else
    {
        if( %this.color1 == %newCol1 )
        {
            if( %newCol2 > 0 )
            {
                if( %newCol2 == %this.color2 )
                {
                    %this.myScore++;
                    %this.myScore++;
                }
                else
                {
                    %this.goHome();
                }
            }
            else
            {
                %this.myScore++;
            }
        }
        else
        {
            if( %this.color2 > 0 )
            {
                %this.goHome();
            }
            else
            {
                %this.color2 = %newCol1;
                %this.setColor();
            }
        }
    }
}

#1
05/17/2012 (10:58 pm)
Modify their collision layers to only collide with those layers you want and assign them to corresponding layers
#2
05/18/2012 (5:56 am)
@Joe - Marc's suggestion is correct. You might also have to modify the collision group.
#3
05/18/2012 (6:40 pm)
I think I might not have explained myself well. The fball has to collide with the diamond in order to bounce off of it. If I set the fball to not collide with the diamond layer, then it will just fall straight down.

Anyway, I solved the problem by having a separate generic scene object attached to each fball. That object only collides with itself and that's where the collision callback is. It then calls the evalCollide function on it's owner with the other owner's attributes.