/* * Object.h * Jonathan Boldiga * 08/26/03 * * Description: Used to represent objects in the world. * */ #include "Node.h" #include "Vector3.h" #include "Camera.h" #ifndef __OBJECT_H #define __OBJECT_H class Object : public Node{ protected: bool isDead; //perform basic physics on the object virtual void onAnimate(float deltaTime){ position += velocity * deltaTime; velocity += acceleration * deltaTime; } //draw the object given the positon of the camera virtual void onDraw(Camera* camera){} //collide with other objects virtual void onCollision(Object* collisionObject){} //perform collision; prepare this object virtual void onPrepare(){ //perform collisons, start at the root of the world. processCollisions(findRoot()); } public: Vector3 position; //object position Vector3 velocity; //object velocity Vector3 acceleration; //object acceleration float size; //size of bounding sphere (radius) //constructor Object(){ isDead = false; } //destructor ~Object(){} virtual void load(){} virtual void unload(){} //draw object void draw(Camera* camera){ //push modelview matrix on stack glPushMatrix(); onDraw(camera); //draw this object if(hasChild()) //draw child ((Object*)childNode)->draw(camera); glPopMatrix(); //draw siblings if(hasParent() && !isLastChild()) ((Object*)nextNode)->draw(camera); } //animate object void animate(float deltaTime){ //animate this object onAnimate(deltaTime); //animate children of this object if(hasChild()) ((Object*)childNode)->animate(deltaTime); //animate any siblings of object if(hasParent() && !isLastChild()) ((Object*)nextNode)->animate(deltaTime); if(isDead) delete this; } //perform collision detection void processCollisions(Object* object){ //if this objects bounding sphere intersects this objects sphere, check to make //sure it is not *this* object if (((object->position - position).length() <= (object->size + size)) && (object != ((Object*)this))){ onCollision(object); //test for any child collisions w/ object if(hasChild()) ((Object*)childNode)->processCollisions(object); //test siblings if(hasParent() && !isLastChild()) ((Object*)nextNode)->processCollisions(object); } //if object has children, check collisions with these children if(object->hasChild()) processCollisions((Object*)(object->childNode)); //if object has siblings "" "" if(object->hasParent() && !object->isLastChild()) processCollisions((Object*)(object->nextNode)); } void prepare(){ onPrepare(); if(hasChild()) ((Object*)childNode)->prepare(); if(hasParent() && !isLastChild()) ((Object*)nextNode)->prepare(); } Object* findRoot(){ //if this object has a parent node, return the root of the parent if(parentNode){ return ((Object*)parentNode)->findRoot(); } return this; } }; #endif