   scene graph and game objects
  hi there, I've been thinking about how to solve this problem for a while and I still haven't found a good solution, anyway...In my game I was thinking about creating a 'GameObject' interface or abstract class which would contain a lot of functions that would do something to the object, for e.g:class GameObject{public:   virtual void press() {}   virtual void takeHit(int damage) {}   etc.};class Enemy : public GameObject{public:   void takeHit(int damage)   { health -= damage; if(health < 0) {    ... }   }private:   int health;};this means I can create objects that will immediatley respond to the events it chooses to respond to and ignore those it doesn't, but the problem is trying to get this system to work alongside the scene graph.I have the scene graph setup where each node contains an array of children nodes and two pure virtual functions: render and update (update for animation).class SceneNode{public:   virtual void render() = 0;   virtual void update(int dTimeMs) = 0;//protected:   std::vector<SceneNode*> children;};My problem would be solved if I took all the functions from GameObject and dropped them into the SceneNode class but I need a way to separate this because the SceneNode class will be in a different library from the GameObject, The way I'm thinking about doing it would be something like this:// librarytemplate <class T>class SceneNode : public T{public:   ...};template <class T>class MeshSceneNode : public SceneNode<T>{public:   ...};// gameclass Enemy : public MeshSceneNode<GameObject>{public:   virtual void takeHit(int damage)   { health -= damage; if(health < 0) {    ... }   }private:   int health;};typedef SceneNode<GameObject> game_object;int main(){   game_object* obj = new Enemy();   obj->children.push_back(new Enemy());   // etc.   return 0;}although... this means every implementation of a scene node needs to be templated and all implementation will need to reside in header files.What do you think? is there a better way to solve this?thanx.[Edited by - staticVoid2 on April 12, 2009 9:13:06 AM]
  don't link the render data and logic data inherently.... the render data need never know about the logic datathe logic data inherently drives the render dataso leave it as 2 separate entities.come up with a method such that logic data can "update" render datai could give you a direct solution however there is more value in your figuring it out than me handing over the solution.   ;  I understand what you mean but I just can't implement it the correct way, I've tried doing something like this:class GameObject{public:   void takeHit(int damage) = 0;};class Enemy : public MeshSceneNode, public GameObject{public:   void takeHit(int damage)   { health -= damage; if(health < 0) {    MeshSceneNode::setColor(255, 255, 255, 255);    // etc. }   }};int main(){   GameObject* obj = new Enemy();   return 0;};but It doesn't work, this is the only way I can think of where your object gets access to the scene node and is also considered to be a GameObject, but if I treat it as a GameObject then there is now no render method.;  it looks like you are limited by your use of C++ and OOP.consider linking the game object and render object using a simpler method.;  do you mean like this:class Enemy : public IGameObject{public:private:   MeshSceneNode* mesh;};this does seem simpler now that I think about it, thanx.   ;  You would likely benefit from checking out the source code (and possibly reading the associated books) that can be found on www.geometrictools.com.  The code demonstrates a clean solution to the problem you are describing, and there are lots of other goodies on that site (plus the source code is free!).   ;  I would definitely do a mesh component like you have in your last example. You'll want all kinds of components. Audio managers, effect managers, physics bodies, AI components.You should look up an component based entity system. It's very handy when you're going to define a bunch of complex objects like game objects, and it's very easy to make the whole thing data driven and even automatic editing tools.For instance.Entity *MakeEnemy(){  Entity *e = new Entity("Enemy1");  Renderable *r = new Renderable("fatty.mesh");  CharacterBody *b = new CharacterBody();  AIScript *ai = new AIScript("commander.ai");  e->AddComponent(r);  e->AddComponent(b);  e->AddComponent(ai);  return e;}then your take hit function would be like thisvoid TakeHit(Entity *e, ...){  AIScript *ai = e->GetComponent<AIScript>();  if (ai) ai->CallFunction("TakeHit", ...);}void SetTransparency(Entity *e, float a){  Renderabl *r = e->GetComponent<Renderable>();  if (r) r->SetAllMaterialAlpha(a);}void PlayEffect(Entity *e, string &effect){  EffectManager *fx = e->GetComponent<EffectManager>();  if (fx) fx->PlayEffect(effect);}These are all very very basic examples..   ;  sorry for the long reply, but I've ran into a few problems using the solution from my last post.The first problem is that when I want to for e.g. select an object to send one of these messags to I would need to perform a ray intersection test with the object and the ray, which is more of a spatial problem that belongs to the scene graph imo, I could just iterate through all of the objects and call an 'intersectsRay' method but what if I have 1000 'GameObject's stored in an array? - this means I would have to iterate through all of them checking for an intersection when, if I'm using an octree for example, I know that most of them will never intersect with the ray, but because this data is stored in the scene graph I can't access it?The second problem is that objects will never get a chance to render themself, they will always depend on some instance of a scene node to do that for them, which means if one object required one triangle to be rendered you would have to either load a separate mesh or create a new triangle scene node class, when all it takes to render a triangle is one line of code.The third problem is saving/loading the data, I would first need to assign id's to each scene node, serialize the scene graph and then serialize all game objects, when I need to load this data back up I would have to load the scene graph, then load the game objects and get each game objects scene node from it's id.I'm currently thinking about just removing the entire scene graph and letting any game implement its own, i.e. put all the properties of a scene node into the game object class. I know there is probaly a better method but this one seems the simplest so far.   