Allegro.cc - Online Community

Allegro.cc Forums » Game Design & Concepts » collision handling

This thread is locked; no one can reply to it. rss feed Print
 1   2 
collision handling
Matt Weir
Member #7,476
July 2006
avatar

Ah, but if you had 1000 particles with only the last one flagged as on it would take 1000 iterations to get to it. If you used a list where you removed dead particles it would take only one. You can go in circles trying to figure which is better but it's 'Horses for courses' in the end.

BTW. My particle engine does the same as your's (uses flags) but I use what I call 'Particle Trees' which are groups of particles (ie. explosion tree, snow tree). When all the particles are dead the tree is killed. Particle 'Trees' are created in the Entity List (along with ememies, items etc) which is a linked list (beacause it changes a lot dynamically). Best of both worlds. ;)

_lican
Member #8,330
February 2007

It's funny, but I had the same problem and with the same type of objects. Let's say it was something similar to Asteroids. Every object in the game was moving so every collision "A with B" is important. I partially solved the collision response problem without the dynamic_cast stuff. It should be faster. Here's what I've got:

1class Object
2{
3public:
4 virtual bool Call( Object *that );
5 virtual bool Bounce( Object *that );
6 virtual bool Bounce( Bonus *that );
7 virtual bool Bounce( Bullet *that );
8 virtual bool Bounce( Ship *that );
9};
10 
11class Bullet: public Object
12{
13 bool Call( Object *that );
14 bool Bounce( Object *that );
15 bool Bounce( Bonus *that );
16 bool Bounce( Bullet *that );
17 bool Bounce( Ship *that );
18};
19 
20// the same for other classes...

Than in the game:

if( Collision(object1,object2) )
{
  // if both functions return true the physics response is applied
  if( object1->Call(object2) && object2->Call(object1) )
    Physics(object1,object2);
}

And here's how it works. The Call function calls the Bounce function of the other object, i.e.:

bool Bullet::Call( Object *object )
{
  object->call(this);
  return false; // the bullet dies so it doesn't need to bounce off
}

Since the functions are overloaded there is no need to check the object type. If the bullet::call was called and the object was a ship then the ship::bounce( *bullet ) function will be called. I hope it's quite clear.

The obvious drawback is that when you have X different objects you'll end up writing X^2 functions. I have some other solutions, but I have to think them over.

ImLeftFooted
Member #3,935
October 2003
avatar

Quote:

Ah, but if you had 1000 particles with only the last one flagged as on it would take 1000 iterations to get to it.

This argument doesn't hold up because I loop every particle updating them every frame. Things like making the particle move etc. During that 'update' I also check for dead and flag if necessary.

On top of that, iterating to the last element in a vector is two instructions: Adding and a pointer lookup. A list lookup of the end is one instruction: a pointer lookup.

Since a pointer lookup will halt the branching, the time needed for these two operations is exactly the same. *

But no one ever needs to access particles in that fashion anyway.

  • Corrected, although I still feel like I'm wording this slightly wrong.

Matt Weir
Member #7,476
July 2006
avatar

Quote:

This argument doesn't hold up because I loop every particle updating them every frame...

I apologise, I misinterperated exactly how you were doing it. :) I think we're both on the same track or very close anyway. Carry on all...

 1   2 


Go to: