首页 > 解决方案 > 我的粒子数组不起作用(C++/粒子系统)

问题描述

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

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

这是在为粒子制作阵列之前的照片。

这是在为粒子制作阵列后现在的样子。

我不明白为什么阵列中只有一个粒子显示以及为什么它不移动。我的假设是在 my或 myg.particle中没有访问地址。但我不知道如何解决它。下面是我的代码and 。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正确访问。

标签: c++arraysopenglparticlesparticle-system

解决方案


当你这样做

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;

推荐阅读