void ofApp::update() { float currentTime = ofGetElapsedTimef(); float elapsedTime = ofClamp(currentTime - time, 0, 0.1); time = currentTime / snelheid; int i = 0; while (i < particles.size()){ if(!particles[i].isAlive){ particles.erase(particles.begin() + i ); }else{ i++; } } if(particles.size() < 4000){ Particle newParticle(¶meters); newParticle.setup(); particles.push_back(newParticle); } for (int i=0; i<particles.size(); i++){ particles[i].update(elapsedTime); } }
void idle(void) //infinitely creates new particles { if(pausecounter % 2 == 1) //turning newParticle on and off newParticle(); glutPostRedisplay(); }
// create num new particeles and initialize the particles void GvParticle::createParticles(int num) { GParticle *p=NULL; if (num<=0) return; DWORD t=timeGetTime(); // create list of num new particles for( ; num>0;num--) { GParticle *pnew=newParticle(); if (pnew == NULL) break; pnew->next=p; pnew->initTime=t; p=pnew; } // initialize values initialize(p); // append new particle list append(p); }
ParticleEmitter::ParticleEmitter(uint16_t n) { numPixels = n; numParticles = MAX_PARTICLES; maxVelocity = float(numParticles / 2000.0); stripPosition = random(100) / 100.0; // 0.0 - 1.0 for (int i=0; i < MAX_PARTICLES; i++) { particles[i] = newParticle(); } }
void ParticleSystem::render(float dt) { preRender(dt); unsigned int newParticles = 0; if (mParticleList.size() < static_cast<unsigned int>(mMaxParticles)) { mCurrentParticleCount += dt * mParticlesPerSecond; newParticles = static_cast<unsigned int>(mCurrentParticleCount); if (mParticleList.size() + newParticles > static_cast<unsigned int>(mMaxParticles)) { newParticles = mMaxParticles - mParticleList.size(); } mCurrentParticleCount -= static_cast<float>(newParticles); } else { mCurrentParticleCount = 0.0f; } if (newParticles > 0) { for (unsigned int i = 0; i < newParticles; i++) { newParticle(); } } float accX = getAccelerationX() * dt; float accY = getAccelerationY() * dt; float ageResp = 1.0f / mMaxAge; float alphaRange = mEndAlpha - mStartAlpha; for (size_t i = 0; i < mParticleList.size(); i++) { Particle *particle = mParticleList[i]; float t = particle->getAge() * ageResp; particle->getGfxComponent()->setAlpha(t * alphaRange + mStartAlpha); particle->preRender(*this, accX, accY, dt); if (particle->getAge() >= mMaxAge) { particle->postRender(dt); mParticleList.erase(mParticleList.begin() + i); i--; continue; } particle->render(dt); particle->postRender(dt); } postRender(dt); }
particle *floodfill(matrix *mat, int id, int x, int y){ particle *newparticle; int *current_id; if((current_id = calloc(1,sizeof(int)))==NULL){ fprintf(stderr, "Out of memory error\n"); exit(1); } *current_id = 1; newparticle = newParticle(id); pt *newpt; newpt = newPt(*current_id,x,y); newparticle->spt = newpt; newparticle->ept = newpt; newparticle->size++; floodfill_work(mat,newparticle,x,y,current_id); free(current_id); return newparticle; }
void ParticleEmitter::update(float deltaTime) { std::list<Particle>::iterator it = m_particles.begin(); while(it != m_particles.end()) { if (!it->update(deltaTime, *this)) it = m_particles.erase(it); else it++; } if (m_num == 0) m_time = 0; else m_time += deltaTime; while (m_time >= m_freq && m_num != 0) { m_time -= m_freq; if (m_num > 0) m_num--; newParticle(); m_particles.back().update(m_time, *this); } }
particle ParticleEmitter::updateParticle(uint16_t i) { particle *p = &particles[i]; p->currentStripPosition = p->currentStripPosition + (maxVelocity * p->velocity); // * //float(millis() - p->startTime); if (0) { if (p->currentStripPosition < 0.0) p->currentStripPosition = 1.0; else if (p->currentStripPosition > 1.0) p->currentStripPosition = 0.0; } else { if (p->currentStripPosition < -1.0 || p->currentStripPosition > 2.0) *p = newParticle(); } return *p; }
void LocalizationManager::UpdateParticles(double deltaX, double deltaY, double deltaTetha, float laserScan[]) { ParticleVector::iterator pd = _particleVector.begin(); ParticleVector::iterator pEnd = _particleVector.end(); int counter = 0; while (pd != pEnd) { pd->update(deltaX, deltaY, deltaTetha, laserScan, Helper::HALF_SCAN_SPAN); if (pd->getBelief() < THRESHOLD && _particleVector.size() > 1) { _particleVector.erase(pd); } else { counter++; if (counter % 13 == 0) { Map* m = pd->getMap(); // m->printMap(); cout << endl << endl; counter = 0; } if (_particleVector.size() < PARTICLES_QTY) { Location* pLocation = pd->getLocation(); Particle newParticle(pLocation->getX(), pLocation->getY(), pd->getMap()); newParticle.setYaw(pLocation->getYaw()); newParticle.setMap(pd->getMap()); _particleVector.push_back(newParticle); delete pLocation; } } pd++; } }
bool comet::dead(){ if(collision(x, x + width , robot_x, robot_x + robotWidth, y, y + height, robot_y, robot_y + robotHeight)){ if(!invincible){ isDead = true; debrisCollided +=1; } if( particles_on){ int iteratorX = 0; int iteratorY = 0; for(int i = 0; i < width * height; i++){ if(iteratorX < width - 1){ iteratorX ++; } else{ iteratorX = 0; iteratorY ++; } particle newParticle( iteratorX + x, iteratorY + y, getpixel(image[0], iteratorX, iteratorY), random(-8,-1), random(1,8), random(-8,-1), random(1,8), 0); debris.push_back( newParticle); } } } return isDead; }
//-------------------------------------------------------------- void testApp::update(){ vidGrabber.grabFrame(); if (vidGrabber.isFrameNew()){ int totalPixels = camWidth*camHeight*3; unsigned char * pixels = vidGrabber.getPixels(); camImg.setFromPixels(pixels,camWidth,camHeight,OF_IMAGE_COLOR,true); if (((ofGetElapsedTimeMillis() - elapsedTime) > 10) && (keyPressed_Space || keyPressed_D || keyPressed_S || keyPressed_F)){ finder.findHaarObjects(camImg); elapsedTime = ofGetElapsedTimeMillis(); } //Particle system ps.run(); if (keyPressed_Space){ for(int i = 0; i < finder.blobs.size(); i++) { ofRectangle cur = finder.blobs[i].boundingRect; //ofRect(cur.x, cur.y, cur.width, cur.height); ofVec2f currentCentroid(finder.blobs[i].centroid.x,finder.blobs[i].centroid.y); ofVec2f faceVelocity(previousCentroid.x - currentCentroid.x,previousCentroid.y - currentCentroid.y); //If you shake your head the particles go away if (currentCentroid.distance(previousCentroid) > 5){ spawnRate *= 0.55; } else{ spawnRate *= ofRandom(1.0,5.0); } //clamp it to something reasonable spawnRate = MIN(MAX(spawnRate,1),100); for(int j = 0; j < spawnRate; j++){ ofVec2f accel; ofVec2f velocity; ofVec2f location; float radius = ofRandom(3,8); velocity.set(0,0); //Make the particles dynamic in this case if (keyPressed_A){ accel.set(0,0.5); velocity.set(0,-1); velocity -= (faceVelocity*0.8); } else{ accel.set(0.0,0.0); } location.set(int(currentCentroid.x + ofRandom(-cur.width,cur.width)*0.5),int(currentCentroid.y + ofRandom(-cur.height,cur.height)*0.5)); int colorIndex = (location.y * camWidth + location.x-1)*3; int distanceAlphaValue = ofClamp((255 - (location.distance(currentCentroid) / (cur.width*0.5)) * 255),0,255); ofColor particleColor(pixels[colorIndex],pixels[colorIndex+1],pixels[colorIndex+2],distanceAlphaValue); //ofColor particleColor(ofRandom(128,255),0,0); Particle newParticle(accel,velocity,location,radius,particleColor); ps.addParticle(newParticle); } previousCentroid.set(finder.blobs[i].centroid.x,finder.blobs[i].centroid.y); } } } }
// Update loop void menu::update(){ //Menu animations if( animation_pos < 100 && !startClicked) animation_pos += 4; if( animation_pos > 0 && startClicked) animation_pos -= 4; // Start the game if( startClicked && animation_pos <= 0) set_next_state( STATE_GAME); // Open submenu or start game if( mini_screen == MINISTATE_MENU){ // Start game with controller if( joystickListener::buttonPressed[JOY_XBOX_START] || joystickListener::buttonPressed[JOY_XBOX_A]){ startClicked = true; } // Buttons if( mouseListener::mouse_pressed & 1){ // Start game if( collision( mouseListener::mouse_x, mouseListener::mouse_x, 40, 40 + al_get_bitmap_width(start), mouseListener::mouse_y, mouseListener::mouse_y, 410, 410 + al_get_bitmap_height(start))){ startClicked = true; } // Scores else if( collision( mouseListener::mouse_x, mouseListener::mouse_x, 660, 660 + al_get_bitmap_width(highscores_button), mouseListener::mouse_y, mouseListener::mouse_y, 30, 30 + al_get_bitmap_height(highscores_button))){ updateScores( scores); mini_screen = MINISTATE_SCORES; } // Credits menu else if( collision( mouseListener::mouse_x, mouseListener::mouse_x, 542, 644, mouseListener::mouse_y, mouseListener::mouse_y, 548, 600)){ mini_screen = MINISTATE_CREDITS; } // Controls menu else if( collision( mouseListener::mouse_x, mouseListener::mouse_x, 644, 696, mouseListener::mouse_y, mouseListener::mouse_y, 548 ,600)){ mini_screen = MINISTATE_CONTROLS; } // Help screen else if( collision( mouseListener::mouse_x, mouseListener::mouse_x, 696, 749, mouseListener::mouse_y, mouseListener::mouse_y, 548, 600)){ mini_screen = MINISTATE_TUTORIAL; } // Options menu else if( collision( mouseListener::mouse_x, mouseListener::mouse_x, 749, 800, mouseListener::mouse_y, mouseListener::mouse_y, 548, 600)){ mini_screen = MINISTATE_OPTIONS; } } } // Exit menus else if( mini_screen == MINISTATE_TUTORIAL || mini_screen == MINISTATE_CREDITS || mini_screen == MINISTATE_CONTROLS || mini_screen == MINISTATE_SCORES ){ if( keyListener::lastKeyPressed != -1 || mouseListener::mouse_pressed & 1 || joystickListener::lastButtonPressed != -1){ mini_screen = MINISTATE_MENU; draw(); } } // Options else if( mini_screen == MINISTATE_OPTIONS && mouseListener::mouse_pressed & 1){ // Particles toggle if( collision( 280, 360, mouseListener::mouse_x, mouseListener::mouse_x, 400, 480, mouseListener::mouse_y, mouseListener::mouse_y)){ settings[SETTING_PARTICLE_TYPE] = (settings[SETTING_PARTICLE_TYPE] + 1) % 4; } // Sound button toggle else if( collision( 120, 200, mouseListener::mouse_x, mouseListener::mouse_x, 180, 260, mouseListener::mouse_y, mouseListener::mouse_y)){ settings[SETTING_SOUND] = (settings[SETTING_SOUND] + 1) % 2; } // Music button toggle else if( collision( 280, 360, mouseListener::mouse_x, mouseListener::mouse_x, 180, 260, mouseListener::mouse_y, mouseListener::mouse_y)){ settings[SETTING_MUSIC] = (settings[SETTING_MUSIC] + 1) % 2; if( settings[SETTING_MUSIC] == 0) al_stop_sample( ¤tMusic); else al_play_sample( music_mainmenu, 1.0, 0.0, 1.0, ALLEGRO_PLAYMODE_ONCE, ¤tMusic); } // Fullscreen toggle else if( collision( 120, 200, mouseListener::mouse_x, mouseListener::mouse_x, 400, 480, mouseListener::mouse_y, mouseListener::mouse_y)){ settings[SETTING_FULLSCREEN] = (settings[SETTING_FULLSCREEN] + 1) % 2; if( settings[SETTING_FULLSCREEN]){ // Fullscreen stuff al_destroy_display( display); al_set_new_display_flags(ALLEGRO_FULLSCREEN_WINDOW); display = al_create_display( SCREEN_W, SCREEN_H); ALLEGRO_DISPLAY_MODE disp_data; al_get_display_mode(al_get_num_display_modes() - 1, &disp_data); float sx = disp_data.width / (float)SCREEN_W; float sy = disp_data.height / (float)SCREEN_H; ALLEGRO_TRANSFORM trans; al_identity_transform(&trans); al_scale_transform(&trans, sx, sy); al_use_transform(&trans); al_hide_mouse_cursor( display); } else{ al_destroy_display( display); al_set_new_display_flags(ALLEGRO_WINDOWED); display = al_create_display( SCREEN_W, SCREEN_H); al_hide_mouse_cursor( display); } } //Screen shake else if( collision( 280, 360, mouseListener::mouse_x, mouseListener::mouse_x, 290, 370, mouseListener::mouse_y, mouseListener::mouse_y)){ settings[SETTING_SCREENSHAKE] = (settings[SETTING_SCREENSHAKE] + 1) % 4; } // Control Toggle else if( collision( 120, 200, mouseListener::mouse_x, mouseListener::mouse_x, 290, 370, mouseListener::mouse_y, mouseListener::mouse_y)){ settings[SETTING_CONTROLMODE] = ((settings[SETTING_CONTROLMODE] + 1) % 3); } // Power off else if( collision( 540, 620, mouseListener::mouse_x, mouseListener::mouse_x, 180, 260, mouseListener::mouse_y, mouseListener::mouse_y)){ write_settings(); set_next_state( STATE_EXIT); } // Exit menu else if( collision( 540, 620, mouseListener::mouse_x, mouseListener::mouse_x, 407, 487, mouseListener::mouse_y, mouseListener::mouse_y)){ mini_screen = MINISTATE_MENU; write_settings(); } } // Update mouse particles if( settings[SETTING_PARTICLE_TYPE] != 3 && mouse_rocket_up){ for( int i = 0; i < 500; i++){ if( random( 1, 10) == 1){ ALLEGRO_COLOR part_color = al_map_rgb( 255, random(0,255), 0); if( settings[SETTING_CHRISTMAS]){ int red_or_green = random( 0, 1) * 255; part_color = al_map_rgb( red_or_green, 255 - red_or_green, 0); } particle newParticle( mouseListener::mouse_x, mouseListener::mouse_y + 16, part_color, random( -2, 2), random( 8, 20), 1, settings[SETTING_PARTICLE_TYPE]); mousePart.push_back( newParticle); } } } for( unsigned int i = 0; i < mousePart.size(); i++){ mousePart.at(i).logic(); if( random( 0, 10) == 0) mousePart.erase( mousePart.begin() + i); } // Close game if( keyListener::key[ALLEGRO_KEY_ESCAPE]) set_next_state( STATE_EXIT); // Check if mouse is going up mouse_rocket_up = ( mouseListener::mouse_y < mouseMove); mouseMove = mouseListener::mouse_y; }