c++ - 我的粒子数组不工作(C++/粒子系统)

标签 c++ arrays opengl particles particle-system

我正在尝试制作一个简单的粒子系统来为我在 opengl 中制作的轮胎和车轮制作烟雾效果。经过搜索,我发现粒子系统可以用于此。我从来没有做过粒子系统,所以我试着看看我是否可以在网上找到任何我可以为我的特定目的修改的东西,但没有运气,所以我在这里尝试自己制作。

我有一个框架,只允许我从我在屏幕上点击的地方制作一个粒子。制作完成后,粒子会从屏幕上掉下来。然后我能够修改代码,然后通过复制和粘贴我用来制作第一个单一代码的代码(但只是更改变量名称)来使 2 显示出来。由此我知道我可以使用 for 循环和数组制作任意数量的粒子(至少我认为我可以)。然而,它并没有真正做到我所希望的。在这样做之前,我只有两个粒子会在我点击屏幕上的任意位置后出现。现在,在实现我的粒子数组后,我删除了第二个粒子(如下面的代码所示)并将其替换为我的数组。 MAX_PARTICLES 仅设置为 5 只是为了测试它。然而,只有一个粒子显示并且它也没有移动......它卡在我点击粒子出现的地方。

Here is a picture of what it was before making an array for particles.

Here is a picture of what it is now after making an array for particles.

我不明白为什么阵列只有一个粒子显示,也不明白为什么它不动。我的假设是 g.particle 地址未在我的 movement() 或我的 render() 中访问。但我不知道如何解决它。下面是我的 movement()render() 代码。

 void movement()
 {
     if (g.n <= 0)
         return;
     Particle *p = &g.particle;
     p->s.center.x += p->velocity.x;
     p->s.center.y += p->velocity.y;

     //this commented code is me making another particle
     /*Particle *b = &g.particles[0];
     b->s.center.x += b->velocity.x;
     b->s.center.y += b->velocity.y;*/

     Particle b[MAX_PARTICLES];
     for(int i=0; i<MAX_PARTICLES; i++)
     {
         b[i] = g.particles[i];
         b[i].s.center.x += b[i].velocity.x;
         b[i].s.center.y += b[i].velocity.y;
     }

     //check for off-screen
     if (p->s.center.y < 0.0) {
         cout << "off screen" << endl;
         g.n = 0;
     }   
 }

 void render()
 {
     glClear(GL_COLOR_BUFFER_BIT);
     //Draw shapes...
     //draw the box
     Shape *s;
     glColor3ub(90,140,90);
     s = &g.box;
     glPushMatrix();
     glTranslatef(s->center.x, s->center.y, s->center.z);
     float w, h;
     w = s->width;
     h = s->height;
     glBegin(GL_QUADS);
     glVertex2i(-w, -h);
     glVertex2i(-w,  h);
     glVertex2i( w,  h);
     glVertex2i( w, -h);
     glEnd();
     glPopMatrix();
     //
     //Draw particles here
     if (g.n > 0) {
         //There is at least one particle to draw.
         glPushMatrix();
         glColor3ub(150,160,220);
         Vec *c = &g.particle.s.center;
         w = h = 2;
         glBegin(GL_QUADS);
         glVertex2i(c->x-w, c->y-h);
         glVertex2i(c->x-w, c->y+h);
         glVertex2i(c->x+w, c->y+h);
         glVertex2i(c->x+w, c->y-h);
         glEnd();
         glPopMatrix();

         /*below code is commented out because it was just me 
         trying to make another particle.*/

         //second particle
         /*glPushMatrix();
         glColor3ub(150,160,220);
         Vec *d = &g.particles[0].s.center;
         w = h = 2;
         glBegin(GL_QUADS);
         glVertex2i(d->x-w, d->y-h);
         glVertex2i(d->x-w, d->y+h);
         glVertex2i(d->x+w, d->y+h);
         glVertex2i(d->x+w, d->y-h);
         glEnd();
         glPopMatrix(); */


         //making a for loop to draw all the particles.
         for(int i=0; i<MAX_PARTICLES; i++)
         {
             glPushMatrix();
             glColor3ub(150,160,220);
             Vec *d = &g.particles[i].s.center;
             w = h = 2;
             glBegin(GL_QUADS);
             glVertex2i(d->x-w, d->y-h);
             glVertex2i(d->x-w, d->y+h);
             glVertex2i(d->x+w, d->y+h);
             glVertex2i(d->x+w, d->y-h);
             glEnd();
             glPopMatrix(); 
         }
     }

如图所示,我基本上采用了用于制作单个粒子的给定代码,并尝试制作一个可同时制作 5 个粒子的数组。以防万一我制作粒子的方法有问题,这里也有相关代码,供大家检查:

 void makeParticle(int x, int y)
 {   
     //Add a particle to the particle system.
     // 
     if (g.n >= MAX_PARTICLES)
         return;
     cout << "makeParticle() " << x << " " << y << endl;
     //set position of particle
     Particle *p = &g.particle;
     p->s.center.x = x;
     p->s.center.y = y;
     p->velocity.y = -4.0;
     p->velocity.x =  1.0;

     /*Particle *v = &g.particles[0];
     v->s.center.x = x;
     v->s.center.y = y;
     v->velocity.y = -5.0;
     v->velocity.x =  1.0; */

     float vy = -5.0;
     for(int i=0; i<MAX_PARTICLES; i++)
     {
         Particle *v = &g.particles[i];
         v->s.center.x = x;
         v->s.center.y = y;
         v->velocity.y = vy;
         v->velocity.x =  1.0;
         vy--;
     }
     ++g.n;
 }

请帮我弄清楚我做错了什么,导致我无法使用数组正确制作粒子。我有一种感觉,只是我没有正确访问 g.particles

最佳答案

当你做的时候

b[i] = g.particles[i];

复制对象到数组b。您对 b[i] 所做的所有更改都将仅针对该对象。

在我看来,实际上并不需要数组 b,您所需要的只是对对象 g.particles[i] 的引用:

Particle& b = g.particles[i];
b.s.center.x += b.velocity.x;
b.s.center.y += b.velocity.y;

关于c++ - 我的粒子数组不工作(C++/粒子系统),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59170411/

相关文章:

c++ - 如何从 boost 线程访问我的类实例?

arrays - 有什么方法可以让 bsearch 始终如一地进行相等匹配?

c++ - 设计问题 - 多重继承,C++

C++ 存储模板结构

java - 重复分配相同大小的字节数组,用池替换吗?

php - 合并数组 PHP

java - 合并网格 opengl 和 libgdx

delphi - 在 Delphi 中使用 OpenGL 渲染到纹理

python - OpenGL - 一些相交和一些非相交多边形的曲面分割

c++ - 当多维数组是结构的属性时,如何通过引用传递它?