This is a conceptual question here. My goal is a first-person shooter, similar to X-Wing or Freespace Descent.
The situation is that I have a working game that consists of a node controlled by the user, able to fly around in space; several target nodes; and the ability to spawn bullets, which fly straight in the direction the user is facing.
My question is about how to handle collisions. I’ve read over the manual and understand the basics of CollisionHandlers, but I need help figuring out where they should be, how many to have, how and when to add my solids, etc.
I have several classes to implement ships, bullets, etc. Each one will create its own visible geometry and tasks to handle movement (and user input, for the player’s fighter), so that all I have to do is make a new instance of a ship or bullet, and everything else is automatic.
I am now trying to add collision detection. I have considered several possibilities:
Each ship has a collision manager.
Each bullet has a collision manager.
There is one global collision manager.
But there are problems with each of these. My main problem is, the collision events specify the nodes involved in the collision, but I need to know the ship that got hit (which owns the node, rather than the other way around), or the bullet that hit it (same problem). Before I get too far into this, I would like to hear some thoughts on where these would normally go. My fear is that I’ll have to write my own collision code (I know C++, so it wouldn’t be terrible–just lots of work, and probably not as good as it could be).
Sorry, I’m not exactly a professional, but I believe I might be of assistance as to your last question, because I had the same problem as well.
What I did was to set unique tags to the models in the class, a entity Id of sorts, and store the instances in an entity dictionary.
Then everytime I needed to get that particular instance (e.g a bullet), I’ll just use the nodepath that I got, trace it back to the model, and get the entity Id, and use the dictionary to locate that particular instance.
Thats what I did at least, and it worked out quite well. Basically.
A unique identifier tagged to each model of the instances (created in the class initialization)
An entity dictionary on the World Class which contains all the instances I need to check against
I see. So the id code might be something like ‘talon fighter 226’ or even just a number. All I need to do is ensure that the codes will be unique, and look it up in the dictionary. And of course, some cleanup code if the fighter gets destroyed, etc.
Thanks. I knew there had to be an easier solution than I was coming up with.
you can use python tags and tag your bullet with the node of the ship which fired it. this way you dont need any lists or dictionaries.
the methods of collisionEvent should provide you with all other functionality.
for all my applications i went with one global collision manager
Thanks for the tip about tagging the bullets. I think it will simplify a good bit of my code.
Maybe I’m misunderstanding you, but I still need the dictionary for ships. I can get references to the nodes that collided from the collisionevent, but not references to the objects that own the nodes (those objects are where basically everything is, from health, to name, to player that owns them, to the tasks that carry out things like movement and control). Should I be organizing things differently then? Is there a way to use tags to handle the info I’m handling using objects?