// Destroy all particles in the system, drop a particle onto the fixture and // keep track of the number of contacts with the fixture in addition to the // stuck particle candiates. void BodyContactTests::DropParticle() { // Reset counters. m_contacts = 0; m_stuck = 0; DestroyAllParticles(); const float32 timeStep = 1.0f / 60.0f; const int32 timeout = (int32)(1.0f / timeStep) * 10; // 10 "seconds" const int32 velocityIterations = 6; const int32 positionIterations = 2; // Step once to eliminate particles. m_world->Step(timeStep, velocityIterations, positionIterations); b2ParticleDef pd; pd.position.Set(0.0f, 33.0f); pd.velocity.Set(0.0f, -1.0f); m_particleSystem->CreateParticle(pd); for (int32 i = 0; i < timeout; ++i) { m_world->Step(timeStep, velocityIterations, positionIterations); m_contacts += m_particleSystem->GetBodyContactCount(); int32 stuck = m_particleSystem->GetStuckCandidateCount(); if (stuck) { m_stuck += stuck; // Should always be particle 0. EXPECT_EQ(*(m_particleSystem->GetStuckCandidates()), 0); } } }
void Ex58AdvancedCollisionsApp::update() { if ( Rand::randInt( 100 ) == 90 ) { squares.push_back( new Square( world, Vec2f( Rand::randFloat( getWindowWidth() ), -10.0f ), 0.0f, 10.0f ) ); } if ( Rand::randInt( 100 ) == 90 ) { circles.push_back( new Circle( world, Vec2f( Rand::randFloat( getWindowWidth() ), -10.0f ), 0.0f, 10.0f ) ); } if ( Rand::randInt( 100 ) == 90 ) { triangles.push_back( new Triugolnik( world, Vec2f( Rand::randFloat( getWindowWidth() ), -10.0f ), 0.0f, 10.0f ) ); } world->Step( timeStep, velocityIterations, positionIterations ); for ( auto& s : squares ) { s->update(); } for ( auto& t : triangles ) { t->update(); } for ( auto& c : circles ) { c->update(); } }
void Box2dApp::update() { for( int i = 0; i < 10; ++i ){ mWorld->Step( 1 / 30.0f, 10, 10 ); } for( list<box1*>::iterator boxIt = mBoxes.begin(); boxIt != mBoxes.end(); ) { if((*boxIt)->isDead()){ ps.push_back(new particleSystem(200,(*boxIt)->getPosition(), (*boxIt)->getAngle())); boxIt = mBoxes.erase(boxIt); }else { (*boxIt)->update(); ++boxIt; } } for( list<particleSystem*>::iterator psIt = ps.begin(); psIt != ps.end();) { if((*psIt)->isDead() == true){ psIt = ps.erase(psIt);} else{ (*psIt)->update(); ++psIt; } } }
void step(b2World& world, float dt) { const int maxSteps = 20; const float fixedDt = 1.0f / 60.f; const float minDt = fixedDt / 10.f; int stepsPerformed = 0; float frameTime = dt; while (frameTime > 0.0f && stepsPerformed < maxSteps) { float delta = (std::min)(frameTime, fixedDt); frameTime -= delta; if (frameTime < minDt) { delta += frameTime; frameTime = 0.0f; } const int velocityIterations = 8; const int positionIterations = 3; world.Step(delta, velocityIterations, positionIterations); } world.ClearForces(); }
// advance the physical simulator steps (must be called every frame to do physics) void physics_step(b2World& world) { static Uint32 time = Ness::get_ticks(); static Uint32 lasttime = time; time = Ness::get_ticks(); world.Step((time - lasttime) / 1000.0f, 10, 10); lasttime = time; // lasttime is a member variable that holds the ticks since the last loop }
// Run simulation for a specified amount of time at the specified // frequency. void BodyContactTests::RunStep(const float32 frequencyHz, const float32 simulationTime) { const float32 timeStep = 1.0f / frequencyHz; const int32 frames = b2Max((int32)(frequencyHz * simulationTime), 1); for (int32 i = 0; i < frames; ++i) { m_world->Step(timeStep, 1, 1); } }
void display_world(b2World& world,sf::RenderWindow& render) { world.Step(1.0/60,int32(8),int32(3)); render.clear(); for (b2Body* body=world.GetBodyList(); body!=nullptr; body=body->GetNext()) { sf::Shape* shape = static_cast<sf::Shape*>(body->GetUserData()); shape->setPosition(converter::meters_to_pixels(body->GetPosition().x),converter::meters_to_pixels(body->GetPosition().y)); shape->setRotation(converter::rad_to_deg<double>(body->GetAngle())); render.draw(*shape); } render.display(); }
void Update (int timer) { float32 timeStep = 1 / 60.0f; //the length of time passed to simulate (seconds) int32 velocityIterations = 6; //how strongly to correct velocity int32 positionIterations = 3; //how strongly to correct position myWorld.Step ( timeStep, velocityIterations, positionIterations); for ( b2Body * b = myWorld.GetBodyList (); b; b = b->GetNext () ) { //do something with the body 'b' if (b->GetUserData () == 0) { break; } } glutPostRedisplay (); CalculateFps (); glutTimerFunc (1000.0f / 60.0f , Update, 1); }
int32 ConfinementTests::TestLeakCount() { for (int32 t = 0; t < NUMBER_OF_STEPS; t++) { m_world->Step(DELTA_T, 1, 1); } int32 bufferIndex = m_particleGroup->GetBufferIndex(); int32 particleCount = m_particleGroup->GetParticleCount(); const b2Vec2 *positionBuffer = m_particleSystem->GetPositionBuffer(); int32 leakCount = 0; for (int32 i = 0; i < particleCount; i++) { b2Vec2 p = positionBuffer[bufferIndex + i]; if (std::abs(p.x) > WIDTH || std::abs(p.y) > HEIGHT) { leakCount++; } } return leakCount; }
void Ex52FixedObjectsApp::update() { if (addBoxes) { boxes.push_back( Box( world, mousePosition.x, mousePosition.y, Rand::randFloat(5.0f, 20.0f), Rand::randFloat(5.0f, 20.0f) ) ); } // World step: float32 timeStep = 1.0f / 60.0f; int32 velocityIterations = 8; int32 positionIterations = 3; world->Step( timeStep, velocityIterations, positionIterations ); for ( std::vector<Box>::iterator iter = boxes.begin(); iter != boxes.end(); ) { iter->update( world ); if (iter->isDead()) { world->DestroyBody( iter->getBody() ); boxes.erase( iter ); } else { ++iter; } } }
int main(int argc, char * argv[]) { // VARS const float32 fps = 60.0f; const float32 timeStep = 1.0f / fps; const int32 velocityIter = 8; const int32 positionIter = 3; // SFML EventWindow app(VideoMode(WIDTH, HEIGHT, BPP), "Box2D",60); app.addEvent(EventManager::createEvent<sf::RenderWindow*>(addSoftCircleCallBack,&app,sf::Event::MouseButtonPressed,sf::Mouse::Left)); app.addEvent(EventManager::createEvent<sf::RenderWindow*>(addCircleCallBack,&app,sf::Event::MouseButtonPressed,sf::Mouse::Right)); app.addEvent(EventManager::createEvent<sf::RenderWindow*>(addEntityCallBack,&app,sf::Event::KeyPressed,sf::Keyboard::Space),true); //Move event app.addEvent(EventManager::createEvent<sf::RenderWindow*,float,float>(moveWindow,&app,10.f,0.f,sf::Event::KeyPressed,sf::Keyboard::Right),true); app.addEvent(EventManager::createEvent<sf::RenderWindow*,float,float>(moveWindow,&app,-10.f,0.f,sf::Event::KeyPressed,sf::Keyboard::Left),true); app.addEvent(EventManager::createEvent<sf::RenderWindow*,float,float>(moveWindow,&app,0.f,10.f,sf::Event::KeyPressed,sf::Keyboard::Down),true); app.addEvent(EventManager::createEvent<sf::RenderWindow*,float,float>(moveWindow,&app,0.f,-10.f,sf::Event::KeyPressed,sf::Keyboard::Up),true); // BOX2D bodys[0] = new RectBody(0,300,10000,10,b2_staticBody); bodys[0]->SetRotation(25); bodys[1] = new RectBody(300,0,10000,10,b2_staticBody); bodys[1]->SetRotation(90); bodys[2] = new RectBody(0,-3500,10000,10,b2_staticBody); bodys[2]->SetRotation(180); bodys[3] = new RectBody(-400,0,10000,10,b2_staticBody); bodys[3]->SetRotation(90); { sf::Image image; if (!image.loadFromFile("smile.png")) exit(1); glEnable(GL_TEXTURE_2D); glGenTextures(1, &texture); glBindTexture(GL_TEXTURE_2D, texture); Vector2u size = image.getSize(); gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGBA, size.x, size.y, GL_RGBA, GL_UNSIGNED_BYTE, image.getPixelsPtr()); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR); glDisable(GL_TEXTURE_2D); } // MAIN LOOP while(app.isOpen()) { app.doEvents(); world.Step(timeStep, velocityIter, positionIter); // on calcule la frame suivante app.clear(sf::Color::Red); for (int i =bodys.size()-1;i>=0;--i) bodys[i]->Draw(app); app.display(); } glDeleteTextures(1, &texture); for(unsigned int i=0; i<bodys.size();++i) delete bodys[i]; return EXIT_SUCCESS; }
int main() { // Initialize globals initGlobals(); // Window window.setSize(sf::Vector2u(window_width, window_height)); window.setPosition(sf::Vector2i(200, 200)); window.setFramerateLimit(FRAMES_PER_SECOND); //window.setVerticalSyncEnabled(true); window.setKeyRepeatEnabled(false); // Camera view sf::View windowView; // Menu initializeMenu(); // UI ui.init(); // Minimap // Minimap minimap; // Create & Set contactlistener BoxContactListener boxContactListener; physicsWorld.SetContactListener(&boxContactListener); // Build world //gameWorld.generateWorld(physicsWorld); // ------------------------------- MAIN LOOP ------------------------------- while(window.isOpen()) { // ------------------------------- Input & Views ------------------------------- sf::Event event; gameEvents.processEvents(window, event); // Update view in case of window resize window_width = window.getSize().x; window_height = window.getSize().y; windowView.setSize(window_width, window_height); // if(minimap.updateViewThroughMinimap) // { // windowView.setCenter(minimap.newViewCenter.x, minimap.newViewCenter.y); // } if(player.hasNewFocus) { windowView.setCenter(player.getNewFocus().x, player.getNewFocus().y); } // Update normal view with inputs windowView = updateView(windowView); if(gameWorld.completionStarted && gameWorld.completionTimer.timeReached()) { windowView.setCenter(window_width/2, window_height/2); global_levelComplete = false; } // Clear window window.clear(sf::Color(255, 255, 255, 255)); if(global_isMenu) { ui.setSize(window_width, window_height); window.setView(ui.getView()); menu_logo_bottom->setPosition(window_width - textures["menu_logo_bottom"].getSize().x, window_height - textures["menu_logo_bottom"].getSize().y); menu.update(gameEvents); menu.draw(window); // Instructions if(show_instructions) { showInstructions(); if(show_instructions_controls) { showControls(); } if(show_instructions_gameplay) { showGameplay(); } } } else { window.setView(windowView); // ------------------------------- Updates ------------------------------- // Player player.update(physicsWorld, gameEvents, gameWorld); if(!global_isPaused) { // Physics physicsWorld.Step(TIME_STEP, VELOCITY_ITERATIONS, POSITION_ITERATIONS); // World gameWorld.update(gameEvents, physicsWorld, player); // UI ui.update(gameWorld, player); } // Calculate viewable area int viewShiftX = window.getSize().x/2 - window.getView().getCenter().x; int viewShiftY = window.getSize().y/2 - window.getView().getCenter().y; int windowMinX = -100 - viewShiftX; int windowMaxX = windowMinX + window_width + 200; int windowMinY = -100 - viewShiftY; int windowMaxY = windowMinY + window_height + 200; // ------------------------------- Drawing ------------------------------- window.setView(ui.getView()); // Background sf::Vertex rectangle[] = { sf::Vertex(sf::Vector2f(0.0f, 0.0f), sf::Color(0, 100, 130, 255)), sf::Vertex(sf::Vector2f(window_width, 0.0f), sf::Color(0, 100,130, 255)), sf::Vertex(sf::Vector2f(window_width, window_height), sf::Color(0, 200, 230, 255)), sf::Vertex(sf::Vector2f(0.0f, window_height), sf::Color(0, 200, 230, 255)) }; window.draw(rectangle, 4, sf::Quads); window.setView(windowView); // World & Player gameWorld.draw(window, b2Vec2(windowMinX, windowMinY), b2Vec2(windowMaxX, windowMaxY), false); player.draw(window); // HUD !!CLASS!! ui.setSize(window_width, window_height); window.setView(ui.getView()); if(!gameWorld.completionTimer.timeReached() && gameWorld.completionStarted) { sf::Text temp("You have won", font_default, 40); int midX = window_width / 2.0f - 100; int midY = window_height / 2.0f - 40; temp.setPosition(midX, midY); temp.setColor(sf::Color::White); window.draw(temp); } if(global_isPaused) { sf::RectangleShape rect; rect.setSize(sf::Vector2f(window_width, window_height)); rect.setPosition(0,0); rect.setFillColor(sf::Color(100, 100, 100, 155)); window.draw(rect); } ui.draw(window); // sf::RectangleShape border; // border.setPosition(0.0f, window_height - (window_height*minimapSize + MINIMAP_BORDER_SIZE)); // border.setSize(sf::Vector2f(window_width*minimapSize + MINIMAP_BORDER_SIZE, window_height*minimapSize + MINIMAP_BORDER_SIZE)); // border.setFillColor(sf::Color(0, 0, 200, 255)); // // window.draw(border); // // // ------------------------------- Minimap ------------------------------- // // minimap.setSize(MAX_WORLD_WIDTH * BOX_SIZE, MAX_WORLD_WIDTH * BOX_SIZE, window_width, window_height); // minimap.calcViewport(minimapSize); // minimap.setCameraPosition(windowView.getCenter().x, windowView.getCenter().y); // window.setView(minimap.getUpdatedView()); // // minimap.update(gameEvents); // // minimap.draw(window); // gameWorld.draw(window, b2Vec2(-10000, -20000), b2Vec2(10000, 10000), true); // player.draw(window); // minimap.drawCameraWindow(window); // Default View again window.setView(windowView); } // Display window.display(); } return 0; }
void Ex59AdvancedJointsApp::update() { arm->update( mouse ); world->Step( timeStep, velocityIterations, positionIterations ); world->ClearForces(); }
void _TBOX_PREFIX_App::update() { for( int i = 0; i < 10; ++i ) mWorld->Step( 1 / 30.0f, 10, 10 ); }
void NOC_5_07_RevoluteJointApp::update() { for( int i = 0; i < 2; ++i ) mWorld->Step( 1 / 30.0f, 10, 10 ); }