c++ - 我的粒子数组不起作用(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
正确访问。
解决方案
当你这样做
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;
推荐阅读
- git - 在将裸仓库推送到具有维护者角色的 gitlab 时,预接收挂钩拒绝错误
- angular - 页面刷新后 Angular Azure AD 保持登录状态
- r - 将每列的唯一值传输到行中 - 每行最多 10 个值
- swiftui - 使用 WheelPickerStyle() 时 Catalyst 应用程序崩溃
- optimization - 如何在具有求和功能的 cplex 中包含带有决策变量的 if 语句
- java - Selenium-java:我想每次从网页写入数据到 excel 总是在新行中
- android - 为什么chrome不能在android上读取本地html文件?
- git - Laravel Forge 未能部署“致命:空身份名称(对于
) 不允许 - ruby - 我可以通过 ruby-git 获取上游远程分支吗
- sql - Postgresql - 将数据库中不同模式的表值合并到一个表中