Hi,
I'm writing a simple scene graph to hold some objects so I can control the rendering and drawing of these objects in OpenGL. I have two classes, one called GameObject which defines the objects with certain parameters such as position and velocity as well as how to draw the object. It has a method called update() which is used to calculate any change in position and then calls the draw() to draw itself onto the screen.
Another class is called sceneNode. This class controls the data structure which I use to store the objects. This an instance of this class contains a pointer to a GameObject instance to allow me to access the data and a sceneNode has children of type sceneNode stored in a vector.
Each class works perfectly well on their own. I can add children to the scene graph etc and I can create a GameObject and tell it to draw itself and everything works as expected.
I then added a new method to sceneNode which is called render(), this method gets the pointer to gameObject and calls it's update function before calling render() on any children of the node.
The problem is: the update() method is called and the GameObject is drawn but it is always drawn in the same place and doesn't move despite having a velocity and change in position. Calling the update() function by using an instance of GameObject DOES draw the object and move the object around the screen. I've been staring at this code for hours and can't see what I'm missing. I'm still fairly new to the idea of pointers and calling methods from another class so I may have missed something obvious.
Here are the relevant methods:
GameObject.cpp: The variables position, velocity etc are listed as protected.
void GameObject::update(){
for (int i = 0; i<3;i++){
this->velocity[i] +=this->acceleration[i];
}
this->position[0] += this->velocity[0];
this->position[1] += this->velocity[1];
this->position[2] += this->velocity[2];
this->draw();
}
void GameObject::draw() {
glTranslatef(this->position[0], this->position[1], this->position[2]);
glBegin(GL_TRIANGLES);
for (int i = 0; i < this->num_tris; i++) {
for (int j = 0; j <3 ; j++) {
glVertex3fv(vertices[triangles[i].INDEXES[j]]);
}
}
glEnd();
}
sceneNode.cpp:
void sceneNode::render(){
GameObject *go = new GameObject();
go = this->data;
//data is the name of the variable storing a pointer to a GameObject
go->update();
//update() is a method that redraws the object and calc's new position etc
if (hasChildren()) { //if this node has children, render all of them aswell...
for (int i = 0; i< this->node_tree.size(); i++) {
this->node_tree.at(i).render();
}
}
sceneNode.h: This is how the GameObject pointer is set up in the sceneNode
Protected:
GameObject* data;
I'm lost as to why the position is not changing for the render() method but is for the update() method as they are both calling the same thing?
Thanks for any help