Sure!
All right. If you played my little game you know the drill already. You are a little square and whilst fetching your target, other squares (enemies) are in the way to hit you and kill you. The point is that I wanted to make the game look a bit more fancy and therefore decided to make enemies collide with each other as well as with the target and player.
The effect of two colliding enemies would be an explosion.
My explosion is a circle being drawn on screen with radius 0.
The Radius grows at 1 Pixel per Frame up to 20.
The explosion was something like..
Code: Select all
if ( Index < 20 )
{
gfx.DrawCircle( x,y,Index,255,255,255 );
Index ++;
}
So, my code was something like:
Code: Select all
//loop through all enemies enemies
for ( int index = 0; index < nEnemyBox; index++ )
{
//search for ALIVE enemies
if ( EnemyBox[ index ].IsAlive == true )
{
//only draw enemies with some health left
if ( EnemyBox[ index ].health > 0 )
{
gfx.DrawBox( EnemyBox[ index ].x,EnemyBox[ index ].y,EnemyBox[ index ].size,EnemyBox[ index ].Red,EnemyBox[ index ].Green,EnemyBox[ index ].Blue );
}
//make explosion
else if ( Index < EXPLOSIONRANGE )
{
gfx.DrawCircle( ( EnemyBox[ index ].x + ( EnemyBox[ index ].size / 2 ) ),( EnemyBox[ index ].y + ( EnemyBox[ index ].size / 2 ) ),SecondGuyExplosionIndex,255,255,255 );
ExplosionIndex ++;
}
//after explosion, resets counters and KILL enemy
else
{
ExplosionIndex = 0;
}
}
}
Explosions did not work out well at all and Indus just found out why.
The enemies are revived once at a time but they die in couples!! an enemy can't magically disappear and collide with himself. So, when the two happy guys enter their loop, they 'share' the explosion counter.
'ExplosionIndex' increases two times per frame, and this gives extra speed to what's happening.
Two explosion with radius increasing 2 pixels per frame (instead of 1) is what we get.
Now, if this was the only issue I would have never realised that the code was wrong and Indus wouldn't have looked at my code and I would still have the bug. The major bug is that there is a third explosion occurring. It happens because of the shit idea of making the two guys share the counter and because of how incredibly complicated and intrinsically wrong my logic is.
When the 2 guys collide, they enter the explosion loop.
The first one grows the counter to 1, the second one to 2, etc..
The loop runs all the way about 20 times..
The first one grows the counter to 19, the second one to 20..
20 is NOT lower than 20 so the first colliding guy exits the loop AND RESETS the counter.
..Therefore the second guy will explode again (this time at 1 per frame
)
Now, since I am not smart but rather stupid, I didn't find a proper solution, but a decent one.
My solution is: when two guys collide, I remember their index and then I make two different explosions for each of them (getting rid of all of that crappy Alive-AlmostAlive-Dead logic thing..)
Now the code is:
Code: Select all
//loop through all enemies enemies
for ( int index = 0; index < nEnemyBox; index++ )
{
//search for ALIVE enemies
if ( EnemyBox[ index ].IsAlive == true )
{
//only draw enemies with some health left
if ( EnemyBox[ index ].health > 0 )
{
gfx.DrawBox( EnemyBox[ index ].x,EnemyBox[ index ].y,EnemyBox[ index ].size,EnemyBox[ index ].Red,EnemyBox[ index ].Green,EnemyBox[ index ].Blue );
}
//make the first collider explode
else if ( index == FirstGuyColliding && FirstGuyExplosionIndex < EXPLOSIONRANGE )
{
gfx.DrawCircle( ( EnemyBox[ index ].x + ( EnemyBox[ index ].size / 2 ) ),( EnemyBox[ index ].y + ( EnemyBox[ index ].size / 2 ) ),FirstGuyExplosionIndex,255,255,255 );
FirstGuyExplosionIndex ++;
}
//make the second colliding enemy explde
else if ( index == SecondGuyColliding && SecondGuyExplosionIndex < EXPLOSIONRANGE )
{
gfx.DrawCircle( ( EnemyBox[ index ].x + ( EnemyBox[ index ].size / 2 ) ),( EnemyBox[ index ].y + ( EnemyBox[ index ].size / 2 ) ),SecondGuyExplosionIndex,255,255,255 );
SecondGuyExplosionIndex ++;
}
//after explosion, resets counters and KILL enemy
else
{
FirstGuyColliding = NENEMYBOX + 1;
SecondGuyColliding = NENEMYBOX + 1;
FirstGuyExplosionIndex = 0;
SecondGuyExplosionIndex = 0;
EnemyBox[ index ].IsAlive = false;
}
}
}
The code works nicely and it looks nicer too.
I lose that bug and I lose that Alive-AlmostAlive-Dead thing. The problem is that there is another minor bug but it's not worth any of my time. (I spent about a day designing this explosion thing)
It's so irrelevant that if you play the game and I don't tell you that there is a bug, you are very much likely not to spot it.
In the occurrence of multiple collisions at the same time (or within 20/mylaptopframepersec seconds)
the first collision will show up and properly, the others will be completely ignored.
In fact I suck at coding!
No.. seriously.. it's still not perfect but it works.