Emitter& Emitter::Update(float elapsed){ if(_active){ if(_particles.size() == 0 || (_continuous && _elapsedLastBirth >= .2)){ _elapsedLastBirth = 0; for(int i = 0; i < 50; i++){ Particle p; p.SetPosition(0, 0); float angle = RADIANS * (rand() % (_maxAngle - _minAngle) + _minAngle); int velocity = rand() % _radius; p.SetColor(_color); p.SetPositionDelta(sf::Vector2f(velocity * cos(angle), velocity * sin(angle))); p.SetFinalColor(sf::Color(255, 255, 255, 0)); p.SetLifetime(_lifetime); p.SetCenter(0, 0); _particles.push_back(p); } } vector<Particle>::iterator it; for(it = _particles.begin(); it < _particles.end(); it++){ if(it->GetLifetime() <= it->GetAge()){ _particles.erase(it); } else{ it->Update(elapsed); } } if(_particles.size() == 0) this->_active = false; _elapsedLastBirth += elapsed; } return *this; }
void Emitter::Update(double elapsed) { Array <int> particlesToRemove; //eliminacion de particulas for( unsigned int i = 0; i < particles.Size(); i++ ) { particles[i]->Update( elapsed ); for( unsigned int a = 0; a < affectors.Size(); a++ ) affectors[a].AddParticles( particles[i] ); if( particles[i]->GetLifetime() <= 0 ) { particlesToRemove.Add( i ); delete particles[i]; for( unsigned int a = 0; a < affectors.Size(); a++ ) affectors[a].DeleteParticles( particles[i] ); } } for( int i = particlesToRemove.Size() - 1; i >= 0 ; i-- ) { particles.RemoveAt( particlesToRemove[i] ); } //fin eliminacion particulas //creacion de particulas int32 nParticles = 0; double velX, velY, velAng, life; uint8 r, g, b; if( IsEmitting() ) { nParticles = static_cast<int32>( RangeRand( static_cast<int32>( minrate ), static_cast<int32>( maxrate ) ) * elapsed ); } for( uint32 i = 0; i < (uint32)nParticles; i++ ) { velX = RangeRand( minvelx, maxvelx ); velY = RangeRand( minvely, maxvely ); velAng = RangeRand( minangvel, maxangvel ); life = RangeRand( minlifetime, maxlifetime) ; r = (uint8)RangeRand( minr, maxr ); g = (uint8)RangeRand( ming, maxg ); b = (uint8)RangeRand( minb, maxb ); Particle *particle = new Particle( image, velX, velY, velAng, life, autofade ); particle->SetPosition( x, y ); particle->SetColor( r, g, b, 255 ); particle->SetBlendMode( blendMode ); particles.Add( particle ); } //fin creacion particulas }
void FluidSim::InitialiseParticles(void) { KernelPow9 = glm::pow(SmoothingKernelSize, 9.0f); KernelPow6 = glm::pow(SmoothingKernelSize, 6.0f); KernelPow2 = glm::pow(SmoothingKernelSize, 2.0f); float pi = glm::pi<float>(); DefaultKernelConstant = 315.0f / (64.0f * pi * KernelPow9); NormalKernelConstant = -945.0f / (32.0f * pi * KernelPow9); SurfaceKernelConstant = NormalKernelConstant; PressureKernelConstant = -45.0f / (pi * KernelPow6); ViscosityKernelConstant = 45.0f / (pi * KernelPow6); particles.reserve(ParticleCount); for(int x = 0; x < ParticlesPerDimension; x++) for(int y = 0; y < ParticlesPerDimension; y++) for(int z = 0; z < ParticlesPerDimension; z++) { Particle* particle = new Particle(ParticleMass); particle->SetPosition(GetRandomValue(-HorizontalBounds, HorizontalBounds), GetRandomValue(0.0f, HorizontalBounds), GetRandomValue(-HorizontalBounds, HorizontalBounds)); //particle->SetPosition(0.0f, 0.0f, 0.0f); particle->SetVelocity(0.0f, 0.0f, 0.0f); particles.push_back(particle); } container = Capsule(); container.radius = HorizontalBounds; container.length = HorizontalBounds; container.centre = glm::vec3(); innerContainer = Capsule(); innerRadius = HorizontalBounds / 2.0f; innerLength = HorizontalBounds / 2.0f; innerContainer.radius = innerRadius; innerContainer.length = innerLength; innerContainer.centre = glm::vec3(); container.CalculateCapsulePosition(glm::vec3()); innerContainer.CalculateCapsulePosition(glm::vec3()); crashCapsule = brokenCapsule = showNeighbours = showSurfaceParticles = hideSurfaceParticles = false; particleHighlightID = 0; wallEnabled = true; }
void Bullet::Update(DWORD) { Particle* pObj = new Particle; pObj->SetPosition(center); pObj->SetRadius(radius*2/3); pObj->SetAngle(rand()%360); ParticleDepot.push(pObj); // TODO speed_y += 1; center.x = center.x + init_speed_x; center.y = center.y + speed_y; }
void Emitter::Update(double elapsed) { if (m_emitting) { double randVelX; double randVelY; double randAngVel; double randLifetime; double randRate = genRandomF(m_minrate, m_maxrate) * elapsed; //spawn uint8 randR, randG, randB; Particle p; for (unsigned short int i = 0; i < randRate; i++) { randVelX = genRandomF(m_minvelx, m_maxvelx); randVelY = genRandomF(m_minvely, m_maxvely); randAngVel = genRandomF(m_minangvel, m_maxangvel); randLifetime = genRandomF(m_minlifetime, m_maxlifetime); randR = genRandomF(m_minr, m_maxr); randG = genRandomF(m_ming, m_maxg); randB = genRandomF(m_minb, m_maxb); p = Particle(m_image, randVelX, randVelY, randAngVel, randLifetime, m_autofade); p.SetBlendMode(Renderer::BlendMode::ADDITIVE); p.SetColor(randR, randG, randB); p.SetPosition(m_x, m_y); m_particles.Add(p); } } //update for (unsigned short int i = 0; i < m_particles.Size(); i++) { if (!m_particles[i].Affected()) { for (unsigned short int j = 0; j < m_affectors.Size(); j++) { if (m_particles[i].GetX() >= m_affectors[j].GetX0() && m_particles[i].GetX() <= m_affectors[j].GetX1() && m_particles[i].GetY() >= m_affectors[j].GetY0() && m_particles[i].GetY() <= m_affectors[j].GetY1()) { m_affectors[j].ChangeParticleProperties(m_particles[i]); } } } if (m_particles[i].GetAlpha() == 0 || m_particles[i].GetLifeTime() <= 0) { m_particles.RemoveAt(i); } else m_particles[i].Update(elapsed); } }
void Emitter::Update(double elapsed) { Array<uint32> toDelete; if (emitting) { uint8 r = 0, g = 0, b = 0; double velx = 0, vely = 0; double angvel = 0; double lifetime = 0; // Incrementamos la lista de particulas. uint32 rate = (uint32)(Random(minrate, maxrate) * elapsed); for (uint32 i = 0; i < rate; i++) { r = (uint8)Random(minr, maxr); g = (uint8)Random(ming, maxg); b = (uint8)Random(minb, maxb); velx = Random(minvelx, maxvelx); vely = Random(minvely, maxvely); angvel = Random(minangvel, maxangvel); lifetime = Random(minlifetime, maxlifetime); Particle* p = new Particle(image, velx, vely, angvel, lifetime, autofade); p->SetBlendMode(blendMode); p->SetColor(r, g, b); p->SetPosition(x, y); particles.Add(p); } } // Actualizamos la lista de particulas y la lista de afectores. for (uint32 i = 0; i < particles.Size(); i++) { particles[i]->Update(elapsed); if (particles[i]->GetLifetime() <= 0) toDelete.Add(i); for (uint32 j = 0; j < affectors.Size(); j++) { if ( !affectors[j]->IsAffecting(particles[i]) && affectors[j]->InRange(particles[i]->GetX(), particles[i]->GetY()) ) { if (affectors[j]->GetMode() & AffectorMode::ANG_VEL) particles[i]->SetAngVelocity(affectors[j]->GetAngularVelocity()); if (affectors[j]->GetMode() & AffectorMode::VELOCITY) particles[i]->SetVelocity(affectors[j]->GetVelocityX(), affectors[j]->GetVelocityY()); if (affectors[j]->GetMode() & AffectorMode::COLOR) particles[i]->SetColor( affectors[j]->GetRed(), affectors[j]->GetGreen(), affectors[j]->GetBlue(), particles[i]->GetAlpha()); affectors[j]->AddAffected(particles[i]); } } } // Eliminamos las particulas cuyo lifetime haya expirado. for (uint32 i = toDelete.Size(); 0 < i; i--) { for (uint32 j = 0; j < affectors.Size(); j++) if ( affectors[j]->IsAffecting(particles[toDelete[i-1]]) ) affectors[j]->RemoveAffected(particles[toDelete[i-1]]); particles.RemoveAt(toDelete[i-1]); } }
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { XEngine *engine = XEngine::GetInstance(); if( engine->Init(hInstance) == FALSE ) { return -1; } Rect rec(Position(-10000.0, 10000.0), Position(10000.0, -10000.0)); TestScene *scene = new TestScene; scene->SetTime(0); //Scene *scene = new Scene; scene->Init(rec); scene->camera->MoveForwardBy(-40); scene->camera->MoveUpBy(18); scene->camera->Pitch(-XM_PI / 10); Material material; material.ambient = XMFLOAT4(1.0f, 1.0f, 1.0f, 1.0f); material.diffuse = XMFLOAT4(1.0f, 1.0f, 1.0f, 1.0f); material.specular = XMFLOAT4(1.0f, 1.0f, 1.0f, 1.0f); material.power = 9.0f; //material.texture = SRVBatch::GetInstance()->LoadSRV("media/", "cup.jpg"); Particle *fire = new Particle; fire->Init("media/","fire.png"); fire->SetPosition(0, 0, 10); //scene->AddRenderableThing(*fire); SkyBox *sky = new SkyBox; //sky->Init("textures/", "skymap.dds"); sky->Init("textures/", "Above_The_Sea.dds"); //sky->Init("textures/", "desert_skymap.dds"); scene->AddSky(sky); Terrain *terrain = new Terrain; //terrain->Init("terrain/testHight.bmp", "terrain/", "dirt01.dds"); terrain->Init("terrain/heightmap01.bmp", "terrain/", "grass.jpg"); scene->AddTerrain(terrain); Wall *wall = new Wall; wall->Init(30, 30, 0.3); wall->Pitch(XM_PI / 2); wall->SetPosition(0, -3, 0); //scene->AddRenderableThing(*wall); //¿É¼ûÐÔ²âÊÔ Wall *wall0 = new Wall; wall0->Init(30, 30, 0.3); wall0->Pitch(XM_PI / 2); wall0->SetPosition(1000, -3, 0); //scene->AddRenderableThing(*wall0); Obj *obj = new Obj; obj->CreateBox(1, 3, 2, material); obj->SetPosition(5, 1, 12); obj->Yaw(XM_PI / 3); obj->Roll(XM_PI / 3); obj->SetScale(1.3); //scene->AddRenderableThing(*obj); Cube *cube = new Cube; cube->Init(2); cube->Pitch(XM_PI / 3); cube->Roll(XM_PI / 3); cube->SetMaterial(material); //scene->AddRenderableThing(*cube); Ball *ball = new Ball; ball->Init(1); ball->SetPosition(0, 0, 6); Obj *objInWall = new Obj; objInWall->CreateBox(1, 1, 1, material); objInWall->SetPosition(0, 1, 0); ball->AddChild(objInWall); //scene->AddRenderableThing(*ball); Ship *ship = new Ship; //ship->Init(); //ship->SetPosition(-10, 0, 0); //scene->AddRenderableThing(*ship); Model *test = new Model; test->LoadModel("media/", "chair.obj"); test->SetScale(1.7); test->SetPosition(-15, 0, 0); test->Pitch(-1.2); test->Yaw(-1.5); //scene->AddRenderableThing(*test); //SmileBoy *smileBoy= new SmileBoy; //smileBoy->Init(); //smileBoy->SetHandleInput(FALSE); //scene->AddRenderableThing(*smileBoy); Female *female = new Female; female->Init(); //female->SetPosition(-7, 0,0); female->SetHandleInput(TRUE); scene->AddRenderableThing(*female); RenderSystem::GetInstance()->SetShadowOpen(true); engine->LoadScene(scene); { Light *light = new Light; light->type = 0; light->position = XMFLOAT4(0.0f, 5.0f, -5.0f, 1.0f); XMStoreFloat4(&light->direction, XMVector4Normalize(XMVectorSet(1.0f, -1.0f, 0.0f, 0.0f))); light->ambient = XMFLOAT4(0.4f, 0.2f, 0.3f, 1.0f); light->diffuse = XMFLOAT4(0.3f, 0.4f, 0.5f, 1.0f); light->specular = XMFLOAT4(0.5f, 0.3f, 0.2f, 1.0f); light->attenuation0 = 0.0f; light->attenuation1 = 0.0f; light->attenuation2 = 0.0f; light->alpha = 0.0f; light->beta = 0.0f; light->falloff = 0.0f; scene->AddLight(light); } //******************************************* engine->Run(); return 0; }
void ParticleEngine::GenerateParticles(int type, float x, float y, int amount) { for (int i=0; i<amount; i++) { Particle *temp = mParticles[mIndex]; // if (mCount < MAX_PARTICLES) if (temp != NULL && !temp->mActive) { mTimer = 0.0f; temp->mActive = true; mIndex ++; if (mIndex>mMaxParticles-1) { mIndex = 0; } temp->SetPosition(x, y); temp->SetQuad(mQuad); temp->mSpeed = abs(rand()%50)/1000.0f; temp->mAngle = abs(rand()%360)*DEG2RAD; //mQuad->mBlend = GU_TFX_MODULATE; if (type == BULLETIMPACT) { //temp->ResetVelocity(); temp->mCircularAcceleration = -0.004f; float lifetime = 250.0f; temp->mLifetime = lifetime; temp->mScale = 0.3; temp->mScaleEnd = 0.1f; temp->mScaleDelta = -0.001f;//(0.1f-temp->mScale)/lifetime; temp->mRotation = 0.0f; temp->mRotationDelta = 0.002f; temp->mAlpha = 255.0f; temp->mAlphaEnd = 0.0f; temp->mAlphaDelta = -0.2f;//.0f/lifetime); //float r = 0.0f;//abs(RAND%256); //float g = 0.0f;//213.1f;//abs(RAND%256); //float b = 0.0f;//55.0f;//abs(RAND%256); //temp->mColor.a = 255.0f; temp->mColor[0] = 0.0f; temp->mColor[1] = 0.0f; temp->mColor[2] = 0.0f; //r = 255.0f; //g = 128.0f; //b = 35.0f; //temp->mColorEnd.a= 255.0f; temp->mColorEnd[0] = 255.0f; temp->mColorEnd[1] = 128.0f;; temp->mColorEnd[2] = 35.0f; } else if (type == BLOOD) { //temp->ResetVelocity(); temp->mCircularAcceleration = -0.004f; float lifetime = 200.0f; temp->mLifetime = lifetime; temp->mScale = 0.5f; temp->mScaleEnd = 0.3f; temp->mScaleDelta = -0.001f;//(0.1f-temp->mScale)/lifetime; temp->mRotation = 0.0f; temp->mRotationDelta = 0.002f; temp->mAlpha = 255.0f; temp->mAlphaEnd = 0.0f; temp->mAlphaDelta = -0.3f;//.0f/lifetime); //float r = 0.0f;//abs(RAND%256); //float g = 0.0f;//213.1f;//abs(RAND%256); //float b = 0.0f;//55.0f;//abs(RAND%256); //temp->mColor.a = 255.0f; temp->mColor[0] = 128.0f; temp->mColor[1] = 0.0f; temp->mColor[2] = 0.0f; //r = 255.0f; //g = 128.0f; //b = 35.0f; //temp->mColorEnd.a= 255.0f; temp->mColorEnd[0] = 255.0f; temp->mColorEnd[1] = 128.0f;; temp->mColorEnd[2] = 35.0f; } else if (type == BULLETSHELL) { //temp->ResetVelocity(); temp->mCircularAcceleration = -0.004f; float lifetime = 500.0f; temp->mLifetime = lifetime; temp->mScale = 0.1f; temp->mScaleEnd = 0.1f; temp->mScaleDelta = 0;//(0.1f-temp->mScale)/lifetime; temp->mRotation = 0.0f; temp->mRotationDelta = 0.002f; temp->mAlpha = 255.0f; temp->mAlphaEnd = 0.0f; temp->mAlphaDelta = -0.01f;//.0f/lifetime); //float r = 0.0f;//abs(RAND%256); //float g = 0.0f;//213.1f;//abs(RAND%256); //float b = 0.0f;//55.0f;//abs(RAND%256); //temp->mColor.a = 255.0f; temp->mColor[0] = 255.0f; temp->mColor[1] = 255.0f; temp->mColor[2] = 0.0f; //r = 255.0f; //g = 128.0f; //b = 35.0f; //temp->mColorEnd.a= 255.0f; temp->mColorEnd[0] = 255.0f; temp->mColorEnd[1] = 255.0f;; temp->mColorEnd[2] = 0.0f; } for (int i=0; i<3; i++) { temp->mColorDelta[i] = (temp->mColorEnd[i]-temp->mColor[i])/3000.0f; } } } }
int main() { srand(time(NULL)); sf::RenderWindow window(sf::VideoMode(800, 600), "Collisions"); CollidersContainer Container; Particle A; A.SetMass(10); A.SetRadius(5); A.SetPosition(10, 10); A.SetActingForce(0, 0); sf::Clock clock; clock.restart(); //Creating Particles //N2 - 156 | O2 - 42 | Ar - 2 | All - 200 //Nitrogen Particles //Mass - 4.652 × 10^-26 kg //Radius - 112 * 10^-12 m A.SetMass(4.652 * pow(10, -26)); A.SetRadius(5.6); for (int i = 0; i < 156; i++) { //Setting random values to Velocity and Position double RandomVelocityX = rand() % 1000 + 1000; double RandomVelocityY = rand() % 1000 + 1000; if (rand() % 2 == 0){ RandomVelocityX *= -1; } if (rand() % 2 == 0){ RandomVelocityY *= -1; } A.SetVelocity(RandomVelocityX, RandomVelocityY); A.SetPosition((rand()) % 800 + 1, (rand()) % 600 + 1); //Creating Particle from template A Container.CreateParticle(A); } //Oxygen Particles //Mass - 5.314 × 10^-26 kg //Radius - 96 * 10^-12 m A.SetMass(5.314 * pow(10, -26)); A.SetRadius(4.8); for (int i = 0; i < 42; i++) { //Setting random values to Velocity and Position double RandomVelocityX = rand() % 1000 + 1000; double RandomVelocityY = rand() % 1000 + 1000; if (rand() % 2 == 0){ RandomVelocityX *= -1; } if (rand() % 2 == 0){ RandomVelocityY *= -1; } A.SetVelocity(RandomVelocityX, RandomVelocityY); A.SetPosition((rand()) % 800 + 1, (rand()) % 600 + 1); //Creating Particle from template A Container.CreateParticle(A); } //Argon Particles //Mass - 6.642 × 10^-26 //Radius - 71 * 10^-12 m A.SetMass(6.642 * pow(10, -26)); A.SetRadius(3.55); for (int i = 0; i < 2; i++) { //Setting random values to Velocity and Position double RandomVelocityX = rand() % 1000 + 1000; double RandomVelocityY = rand() % 1000 + 1000; if (rand() % 2 == 0){ RandomVelocityX *= -1; } if (rand() % 2 == 0){ RandomVelocityY *= -1; } A.SetVelocity(RandomVelocityX, RandomVelocityY); A.SetPosition((rand()) % 800 + 1, (rand()) % 600 + 1); //Creating Particle from template A Container.CreateParticle(A); } while (window.isOpen()) { sf::Time T = clock.getElapsedTime(); sf::Event event; while (window.pollEvent(event)) { if (event.type == sf::Event::Closed) window.close(); } window.clear(); Container.MoveParticles(0.0015); Container.SimulateCollisions(); Container.DeflectParticlesOutOfBorder(); for (unsigned int i = 0; i < Container.ParticlesQuantity(); i++) { sf::CircleShape shape(Container.GetParticleRadius(i)); shape.setPosition(Container.GetParticlePositionX(i), Container.GetParticlePositionY(i)); switch ((int)Container.GetParticleRadius(i)) { case 5: shape.setFillColor(sf::Color::Blue); break; case 4: shape.setFillColor(sf::Color::White); break; default: shape.setFillColor(sf::Color::Magenta); break; } window.draw(shape); } while (T.asSeconds() < 0.045){ T = clock.getElapsedTime(); } window.display(); clock.restart(); } return 0; }