Collision Detection and Targets

I guess I’ve got a basic understanding of collision detection so far but ive run into a problem. Ive searched the forums for some time and didnt find anything particulary helpful.

I’m using an event handler and ive got bullets (those are stored as classes in a List) and enemies (stored as classes in a dictionary with the keys to them stored in a List) so far.
Now heres the problem: how can i refer to which enemy i hit?

one way would be to give each enemy an individual collision node name and each bullet as well and then make an accept statement for each bullet and enemy and return the identifiers for both by the accept statement. well thats obviously not very effecitve.

how i would like things to work:

create a few enemies, add collision solids to them

spawn a bullet and add the cnode to the traverser(the important part here would be to just use one self.accept statement.)

if my bullet hits, return the indexnumber of the enemy and bullet to calculate damage etc.

So my questions are: is that even possible? what would be the most efficient way to do this? how would you set up the collision detection in such a scenario?
returning the node of the hit enemy and searching through the enemylist to find the correct one would be possible too, but quite cpu hungry as well i think.

Thx in advance!

When the collision is reported, either via a CollisionHandlerQueue or via a collision event, you get back a CollisionEntry object which contains details about the collision (this object is the sole parameter of the event function).

In particular, you can call entry.getIntoNodePath() and entry.getFromNodePath() to return the two objects that collided. You can in turn use nodePath.setTag() and getTag() to store data like an index number along with each enemy.


Thanks! of course the setTag and getTag functions do the trick :slight_smile: