从另一个类调用类中的方法时出现问题

发布于 2024-08-22 03:14:35 字数 1993 浏览 4 评论 0原文

我正在编写一个简单的场景图来保存一些对象,以便我可以控制这些对象在 OpenGL 中的渲染和绘制。我有两个类,一个称为 GameObject,它定义具有某些参数的对象,例如位置和速度以及如何绘制对象。它有一个名为 update() 的方法,用于计算位置的任何变化,然后调用 draw() 将其自身绘制到屏幕上。

另一个类称为 sceneNode。这个类控制我用来存储对象的数据结构。这个类的实例包含一个指向 GameObject 实例的指针,以允许我访问数据,并且 sceneNode 具有存储在向量中的 sceneNode 类型的子级。

每个类都可以很好地独立运行。我可以将子项添加到场景图等中,我可以创建一个游戏对象并告诉它自行绘制,一切都按预期进行。

然后,我向 sceneNode 添加了一个名为 render() 的新方法,该方法获取指向 gameObject 的指针,并在对节点的任何子节点调用 render() 之前调用它的更新函数。

问题是:调用 update() 方法并绘制 GameObject,但它始终绘制在同一位置,并且尽管速度和位置发生变化,但它不会移动。使用 GameObject 实例调用 update() 函数确实会绘制对象并在屏幕上移动对象。我已经盯着这个代码几个小时了,看不出我错过了什么。我对指针和从另一个类调用方法的想法仍然相当陌生,所以我可能错过了一些明显的东西。

以下是相关方法:

GameObject.cpp: 变量位置、速度等被列为受保护的。

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: 这就是 GameObject 指针在 sceneNode 中的设置方式,

Protected:
GameObject* data;

我不明白为什么 render() 方法的位置没有改变,但 update() 方法的位置却改变了,因为它们都调用相同的东西?

感谢您的帮助

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

涫野音 2024-08-29 03:14:35

这里有一些大问题。

GameObject *go = new GameObject();
go = this->data;

您正在创建一个新对象,然后您忘记了指向该对象的指针。每次调用渲染时都会发生这种情况,因此您有大量未使用的内存随着时间的推移而增长。您可以直接说:

GameObject *go = NULL;

创建一个指针而不为其分配数据。

您也可以在任何地方省略 this->foo。它隐含在所有类中。

因此,虽然我看不到这段代码中的错误,但它很可能在其他地方,因为您在使用上面的指针时犯了错误。您可能有指向您没有绘制的东西的指针。如果你看看正在更新的东西的内存地址和在render()中正在更新的东西的内存地址,它们实际上可能是不同的。如果是,那么您就知道您没有对正在绘制的相同内容调用更新。要获取内存地址,请查看函数调用中“this”的值。把它们写下来并确保它们与您正在绘制的对象相同。错误不在这段代码中。

There are some big problems here.

GameObject *go = new GameObject();
go = this->data;

You're creating a new object and then you are forgetting the pointer to that object. This happens every time you call render, so you have tons of memory that you aren't using that grows with time. You can just say:

GameObject *go = NULL;

to create a pointer without allocating data to it.

You can also omit the this->foo everywhere. It's implied within all classes.

So although I can't see the error in this code, chances are it is elsewhere, since you're making mistakes with pointers like above. You probably have pointers pointing to things you're not drawing. If you look at the memory addresses of the things that are updating and the memory addresses of the things being updated in render(), they may actually be different. If they are then you know that you're not calling update on the same things you are drawing. To get a memory address look at the value of 'this' within a function call. Write them down and make sure they are the same as the objects you are drawing. The error is not in this code.

呢古 2024-08-29 03:14:35

在SceneNode中,当你分配this->数据要走,您实际上将 go 设置为 null!取出该分配或执行此操作 -> data = new GameObject() 而不是使用 go.

In SceneNode, when you assign this-> data to go, you are effectively setting go to null! take out that assignment or do this->data = new GameObject() instead of using go.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文