Archived

This topic is now archived and is closed to further replies.

Question about Inherited Objects

This topic is 5589 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I have a base class called CDestructibleObject. From this class, I derive other classes: CShip and CAsteroid. Obviously, each of these derived classes has its own unique attributes and methods. Right now, I''ve got an array of each object type to represent objects flying around in my game. Now I want to perform collision detection in my game, and I want any destructible object to be able to collide with any other destructible object. I could do it this way: 1. Loop through each CShip object 2. Detect collisions with all other CShip objects 3. Detect collisions with all CAsteroid objects 4. Loop through each CAsteroid object 5. Detect collisions with all other CAsteroid objects However, this seems REALLY klunky and inefficient to me. I thought of another possible way--since these are all derived from CDestructibleObject, would it be possible to create an array of CDestructibleObjects, with each object referencing either a CShip or a CAsteroid? That way, I could put collision detection code in the CDestructibleObject class, and only have to loop through one array to detect collisions. Would this be a good way to do it? If so, could someone please give me some suggestions on how to set it up and how to reference each object? Is there a better way? Thanks in advance!
I am always open to feedback, positive or negative...

Share this post


Link to post
Share on other sites
Yes, you should absolutely do that. If the collision detection methods aren't similar in CShip and CAsteroid, maybe you should consider doing the collision detection method virtual and then define the method in each of the child classes. Then you would be able to do this:

CDestructibleObject *Array[2];
Array[0] = new CShip();
Array[1] = new CAsteroid();

for (int index = 0; index < 2; index++)
{
Array[index]->DetectCollisions();
}

Pretty neat, huh?



[edited by - xjussix on August 27, 2002 7:44:59 AM]

Share this post


Link to post
Share on other sites
I figured I could do something like that, but I didn''t think it would be THAT easy... thanks!


I am always open to feedback, positive or negative...

Share this post


Link to post
Share on other sites
NP! If you have a c++ book or want to search the net, there''s a keyword: BINDING. :D

Here''s a very good thread about it: http://www.gamedev.net/community/forums/topic.asp?topic_id=55599

Share this post


Link to post
Share on other sites