void processRemoval(b2Vec2 removalPosition, float removalRadius, b2World& world, bool simplifyGeometry)
{
	auto foundBodies = queryDestructibleBodies(removalPosition, removalRadius, world);
	auto batch = matchBodiesToRings(foundBodies.begin(), foundBodies.end());

	// Partition the shapes by area, so that elements to be processed are at the beginning
	auto borderIt = std::partition(batch.begin(), batch.end(), [](const match_t& m) {
		const double areaEpsilon = 0.02;
		return bg::area(m.second) > areaEpsilon;
	});

	// Remove small shapes
	std::for_each(borderIt, batch.end(), [&](const match_t& m) {
		world.DestroyBody(m.first);
	});

	// Subtract the input polygon from each shape returned from the query
	ring_t diff = makeConvexRing(removalPosition, removalRadius, 16);
	boost::geometry::correct(diff);

	typedef std::pair<std::unique_ptr<b2ChainShape>, b2Filter> shape_property_t;
	std::vector<shape_property_t> resultShapes;
	std::for_each(batch.begin(), borderIt, [&](const match_t& m) {
		auto subtractionResult = subtract(m.second, diff);
		// Simplify the results
		if (simplifyGeometry)
		{
			simplify(subtractionResult);
		}
		
		// Convert the rings to b2ChainShapes and add to result shapes
		auto converted = convertGeometry(subtractionResult);

		auto moveBegin = std::make_move_iterator(converted.begin());
		auto moveEnd = std::make_move_iterator(converted.end());
		std::transform(moveBegin, moveEnd, std::back_inserter(resultShapes),
			[&](std::unique_ptr<b2ChainShape> converted) {
			auto filter = m.first->GetFixtureList()->GetFilterData();
			return std::make_pair(std::move(converted), filter);
		});
		
		if (!subtractionResult.empty())
		{
			world.DestroyBody(m.first);
		}
	});

	for (auto&& s : resultShapes)
	{
		b2BodyDef bd;
		b2Body* body = world.CreateBody(&bd);
		auto fixture = body->CreateFixture(s.first.get(), 0.0f);
		fixture->SetFilterData(s.second);
	}
}
Exemple #2
0
void ReleaseNormalKeyInput (unsigned char key, int x, int y)
{
   printf (" input %d\n", key );
   switch (key)
   {
      case 'w':
         break;
      case 'f':

         break;
      case 'a':

         break;
      case 'r':
         for ( b2Body * b = myWorld.GetBodyList (); b; b = b->GetNext () )
         {
            myWorld.DestroyBody (b);
         }
         delete agent;

         agent = new Agent (myWorld);
         readFile ("map.txt");
         break;

      case '1':
         break;
      default:
         break;
   }
}
Exemple #3
0
void Player::clear(b2World& world)
{
	if (m_defender != nullptr)
	{
		world.DestroyBody(m_defender->getBody());
		delete m_defender;
		m_defender = nullptr;
	}

	if (m_gatherer != nullptr)
	{
		world.DestroyBody(m_gatherer->getBody());
		delete m_gatherer;
		m_gatherer = nullptr;
	}

	m_dead = false;
	m_won = false;
	m_points = 0.f;
}
void CBox::ResetPosition(b2World & world) {
	//destroy stuff (memory management, otherwise they build up)
	m_body->DestroyFixture(m_body->GetFixtureList());
	world.DestroyBody(m_body);
	n++;
	//m_shape.SetOrigin(5,5); 
	m_bodyDef.position.Set(300.0f/PPM,-300.0f/PPM-n*5/PPM);
	m_bodyDef.angularVelocity = 0;
	//m_bodyDef.angle = 270;
	m_bodyDef.linearVelocity.Set(0, 60);
	//reset fixture and body
	SetWorld(world);
	//reset shape
	update();
}
FixtureShape::FixtureShape(b2World & world, b2Vec2 initialPos, b2Vec2 size,
                           unsigned ySize, float PPM, sf::Color color)
: Shape(world, initialPos, ySize, PPM)
{
    mShape = std::unique_ptr<sf::RectangleShape>(new sf::RectangleShape(sf::Vector2f(size.x,size.y)));
    mShape->setFillColor(color);
    mShape->setOrigin(size.x/2.f, size.y/2.f);
    world.DestroyBody(mBody);
    mBodyDef.type = b2_staticBody;
    mBody = world.CreateBody(&mBodyDef);
    mBodyShape = std::unique_ptr<b2PolygonShape>(new b2PolygonShape());
    static_cast<b2PolygonShape*>(mBodyShape.get())->SetAsBox(size.x/(2*mPPM),
                                                             size.y/(2*mPPM));
    setBodyFix(0.3f, 0.5f, 0.25f);
    update();
}
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;
        }
    }
}
Exemple #7
0
    MapLevel::MapLevel(Context& context, std::size_t level, sf::Image& levelData, b2World& world)
        : mContext(context)
        , mWorld(LevelWidth, std::vector<Tile>(LevelHeight))
        , mBoxWorld(world)
        , mTexture(context.getTexture(Textures::GameAtlas))
        , mDestinationList()
        , mSourceList()
        , mEntities()
    {
        LOG_CALL("MapLevel::MapLevel");

        // Reset Box2D world
        LOG_DEBUG("Clearing Box2D world from entities: %d to remove.", mBoxWorld.GetBodyCount());
        b2Body* body = world.GetBodyList();
        while (nullptr != body) {

            b2Body* nextBody = body->GetNext();
            world.DestroyBody(body);
            body = nextBody;
        }

        // Create Box2D world boundaries
        b2utils::createGround(mBoxWorld, 20.f, 300.f, 40.f, 600.f);
        b2utils::createGround(mBoxWorld, 780.f, 300.f, 40.f, 600.f);

        b2utils::createGround(mBoxWorld, 400.f, 18.f, 800.f, 36.f);
        b2utils::createGround(mBoxWorld, 400.f, 582.f, 800.f, 36.f);

        loadLevel(level, levelData);
        initSprites();

        //mDebugPlayerSpawn.setFillColor(sf::Color::Yellow);
        //LOG_DEBUG("Player spawn position: (%d, %d)", mPlayerSpawnPos.x, mPlayerSpawnPos.y);
        //mDebugPlayerSpawn.setPosition(WorldOffsetX + TileWidth * mPlayerSpawnPos.x, WorldOffsetY + TileHeight * mPlayerSpawnPos.y);
        //mDebugPlayerSpawn.setSize(sf::Vector2f(24.f, 24.f));
    }
void Ex52FixedObjectsApp::shutdown() {
    for (auto& b : boxes) {
        world->DestroyBody( b.getBody() );
    }
    delete world;
}
void Ex51aBox2DCustomApp::shutdown() {
    for ( auto& b : boxes ) {
        world->DestroyBody( b.getBody() );
    }
    delete world;
}