Size Does Matter

I just found a bug: when the bot moved really fast it could pass through the wall. How? It was a combination of bot’s size, wall’s size, and speed.

I checked for collision with this code:

var inst = instance_place( x+xspd, y, obj_wall_collision );
if ( inst != noone )
{
    // Collision, adjust position and speed
}

The size of the bot was 8×8 while the wall’s was 32×32. If the bot’s speed is too fast, the collision checker would return false since the position added by speed is further than the wall.

I could add a precise collision checking such as:

var inst;
var xtarget = x+xspd;
while ( abs(xtarget-x) > 0 )
{
    inst = instance_place( x + sign(xspd), y, obj_wall_collision );
    if ( inst != noone )
    {
        // Collision, adjust position and speed
        break;
    }
    x += sign(xspd);
}

but then it would be too heavy on the processor, since the code will be called each step.

My simple solution was adding more thickness to the wall. It worked for now, but if the bug still occurs, I might need to think of other way of checking for collision.

Leave a comment