粒子系统:所有粒子向同一个方向运动
Particle system: All particles move in the same direction
我正在尝试创建一个粒子系统。在 ParticleSystem
构造函数中,我创建了具有随机颜色和速度的新粒子。
当我运行我的代码时,所有的粒子都有不同的颜色但是都在同一个方向移动
#include "stdafx.h"
#include "ParticleSystem.h"
ParticleSystem::ParticleSystem(){}
ParticleSystem::ParticleSystem(float size, int sides, sf::Vector2f velocity,
int pAtm)
: Particle(size, sides, velocity, pAtm)
{
for (int it = 0; it < pAtm; it++) {
particleVector.push_back(Particle(size, sides, velocity, pAtm));
}
}
ParticleSystem::~ParticleSystem()
{
}
const bool& ParticleSystem::getClick() const
{
return isClick;
}
void ParticleSystem::checkForClick()
{
if (sf::Mouse::isButtonPressed(sf::Mouse::Left)) {
isClick = true;
}
else {
isClick = false;
}
}
void ParticleSystem::update(const float& ft)
{
checkForClick();
move(dt);
//update1(dt);
}
void ParticleSystem::render(sf::RenderTarget& target)
{
for (std::vector<Particle>::iterator it = particleVector.begin(); it != particleVector.end(); ++it) {
target.draw(shape);
}
}
for (int it = 0; it < pAtm; it++) {
particleVector.push_back(Particle(size, sides, velocity, pAtm));
}
您为所有粒子赋予了相同的速度矢量。
如果你想要每个粒子的随机速度,你需要为每个粒子生成一个新的随机速度:
for (int it = 0; it < pAtm; it++) {
sf::Vector2f randomVelocity = generateRandomVelocity(); // Put the random vector generation logic in a function
particleVector.push_back(Particle(size, sides, randomVelocity, pAtm));
}
但是你的代码让我有点困惑,看起来你是在让 ParticleSystem 继承自 Particle。这实际上没有意义,因为 ParticleSystem 不是 Particle。 ParticleSystem 应该存储粒子而不是一个。我认为这种奇怪的继承让你感到困惑。
我正在尝试创建一个粒子系统。在 ParticleSystem
构造函数中,我创建了具有随机颜色和速度的新粒子。
当我运行我的代码时,所有的粒子都有不同的颜色但是都在同一个方向移动
#include "stdafx.h"
#include "ParticleSystem.h"
ParticleSystem::ParticleSystem(){}
ParticleSystem::ParticleSystem(float size, int sides, sf::Vector2f velocity,
int pAtm)
: Particle(size, sides, velocity, pAtm)
{
for (int it = 0; it < pAtm; it++) {
particleVector.push_back(Particle(size, sides, velocity, pAtm));
}
}
ParticleSystem::~ParticleSystem()
{
}
const bool& ParticleSystem::getClick() const
{
return isClick;
}
void ParticleSystem::checkForClick()
{
if (sf::Mouse::isButtonPressed(sf::Mouse::Left)) {
isClick = true;
}
else {
isClick = false;
}
}
void ParticleSystem::update(const float& ft)
{
checkForClick();
move(dt);
//update1(dt);
}
void ParticleSystem::render(sf::RenderTarget& target)
{
for (std::vector<Particle>::iterator it = particleVector.begin(); it != particleVector.end(); ++it) {
target.draw(shape);
}
}
for (int it = 0; it < pAtm; it++) {
particleVector.push_back(Particle(size, sides, velocity, pAtm));
}
您为所有粒子赋予了相同的速度矢量。 如果你想要每个粒子的随机速度,你需要为每个粒子生成一个新的随机速度:
for (int it = 0; it < pAtm; it++) {
sf::Vector2f randomVelocity = generateRandomVelocity(); // Put the random vector generation logic in a function
particleVector.push_back(Particle(size, sides, randomVelocity, pAtm));
}
但是你的代码让我有点困惑,看起来你是在让 ParticleSystem 继承自 Particle。这实际上没有意义,因为 ParticleSystem 不是 Particle。 ParticleSystem 应该存储粒子而不是一个。我认为这种奇怪的继承让你感到困惑。