Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/143.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 未保留类变量中的更改_C++_Class_Visual C++_Opengl - Fatal编程技术网

C++ 未保留类变量中的更改

C++ 未保留类变量中的更改,c++,class,visual-c++,opengl,C++,Class,Visual C++,Opengl,这一定是一个愚蠢的错误,但我已经研究这个bug好几天了,终于把它缩小到一个奇怪的问题。也许我只是看不见它,因为我的大部分经验是用java编程,而不是C++。在openGL中,我正在设置雪人在某些地形上移动的动画,并且每当我将雪人的速度设置为随机行走时,它都不会在下次调用更新函数时保留。我通过计算一个概率来实现这一点,如果概率检查成功,雪人将得到一个随机速度,并在每次更新时添加到位置。不知何故,每次更新函数退出时,速度都会被重置为0,导致它们在一帧内跳一点,然后在未来的帧中传送回它们以前的位置,最

这一定是一个愚蠢的错误,但我已经研究这个bug好几天了,终于把它缩小到一个奇怪的问题。也许我只是看不见它,因为我的大部分经验是用java编程,而不是C++。在openGL中,我正在设置雪人在某些地形上移动的动画,并且每当我将雪人的速度设置为随机行走时,它都不会在下次调用更新函数时保留。我通过计算一个概率来实现这一点,如果概率检查成功,雪人将得到一个随机速度,并在每次更新时添加到位置。不知何故,每次更新函数退出时,速度都会被重置为0,导致它们在一帧内跳一点,然后在未来的帧中传送回它们以前的位置,最终不会使它们移动。在调用过程中,x和y位置也会恢复为以前的值。命令提示符输出的示例如下:

DEBUG: new frame
2) dx dy 0.000000 0.000000
256.000000 15.000000 0.000000 0.000000
2) dx dy 0.000000 0.000000
104.000000 17.000000 0.000000 0.000000
2) dx dy 0.000000 0.000000
256.000000 256.000000 0.000000 0.000000
1) dx dy -0.100000 -0.400000
2) dx dy -0.100000 -0.400000
306.000000 498.000000 -0.100000 -0.400000
2)dx dy 0.000000 0.000000
6.000000 256.000000 0.000000 0.000000
DEBUG: new frame
2) dx dy 0.000000 0.000000
256.000000 15.000000 0.000000 0.000000
2) dx dy 0.000000 0.000000
104.000000 17.000000 0.000000 0.000000
2) dx dy 0.000000 0.000000
256.000000 256.000000 0.000000 0.000000
2) dx dy 0.000000 0.000000
256.000000 256.000000 0.000000 0.000000
2)dx dy 0.000000 0.000000
6.000000 256.000000 0.000000 0.000000
它们打印出一组值,但需要注意的是,第4个元素在调用之间应该有相同的值,而它们没有。它们被重置

雪人

#pragma once
#include "ppm_canvas.h"
class Snowman
{
public:
Snowman(canvas_t);
~Snowman(void);

void setWireframe(bool);
void toggleWireframe(void);
void setAnimate(bool);
void toggleAnimate(void);
void setArmSegments(int);
void addArmSegment(void);
void subtractArmSegment(void);

void update(canvas_t);
void draw(void);

private:
bool wireFrame;
bool animate;
bool walking;
int armSegments;
int animationFrameNumber;
float manualUserOffset;

float x, y, z;
float dx, dy;

inline float f(void);
inline void drawMouth(int headRadius);
inline void drawFace(int headRadius);
void drawArm(int remainingSegments);
inline void drawBody();
inline float getHeight(canvas_t);
};
来自Snowman.cpp

void Snowman::update(canvas_t texture){
    //randomly toggle the walking variable
    int probability = rand() % 100;
    if(probability <= 2){
        walking = !walking;
        this->dx = static_cast<float>(( (rand() % 100) - 50))/10.0;
        this->dy = static_cast<float>(( (rand() % 100) - 50))/10.0;
        printf("1) dx dy %f %f\n", dx, dy);
    }
    printf("2) dx dy %f %f\n", dx, dy);
    //code to control movement
    if(walking){
        this->animate = true;
        this->x += this->dx;
        this->y += this->dy;
        constrain(this->x, 0, texture.width * 2);
        constrain(this->y, 0, texture.height * 2);
    }else{
        animate = false;
    }

    //set the height after x and y are resolved
    this->z = getHeight(texture);
}    

void Snowman::draw(void){
glPushMatrix();{
    //turns out x is correct, y is vertical, and z is the other horozintal.  oops...
    float alittlebit = 10.0;//offset because the center of the bottom sphere is below 0
    glRotatef(atan2(dy, dx), 0, 1, 0);
    glTranslatef(x, z + alittlebit, y);
    drawBody();
}glPopMatrix();
printf(" %f %f %f %f\n", this->x, this->y, this->dx, this->dy );
}
我还包括了一个视频的错误在下面的行动。我已经独自花了8个多小时来解决这个问题,非常希望能得到任何帮助

此外,没有办法将问题浓缩为这一点,但以下是完整的源代码:

尝试在drawSnowmen()中使用矢量元素的引用。请参阅我上面关于复制构造函数的评论

    for(Snowman &i:snowmen){
        i.update(terrain);
        i.draw();
    }
如果没有ref,update()将修改Snowman的临时实例,而不是存储在vector中的实例:

    for(Snowman i:snowmen){
        i.update(terrain);
        i.draw();
    }

使用调试器,你要设置一个数据断点。我已经。它还没有阐明这个问题你能准备一份吗?在我们找到你的bug之前,你要求我们做很多你可以做得更容易的工作。它与地形代码紧密结合,甚至远不止这些。我所能做的就是把所有的源代码上传到网上,然后为雪人添加你自己的复制构造函数。或者使用指向雪人的指针向量。看看这会不会改变什么。您唯一定义的构造函数正在清除有问题的成员。我想知道drawSnowmen中的for循环是否提供了变量I中期望的Snowman的本地副本。考虑一个由REF变量和I。
    for(Snowman i:snowmen){
        i.update(terrain);
        i.draw();
    }