Removing the object after collision

I have collision detection function:

void Level::logic()
{
//check bullet-enemy collision
for (int i = 0; i < 10; i++)
{
for (int j =0; j < 10; j++)
{
if( check_collision( bullets[i]->GetCollisionBox(), enemies[j]->GetCollisionBox() ) == true )
{

}
}
}
}

the question is: how can I remove the enemy from the array,i.e. stop the drawing of the enemy?

That’s exactly why you don’t use an array. :wink:

If you use a linked list instead, you can just remove the enemy from the list,
and put it back into the pool, delete it or whatever. (A LIFO stack of pre-
allocated objects is a nice and handy solution that avoids abusing the memory
manager and constantly reinitializing objects in-game.)

Of course, there are various ways of doing this with arrays as well, if you
have a good reason to use arrays. You can mark each object with an "in use"
flag, or you can add an “objects in use” variable to each array.

Generally, you’ll need to keep objects in order (for correct rendering) for
this sort of stuff, making arrays rather inefficient, at least for large
numbers of objects.

Anyway, this has very little to do with SDL, and very much to do with general
programming. In the case of C++, there are various template libraries that may
come in very handy for this sort of stuff.On Friday 01 January 2010, at 17.56.25, “dekyco” wrote:

I have collision detection function:

void Level::logic()
{
//check bullet-enemy collision
for (int i = 0; i < 10; i++)
{
for (int j =0; j < 10; j++)
{
if( check_collision( bullets[i]->GetCollisionBox(),
enemies[j]->GetCollisionBox() ) == true ) {

}
}
}
}

the question is: how can I remove the enemy from the array,i.e. stop the
drawing of the enemy?


//David Olofson - Developer, Artist, Open Source Advocate

.— Games, examples, libraries, scripting, sound, music, graphics —.
| http://olofson.net http://kobodeluxe.com http://audiality.org |
| http://eel.olofson.net http://zeespace.net http://reologica.se |
’---------------------------------------------------------------------’

the question is: how can I remove the enemy from the array,i.e. stop the
?drawing of the enemy?

That’s exactly why you don’t use an array. :wink:

Another option is:

Do not use constant figures to express the number of bullets and
enemies. Create two variables:

size_t numberOfBullets = 10;
size_t numberOfEnemies = 10;

When a bullet or enemy is deleted, decrement the corresponding
numberOf variable, and if there are still any items remaining in the
list, copy the last item to the position where the deleted item was
previously stored:

void Level:DeleteEnemy(size_t id) {
if (–numberOfBullets)
bullets[id] = bullets[numberOfBullets];
}

Of course as Bob pointed out, you’ll need to keep your lists sorted,
making arrays a poor choice because sorting an array can be much more
expensive than sorting a list.

Generally, you’ll need to keep objects in order (for correct rendering) for
this sort of stuff, making arrays rather inefficient, at least for large
numbers of objects.

Another important reason for sorting is so that you can avoid
explosive growth of collision tests! It may seem complex when you
begin writing your first collision dynamics code for arcade games. You
may want to consider finding an existing library out there to help you
write that kind of game!

One more thing: A lesson I learned a long time ago now is that you
shouldn’t put bullets and enemies in separate containers. You may
decide later that you want some bullets to collide with other bullets,
or some enemies to collide with some other enemies, and then you’ll
have to refactor your code. Best instead to make a generic
"CollidableObject" class, and have each instance of that class know
what kind it is.On Fri, Jan 1, 2010 at 12:09 PM, David Olofson wrote:

On Friday 01 January 2010, at 17.56.25, “dekyco” wrote:


http://codebad.com/

If you like to stick to arrays, here are some thoughts:
How about adding a simple boolean isActive which can be tested and
updated during the check loop. The computationally expensive function
should be your check_collision() routine - especially if you were to
implement pixel-perfect detection. And if you are worried about memory
recovery as enemies are being removed, the arrays can be compacted from
time to time (i.e. does not have to be done every iteration) using a
reallocation loop before or after the detection has run. Or you could
use a static-list algorithm that allows for removal . Here is an example
in pseudocode: list=item[1…N]; remove(X){if (X<N){item[X]=item[N];}
N–; realloc(N*itemSize);}

dekyco wrote:> I have collision detection function:

void Level::logic()
{
//check bullet-enemy collision
for (int i = 0; i < 10; i++)
{
for (int j =0; j < 10; j++)
{
if( check_collision( bullets[i]->GetCollisionBox(),
enemies[j]->GetCollisionBox() ) == true )
{

}
}
}
}

the question is: how can I remove the enemy from the array,i.e. stop
the drawing of the enemy?


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

Thank you very much,problem is solved.

dekyco