我正在进行一项opengl任务,我必须让一个生物(我选择了一个雪人)在一些地形上移动。我试图让它四处走动,我得到了最奇怪的错误。打印出数字后,我经常将“-1。#QNAN0”作为数字。我甚至都不知道这意味着什么。下面是雪人的更新函数,构造函数和头文件。我正在尝试将2个数字用作速度并将其添加到位置,同时将其设置为动画(随机更改),但我不明白哪些错误导致我无法从rand()中获取数字。< / p>
每次概率检查成功时,都会输出:
DEBUG: probability check succeeded
-1.#QNAN0 0.000000
或
DEBUG: probability check succeeded
0.000000 0.000000
每个人有大约50%的可能性。
来自Snowman.cpp
void Snowman::update(canvas_t texture){
//randomly toggle the walking variable
int probability = rand() % 100;
//printf("DEBUG: probability = %d\n", probability);
if(probability <= 10){
printf("DEBUG: probability check succeeded\n");
walking = !walking;
dx = static_cast<float>(( (rand() % 10) - 5));
dy = static_cast<float>(( (rand() % 10) - 5));
printf("%f %f\n", dx, dy);
}
//code to control movement
if(walking){
animate = true;
x += dx;
y += dy;
constrain(x, 0, texture.width);
constrain(y, 0, texture.height);
}else{
animate = false;
}
//set the height after x and y are resolved
z = getHeight(texture);
}
Snowman::Snowman(canvas_t terrain)
{
wireFrame = false;
animate = false;
armSegments = 2;
animationFrameNumber = 0;
manualUserOffset = 0;
//set its initial position
x = rand() % terrain.width;
y = rand() % terrain.height;
dx = 0;
dy = 0;
}
来自Snowman.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;
int 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);
};
答案 0 :(得分:1)
dx
和dy
为int
,但您的格式说明符%f
需要double
或float
。所以你有未定义的行为。