OpenGL-在随机方向上移动的对象

OpenGL - Objects moving in random direction

本文关键字:移动 对象 方向 随机 OpenGL-      更新时间:2023-10-16

我正在使用OpenGL和C制作一个2D视频游戏,但我遇到了一些麻烦。我试图让一定数量的物体,比如说100,以随机的速度和方向漫游,但它不起作用,我相信有更好的方法可以做到这一点,但我想不通。

到目前为止,这是我的课,我想被画100次:

#include "stdafx.h"
#include <math.h>
#ifndef _THING_H
#define _THING_H
class thingClase {
public:
    float ypos, xpos, zpos;
    float count;
    thingClase() {
        xpos = rand()%80;
        ypos = rand()%80;
        zpos = 1;
        count = 1;
    }
    };
thingClase thing[100];
int thingNum = 0;
int thingTotal = 100;
int width = 1;
bool alive;
#endif  /* _THING_H */

我的主要功能包括:

void drawThing() {
for (thingNum = 0; thingNum < thingTotal; thingNum++) {
    glPushMatrix();
    glTranslated(thing[thingNum].xpos, thing[thingNum].ypos, thing[thingNum].zpos);
    glPopMatrix();
}
}

所以,我看到你正在设置一个转换来绘制一些东西:

void drawThing() {
// EDIT:
//
// Set the matrix mode!
glMatrixMode(GL_MODELVIEW);
for (thingNum = 0; thingNum < thingTotal; thingNum++) {
    // Yes, good, matrix is saved!
    glPushMatrix();
    // Yes, translate to the thing's position
    glTranslated(thing[thingNum].xpos, thing[thingNum].ypos, thing[thingNum].zpos);
    // Draw the thing here!
    //
    // You know, like call a display list or something.
    // Restore the matrix, also good!
    glPopMatrix();
}
}

你需要有一幅画。因此,如果您使用的是旧的、更易于使用的OpenGL,请制作一个显示列表或使用glBeginglEnd

然后,在您感到舒适之后,制作并使用VBO和着色器,就像我们今天OpenGL的方式一样。