Scene graph and bounding boxes

Started by
1 comment, last by aliyak 15 years, 4 months ago
I am using a simple scene graph I found here and I made very little modification to it. The thing is I want to store my bounding boxes along with the geometry also , my bounding box simply holds center of the object(x,y,z coords and height,width,depth of the object) the thing is I could not find a way to mutply and move my bb s to where they should be by multiyle them to parent transformation matricies. Anyone have any idea how I can do it ? the code is like this: tvector is a resizeable array class incase you dont know what it is. The original code is written by Garret Foster and can be found here: http://www.gamedev.net/reference/programming/features/scenegraph/default.asp

#ifndef _CSCENENODE_H
#define _CSCENENODE_H
#include "tvector.h"
#include "BoundingBox.h"
#include <iostream>
#include <GL/glut.h>
using namespace std;
//Scene NOde Base Class
class CSceneNode
{
public:
	CSceneNode() { }										//Constructor
	virtual ~CSceneNode() { Destroy(); }					//Destructor
	void Release() {  }										//Free Memory -not working as intended *delete this*
	virtual void Update()									//update the tree rooted at this node recursively
	{
		for( int x =0;x<m_lstChildren.size(); x++)
		{
			(*m_lstChildren[x]).Update();
		}
	}
	void Destroy()											//Destroy node and the tree rooted at the node -no working as intended *delete this*
	{
		for( int x =0;x<m_lstChildren.size(); x++ )
			(*m_lstChildren[x]).Release();
		m_lstChildren.clear();
	}
	void AddChild( CSceneNode* pNode )						//Add Child to this node
	{
		m_lstChildren.push_back(pNode);
	}
protected:
	tvector<CSceneNode*> m_lstChildren;						//Childs vector
};

class CGeometryNode: public CSceneNode
{
public:
  CGeometryNode(BoundingBox b) {box=b; }
  ~CGeometryNode() { }

  void Update()
  {
	//object drawn here

	//I want to multiply transformation matricies with BoundingBox here somehow

  CSceneNode::Update();

  }
protected:
 BoundingBox box;
};
class CRotationNode: public CSceneNode
{
public:
  CRotationNode(float rotangle, float x1,float y1, float z1) {rotAngle=rotangle;x=x1;y=y1;z=z1; }
  ~CRotationNode() { }

  void Update()
  {
  glPushMatrix();
  glRotatef(rotAngle,x,y,z);

  CSceneNode::Update();
  glPopMatrix()
  }
protected:
	float rotAngle,x,y,z;
};
class CScaleNode: public CSceneNode
{
public:
  CScaleNode( float x1,float y1, float z1) {x=x1;y=y1;z=z1; }
  ~CScaleNode() { }

  void Update()
  {
	  glPushMatrix();

  glScalef(x,y,z);

  CSceneNode::Update();
    glPopMatrix();
  }
protected:
	float x,y,z;
};
class CTranslateNode: public CSceneNode
{
public:
  CTranslateNode( float x1,float y1, float z1) {x=x1;y=y1;z=z1; }
  ~CTranslateNode() { }

  void Update()
  {
	 glPushMatrix();

  glTranslatef(x,y,z);

  CSceneNode::Update();
   glPopMatrix();
  }
protected:
	float x,y,z;
};
#endif
Advertisement
This depends on whether you are using your bounding boxes for testing for collisions, or as some sort of actual graphical representation of the bounding boxes. The display matrix will have no effect on an "imaginary" bounding box used to check collisions.

If I am understanding correctly, your coordinates for your objects are stored as relative to the coordinates of the parent. This means the actual world coordinates of the parent acts as the zero-point for the object. However, the problem arises in that the parent may be a child of something else, and so on, until you reach the root.

In order to find the world coordinates of an object, you have to trace it back from the root.

Once you find the world coordinates of the object, knowing the location of the bounding box is trivial.
Check out the first gameplay video from my javascript/PHP RTS game
Yep you got me correct. I guess I found a way but i need to modify my bounding boxes. I figured that I can get the current matrix as a float array [16] by glGetFloatv(GL_MODELVIEW_MATRIX,m). I ll represent my bb's with a center point and a vector ( learned this from gamasutra cant remember the link exactly now) and multiply them with the current matrix. But this will be some what expensive operation I guess, but since i don't have a lot of moving object I don't think this will be a problem for me. If it works I ll put up the code here and explain in case someone else needs something like this.

This topic is closed to new replies.

Advertisement