C++/过剩 : Generating multiple asteroids/bullets

标签 c++ object glut

我在为我的 GLUT 小行星游戏显示和动画多个小行星时遇到问题。我可以生成 1 ok,但是当我尝试添加更多时,输出不正确,createasteroid 不断被调用,并且出于某种原因只是在屏幕的不同部分闪烁小行星几毫秒。我相信这可能是因为当显示功能结束时对象被销毁但我无法找到解决方法到目前为止这是我的代码:

小行星.h

class asteroid
{
public:
asteroid(void); //constructer
~asteroid(void); //deconstructer

void createAsteroid();
float generateAsteroidLocation(float a, float b);
void animateAsteroid();
};

小行星.cpp

#include "asteroid.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#define _USE_MATH_DEFINES
#include <math.h>
#include <ctime>
#include <GL/glut.h>

float asteroidX,asteroidY, V;
int numOfAsteroids=0;

asteroid::asteroid(void){

}

asteroid::~asteroid(void){

}

void asteroid::createAsteroid(){
// Translate to an area outside of the screen and then give a random velocity in     the direction of the player
asteroidX = generateAsteroidLocation(0, 30);
asteroidY = generateAsteroidLocation(0, 30);
V = generateAsteroidLocation(0.000030, 0.000050);

printf("Asteroid Y Location %f \n", asteroidY);
printf("Asteroid X Location %f \n", asteroidX);
printf("Asteroid Velocity %f \n", V);
printf("Asteroid Created! \n");

glPushMatrix();
glTranslatef(asteroidX, asteroidY, 0.0);
glColor3f(1.0, 1.0, 1.0);
glBegin(GL_LINE_STRIP);
glVertex2f(1.0, 1.0);
glVertex2f(1.8, 0.0);
glVertex2f(1.4, -0.8);
glVertex2f(1.0, -1.7);
glVertex2f(-1.5, -1.0);
glVertex2f(-1.0, 0.0);
glVertex2f(0.0, 1.4);
glVertex2f(1.0, 1.0);
glEnd();
glPopMatrix();

}

float asteroid::generateAsteroidLocation(float a, float b){
float random = ((float) rand()) / (float) RAND_MAX;
float range = b - a; 
return (random*range) + a;
}

void asteroid::animateAsteroid(){
float dt = 3500;
float Dx = 25 - asteroidX;
float Dy = 25 - asteroidY;
float Cx = asteroidX + Dx / sqrt(Dx*Dx+Dy*Dy) * V * dt;
float Cy = asteroidY + Dy / sqrt(Dx*Dx+Dy*Dy) * V * dt;
asteroidX = Cx;
asteroidY = Cy;
}

main.cpp(导致问题的函数)

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#define _USE_MATH_DEFINES
#include <math.h>
#include <ctime>
#include "asteroid.h"
#include <GL/glut.h>

asteroid a1;
asteroid a2;

void display(void)
{
glClear(GL_COLOR_BUFFER_BIT);
// check to see if the player is ready, if not, show the splashscreen
if(ready == false){
    splashScreen();
    showText();
}
else{

    createSpaceship();
    // PROBLEM HERE
    a1.createAsteroid();
    a2.createAsteroid();

}
// Setup the scene
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, 50, 0, 50, 0, 50);
// call the needed functions
glutSwapBuffers();

}

void idle(void)
{
glutPostWindowRedisplay(glutGetWindow());
// PROBLEM HERE
a1.animateAsteroid();
a2.animateAsteroid();

}

在这方面的任何帮助都会很棒。我对采取什么方法感到困惑。如果需要,我可以提供更多信息或 pastebin。 谢谢! -丹

最佳答案

您只想将 createAsteroid 函数分成两部分,一个创建 Asteroid,设置其初始值,另一个执行渲染。

你似乎有一个 ready 函数来知道加载何时完成,你应该在附近创建和初始化你的小行星。在显示中你应该调用一个只做这部分的函数:

glPushMatrix();
glTranslatef(asteroidX, asteroidY, 0.0);
glColor3f(1.0, 1.0, 1.0);
glBegin(GL_LINE_STRIP);
glVertex2f(1.0, 1.0);
glVertex2f(1.8, 0.0);
glVertex2f(1.4, -0.8);
glVertex2f(1.0, -1.7);
glVertex2f(-1.5, -1.0);
glVertex2f(-1.0, 0.0);
glVertex2f(0.0, 1.4);
glVertex2f(1.0, 1.0);
glEnd();
glPopMatrix();

此外,您似乎也在每次显示 时重新创建玩家的宇宙飞船,它应该像小行星一样只在初始化阶段创建!

编辑:如果我不清楚,在创建游戏对象时,您通常至少需要 4 个主要方法:Create/InitUpdate(在这里您可以为您的对象设置动画,编写游戏代码,例如测试子弹命中等...)、渲染清理/销毁。坚持这一点应该真的可以帮助您继续游戏!

关于C++/过剩 : Generating multiple asteroids/bullets,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13554679/

相关文章:

c++ - GLSL 着色器中的链接器错误?

c++ - 涉及指针的函数模板重载解析

JavaScript:对类/函数和对象使用相同的名称?

c++ - 在命令行中将输入显示到数组中时,如何删除./a.out?

c++ - 段错误而不是构造函数调用

objective-c - cocoa 和OpenGL : failed to draw cube

c++ - 如何使用 Glut 让相机飞过 OpenGL 中的对象?

python - 如何在 QGLWidget 中使用 glutInitDisplayMode() 来获得抗锯齿功能?

java - 如何使用我创建的对象测试 java 中的方法? (欢迎提供有用的标题建议。)

c++ - 从 C++ 中的类成员函数返回对象