void createWorld (b2World& world) {
        {
            b2BodyDef bd;
            b2Body * ground = world.CreateBody(&bd);

            b2EdgeShape* shape = new b2EdgeShape();
            shape->Set(b2Vec2(-40, 0), b2Vec2(40, 0));
            ground->CreateFixture(shape, 0.0f);
            delete shape;
        }

        {
            b2CircleShape * shape = new b2CircleShape();
            shape->m_radius = 1;

            b2FixtureDef fd;
            fd.shape = shape;
            fd.density = 1.0f;

            const int length = 7;
            float restitution[length] = {0, 0.1f, 0.3f, 0.5f, 0.75f, 0.9f, 1.0f};
            
            for (int i = 0; i < length; i++) {
                b2BodyDef bd;
                bd.type = b2_dynamicBody;
                bd.position.Set(-10.0f + 3.0f * i, 20.0f);

                b2Body * body = world.CreateBody(&bd);
                fd.restitution = restitution[i];
                body->CreateFixture(&fd);
            }

            delete shape;
        }
    }
void addStaticShapes(b2World& world)
{

	ShapeFactory factory { constants::RENDER_SCALE };
	// Add the nondestructible screen edges
	std::vector<b2Vec2> boundaryPoints =
	{ b2Vec2{ 0.0f, 0.0f }, b2Vec2{ 0.0f, screenHeight },
	b2Vec2{ screenWidth, screenHeight }, b2Vec2{ screenWidth, 0.0f } };
	auto boundaryShape = factory.chain(boundaryPoints.data(), boundaryPoints.size());

	b2BodyDef boundaryDef;
	b2Body* boundaryBody = world.CreateBody(&boundaryDef);
	auto boundaryFixture = boundaryBody->CreateFixture(boundaryShape.get(), 0.0f);
	auto filter = boundaryFixture->GetFilterData();
	filter.categoryBits = Shape::normal;
	boundaryFixture->SetFilterData(filter);

	// Add a destructible polygon
	std::vector<b2Vec2> polygonPoints =
	{ b2Vec2{ screenWidth * 0.1f, screenHeight * 0.4f },
	b2Vec2{ screenWidth * 0.1f, screenHeight * 0.95f },
	b2Vec2{ screenWidth * 0.9f, screenHeight * 0.95f },
	b2Vec2{ screenWidth * 0.9f, screenHeight * 0.7f },
	b2Vec2{ screenWidth * 0.4f, screenHeight * 0.4f } };

	auto polygonShape = factory.chain(polygonPoints.data(), polygonPoints.size());
	b2BodyDef polygonDef;
	b2Body* polygonBody = world.CreateBody(&polygonDef);
	auto polygonFixture = polygonBody->CreateFixture(polygonShape.get(), 0.0f);
	filter.categoryBits = Shape::destructible;
	polygonFixture->SetFilterData(filter);
}
Exemple #3
0
    void createWorld (b2World& world) {
        m_stepCount = 0;
        m_angularVelocity = 0;
        {          
            b2BodyDef bd;
            bd.position.Set(0, 0);
            b2Body * body = world.CreateBody(&bd);

            b2PolygonShape * shape = new b2PolygonShape();
            shape->SetAsEdge(b2Vec2(-10, 0), b2Vec2(10, 0));
            body->CreateFixture(shape, 0);

            shape->SetAsBox(0.2f, 1.0f, b2Vec2(0.5f, 1.0f), 0);
            body->CreateFixture(shape, 0);
            delete shape;
        }

        {
            b2BodyDef bd;
            bd.type = b2_dynamicBody;
            bd.position.Set(0, 20);

            b2PolygonShape * shape = new b2PolygonShape();
            shape->SetAsBox(2, 0.1f);

            m_body = world.CreateBody(&bd);
            m_body->CreateFixture(shape, 1);

            m_angularVelocity = 33.468121f;
            m_body->SetLinearVelocity(b2Vec2(0, -100));
            m_body->SetAngularVelocity(m_angularVelocity);
            delete shape;
        }
    }
    void createWorld (b2World& world) {
        {
            b2BodyDef bd;
            b2Body * ground = world.CreateBody(&bd);

            b2EdgeShape* shape = new b2EdgeShape();
            shape->Set(b2Vec2(-40, 0), b2Vec2(40, 0));
            ground->CreateFixture(shape, 0);
            delete shape;
        }

        {
            b2CircleShape * shape = new b2CircleShape();
            shape->m_radius = 1.0f;

            for (int i = 0; i < e_count; i++) {
                b2BodyDef bd;
                bd.type = b2_dynamicBody;
                bd.position.Set(0, 4.0f + 3.0f * i);
                b2Body * body = world.CreateBody(&bd);
                body->CreateFixture(shape, 1.0f);
            }

            delete shape;
        }
    }
Exemple #5
0
GravityChanger::GravityChanger(b2World& world, float x, float y, bool flipped) : GameObject(world, x, y, "GravityChanger", new GravityChangerDrawable(x,y)), flipped(flipped) {
	b2BodyDef bodyDef;
	bodyDef.position.Set(x,y);
	b2Body* body_ptr=world.CreateBody(&bodyDef);
	b2PolygonShape shape1;
	shape1.SetAsBox(1.0f, 0.5f);
	check1=body_ptr->CreateFixture(&shape1,0.0f);
	b2PolygonShape shape2;
	shape2.SetAsBox(0.4f, 1.0f, b2Vec2(-1,-0.5f), 0);
	b2PolygonShape shape3;
	shape3.SetAsBox(0.4f, 1.0f, b2Vec2(1,-0.5f), 0);
	body_ptr->CreateFixture(&shape2,0);
	body_ptr->CreateFixture(&shape3,0);
	bodies.push_back(PhysBody(body_ptr, body_ptr->GetPosition(), body_ptr->GetAngle()));

	


	b2BodyDef bodyDef2;
	bodyDef2.position.Set(x,y-1.5f);
	bodyDef2.type = b2_dynamicBody;
	b2Body* button_ptr=world.CreateBody(&bodyDef2);
	button_ptr->SetGravityScale(0);
	b2PolygonShape buttonshape;
	buttonshape.SetAsBox(0.5f,0.5f);
	check2=button_ptr->CreateFixture(&buttonshape, 0);
	button_ptr->SetUserData(this);
	bodies.push_back(PhysBody(button_ptr, button_ptr->GetPosition(), button_ptr->GetAngle()));

	b2PrismaticJointDef prismDef;
	prismDef.bodyA = body_ptr;
	prismDef.localAnchorA = b2Vec2(0,-1);
	prismDef.bodyB = button_ptr;
	prismDef.localAnchorB = b2Vec2(0,0);
	prismDef.localAxisA = b2Vec2(0,1);
	prismDef.upperTranslation =1;
	prismDef.lowerTranslation =-0.5f;
	prismDef.enableLimit=true;
	prismDef.collideConnected=true;
	prismDef.enableMotor = true;
	prismDef.maxMotorForce=25;
	prismDef.motorSpeed=-3;
	world.CreateJoint(&prismDef);
	if (flipped) {
		bodies[0].body_ptr->SetTransform(bodies[0].original_pos-b2Vec2(0,1.5f), 3.14159);
		bodies[0].original_pos=bodies[0].body_ptr->GetTransform().p;
		bodies[0].original_rot=3.14159;

		bodies[1].body_ptr->SetTransform(bodies[1].original_pos+b2Vec2(0,1.5f), bodies[1].original_rot);
		bodies[1].original_pos=bodies[1].body_ptr->GetTransform().p;
	}
}
void FlipperRight::create(b2World& World, int MouseX, int MouseY)
{
    //FLIPPER
    vertices[0].Set(0, -0.1);
    vertices[1].Set(-2, 0);
    vertices[2].Set(-2, 0.25);
    vertices[3].Set(0, 0.35);

    BodyDef.position = b2Vec2(MouseX/SCALE, MouseY/SCALE);
    BodyDef.type = b2_dynamicBody;
    Body = World.CreateBody(&BodyDef);
    Shape.Set(vertices, 4);
    FixtureDef.shape = &Shape;
    FixtureDef.density = 1.f;
    Body->CreateFixture(&FixtureDef);


    polygon.setPointCount(4);
    polygon.setPoint(0, sf::Vector2f( 0*SCALE,  -0.1*SCALE));
    polygon.setPoint(1, sf::Vector2f( -2*SCALE,  0*SCALE));
    polygon.setPoint(2, sf::Vector2f( -2*SCALE,  0.25*SCALE));
    polygon.setPoint(3, sf::Vector2f( 0*SCALE,  0.35*SCALE));
    polygon.setFillColor(sf::Color(0,206,209));

    //WALL
    BodyDef2.position = b2Vec2((MouseX+42)/SCALE, (MouseY-15)/SCALE);
    BodyDef2.type = b2_staticBody;
    Body2 = World.CreateBody(&BodyDef2);
    Shape2.SetAsBox((90/2)/SCALE, (10/2)/SCALE);
    FixtureDef2.density = 1.f;
    FixtureDef2.shape = &Shape2;
    Body2->CreateFixture(&FixtureDef2);
    Body2->SetTransform(Body2->GetPosition(), -0.4);

    rect.setSize(sf::Vector2f(90, 10));
    rect.setOrigin(90/2, 10/2);
    rect.setFillColor(sf::Color(0,206,209));

    //JOIN
    jointDef.Initialize(Body, Body2, b2Vec2(MouseX/SCALE,MouseY/SCALE));
    jointDef.lowerAngle = -0.12f * b2_pi;
    jointDef.upperAngle = 0.12f * b2_pi;
    jointDef.enableLimit = true;
    jointDef.maxMotorTorque = 150.0f;
    jointDef.motorSpeed = 2.f;
    jointDef.collideConnected = false;
    jointDef.enableMotor = true;

    joint = (b2RevoluteJoint*)World.CreateJoint(&jointDef);

}
Exemple #7
0
void Enemy::Initialize(b2World& world, b2Vec2 position) {
	bodyDef.position = position;
	bodyDef.type = b2_dynamicBody;
	bodyDef.fixedRotation = true;
	body = world.CreateBody(&bodyDef);

	Convert convert;
	b2Vec2 vs0, vs1, vs2, vs3;

	b2Vec2* vs = new b2Vec2[4];
	vs0 = convert.CoordPixelsToWorld(0, 50, 50.0f, 50.0f);
	vs1 = convert.CoordPixelsToWorld(0, 0, 50.0f, 50.0f);
	vs2 = convert.CoordPixelsToWorld(50, 0, 50.0f, 50.0f);
	vs3 = convert.CoordPixelsToWorld(50, 50, 50.0f, 50.0f);
	vs[0].Set(vs0.x, vs0.y);
	vs[1].Set(vs1.x, vs1.y);
	vs[2].Set(vs2.x, vs2.y);
	vs[3].Set(vs3.x, vs3.y);
	shape.Set(vs, 4);
	delete vs;

	fixtureDef.density = 1.0f;
	fixtureDef.friction = 0.0f;
	fixtureDef.shape = &shape;
	body->CreateFixture(&fixtureDef);

	b2Fixture* enemySensorFixture = body->CreateFixture(&fixtureDef);
	ContactUserData* cud = new ContactUserData();
	cud->type = ContactUserData::Type::ENEMY;
	cud->data = this;
	enemySensorFixture->SetUserData(cud);
}
Exemple #8
0
void Circle::make(b2World& w) {
	// shape
	b2CircleShape circle;
	circle.m_radius = PIXELS_TO_METERS(radius);
	
	
	// body
	if(!body_type_set) {
		if(density == 0.0f) {
			body_def.type = b2_staticBody;
		}
		else {
			body_def.type = b2_dynamicBody;
		}
	}
	body = w.CreateBody(&body_def);
	
	// fixture
	fixture_def.shape = &circle;
	fixture_def.density = density;
	fixture_def.friction = friction;
	fixture_def.restitution = restitution;
	body->CreateFixture(&fixture_def);
//	body->CreateFixture(&circle, 10);

}
void box2d_basicApp::setup()
{
	b2Vec2 gravity( 0.0f, 5.0f );
	mWorld = new b2World( gravity );

	b2BodyDef groundBodyDef;
	groundBodyDef.position.Set( 0.0f, getWindowHeight() );
	b2Body* groundBody = mWorld->CreateBody(&groundBodyDef);

	// Define the ground box shape.
	b2PolygonShape groundBox;

	// The extents are the half-widths of the box.
	groundBox.SetAsBox( getWindowWidth(), 10.0f );

	// Add the ground fixture to the ground body.
	groundBody->CreateFixture(&groundBox, 0.0f);

	//auto ctx = audio::Context::master();
	//auto sourceFile = /*, ctx->getSampleRate()*/);//(app::loadResource("1.wav"));
//audio::BufferRef buffer = sourceFile->loadBuffer();
//    mBufferPlayerNode = ctx->makeNode( new audio::BufferPlayerNode( buffer ) );
//    // add a Gain to reduce the volume
//    mGain = ctx->makeNode( new audio::GainNode( 0.5f ) );
//    // connect and enable the Context
//    mBufferPlayerNode >> mGain >> ctx->getOutput();
//    ctx->enable();

	//float volume = 1.0f - (float)event.getPos().y / (float)getWindowHeight();
	//float pan = (float)event.getPos().x / (float)getWindowWidth();
	//mVoice->setVolume( volume );
	//mVoice->setPan( pan );

	new_game();
}
  void createBodies( b2World& world )
  {
    process();
    if ( hasAttribute( ATTRIB_DECOR ) ){
      return; //decorators have no physical embodiment
    }
    int n = m_shapePath.numPoints();
    if ( n > 1 ) {
      b2BodyDef bodyDef;
      bodyDef.position = m_origin;
      bodyDef.position *= 1.0f/PIXELS_PER_METREf;
      bodyDef.userData = this;
      if ( m_attributes & ATTRIB_SLEEPING ) {
	bodyDef.isSleeping = true;
      }
      m_body = world.CreateBody( &bodyDef );
      for ( int i=1; i<n; i++ ) {
	BoxDef boxDef;
	boxDef.init( m_shapePath.point(i-1),
		     m_shapePath.point(i),
		     m_attributes );
	m_body->CreateShape( &boxDef );
      }
      m_body->SetMassFromShapes();

    }
    transform();
  }
Exemple #11
0
DynamicBox::DynamicBox(std::string name, float x, float y, float rotation, b2World& world) : Collidable(name)
{
    std::ifstream file(name.c_str());
    float density;
    float friction;
    std::string fileName;
    file >> fileName >> density >> friction;


    sprite.setTexture(rm->getTexture(fileName));
    float width = sprite.getGlobalBounds().width;
    float height = sprite.getGlobalBounds().height;

    sprite.setPosition(x, y);
    sprite.setOrigin(width/2.f, height/2.f);

    DEF.position.Set((x+width/2.f)/PPM, (y+height/2.f)/PPM);
    DEF.type = b2_dynamicBody;
    DEF.angle = rotation;
    DEF.linearDamping = 0.5f;

    SHAPE.SetAsBox(width/2.f/PPM, height/2.f/PPM);

    body = world.CreateBody(&DEF);

    FIX.shape = &SHAPE;
    FIX.density = density;
    FIX.friction = friction;
    //FIX.linearDamping = .2;
    FIX.restitution = 0.0;

    body->CreateFixture(&FIX);
    body->SetUserData(this);
}
Exemple #12
0
Object::Object(sf::Vector2f s, sf::Color c, b2World& w, sf::Vector2f p)
{

	objectType t = player;
	m_type = t;

	n++;

	shape.setSize(s);
	shape.setOrigin(s.x/2, s.y/2);

	shape.setFillColor(c);

	bodyDef.type = b2_dynamicBody;

	if(p.x == 0)
		bodyDef.position.Set(toB2(300), -toB2(300.0f/30-n*5));
	else
		bodyDef.position.Set(toB2(p.x), -toB2(p.y));

	dynamicBody.SetAsBox(toB2(10.0f), toB2(10.0f));

	fixtureDef.shape = &dynamicBody;
	fixtureDef.density = 0.3f;
	fixtureDef.friction = 0.5f;

	body = w.CreateBody(&bodyDef);
	body->CreateFixture(&fixtureDef);

	hitbox.setSize(shape.getSize());
	hitbox.setOutlineColor(sf::Color::Red);
	hitbox.setPosition(shape.getPosition());
	hitbox.setOutlineThickness(1);
}
void
ConfinementTests::SetUp()
{
	// Define the gravity vector.
	b2Vec2 gravity(0.0f, -10.0f);

	// Construct a world object, which will hold and simulate the rigid bodies.
	m_world = new b2World(gravity);

	// Create the ground body
	const b2BodyDef groundBodyDef;
	m_groundBody = m_world->CreateBody(&groundBodyDef);

	// Create the particle system
	b2ParticleSystemDef particleSystemDef;
	particleSystemDef.radius = 0.01f;
	m_particleSystem = m_world->CreateParticleSystem(&particleSystemDef);

	// Create particles
	b2ParticleGroupDef particleDef;
	b2PolygonShape particleShape;
	particleShape.SetAsBox(WIDTH, HEIGHT);
	particleDef.shape = &particleShape;
	m_particleGroup = m_particleSystem->CreateParticleGroup(particleDef);
}
void JointPlatform::SetWorld(b2World & world)
{
	
	_pBody = world.CreateBody(&_bodyDef);
	_pBody->CreateFixture(&_fixtureDef);

	_pJointBody = world.CreateBody(&_jointBodyDef);
	_pJointBody->CreateFixture( &_jointFixtureDef);

	_jointDef.bodyA = _pBody;
	_jointDef.bodyB = _pJointBody; 
	_jointDef.localAnchorA = _pBody->GetLocalCenter() ;
	_jointDef.localAnchorB = _pJointBody->GetLocalCenter();

	_pJoint = (b2RevoluteJoint *)world.CreateJoint(&_jointDef);
}
Exemple #15
0
void Rectangle::make(b2World& w) {

	// shape
	b2PolygonShape shape;
	shape.SetAsBox(PIXELS_TO_METERS(hw),PIXELS_TO_METERS(hh));
	
	// body
	if(!body_type_set) {
		if(density == 0.0f) {
			body_def.type = b2_staticBody;
			printf("Creating static rect.\n");
		}
		else {
			body_def.type = b2_dynamicBody;
		}
	}

	body_def.position.Set(PIXELS_TO_METERS(x+hw), PIXELS_TO_METERS(y+hh));
	body = w.CreateBody(&body_def);

	// fixture
	fixture_def.shape = &shape;
	fixture_def.density = density;
	fixture_def.friction = friction;
	fixture_def.restitution = restitution;
	body->CreateFixture(&shape, 0.0f);
}
Exemple #16
0
Seesaw::Seesaw(b2World& world, float x, float y, bool flipped) : GameObject(world, x,y,"Seesaw", new SeesawDrawable(x,y)), flipped(flipped) {
	b2BodyDef bodyDef;
	bodyDef.position.Set(x, y);
	b2Body* body_ptr = world.CreateBody(&bodyDef);
	
	b2Vec2 vertices[3];
	vertices[0].Set(0, 0);
	vertices[1].Set(-1, 2);
	vertices[2].Set(1,2);
	b2PolygonShape polygonShape;
	polygonShape.Set(vertices, 3);
	b2FixtureDef fixtureDef;
	fixtureDef.shape = &polygonShape;
	body_ptr->CreateFixture(&fixtureDef);
	bodies.push_back(PhysBody(body_ptr, body_ptr->GetPosition()));
	
	
	bodyDef.type = b2_dynamicBody;
	bodyDef.position.Set(x, y);
	if (flipped) {
		bodyDef.angle = -0.4;
	}
	else {
		bodyDef.angle = 0.4;
	}
	b2Body* body_ptr2 =world.CreateBody(&bodyDef);
	b2PolygonShape boxShape;
	boxShape.SetAsBox(5,0.2);
	b2FixtureDef fixtureDef2;
	fixtureDef2.shape = &boxShape;
	fixtureDef2.density = 1;
	fixtureDef2.friction = 1;
	fixtureDef2.restitution = 0;
	body_ptr2->CreateFixture(&fixtureDef2);
	bodies.push_back(PhysBody(body_ptr2, body_ptr2->GetPosition(), body_ptr2->GetAngle()));

	b2RevoluteJointDef jointDef;
	jointDef.localAnchorA.Set(0, 0);
	jointDef.localAnchorB.Set(0, 0);
	jointDef.bodyA = body_ptr;
	jointDef.bodyB = body_ptr2;
	jointDef.enableLimit = true;
	jointDef.lowerAngle = -0.4;
	jointDef.upperAngle =  0.4;
	world.CreateJoint(&jointDef);
}
Exemple #17
0
    void createWorld (b2World& world) {
        {
            b2BodyDef bd;
            b2Body * ground = world.CreateBody(&bd);

            b2PolygonShape * shape = new b2PolygonShape();
            shape->SetAsEdge(b2Vec2(-40, 0), b2Vec2(40, 0));
            ground->CreateFixture(shape, 0.0f);

            shape->SetAsEdge(b2Vec2(20, 0), b2Vec2(20, 20));
            ground->CreateFixture(shape, 0);
            delete shape;
        }

        float xs[e_columnCount] = {0, -10, -5, 5, 10};

        for (int j = 0; j < e_columnCount; j++) {
            b2PolygonShape * shape = new b2PolygonShape();
            shape->SetAsBox(0.5f, 0.5f);

            b2FixtureDef fd;
            fd.shape = shape;
            fd.density = 1.0f;
            fd.friction = 0.3f;

            for (int i = 0; i < e_rowCount; i++) {
                b2BodyDef bd;
                bd.type = b2_dynamicBody;

                int n = j * e_rowCount + i;
                m_indices[n] = n;

                float x = 0;
                bd.position.Set(xs[j] + x, 0.752f + 1.54f * i);
                b2Body * body = world.CreateBody(&bd);
                body->SetUserData((void *)n);

                m_bodies[n] = body;
                body->CreateFixture(&fd);
            }

            delete shape;
        }

        m_bullet = NULL;
    }
Exemple #18
0
    void createWorld (b2World& world) {
        b2Body * ground;

        {
            b2BodyDef bd;
            ground = world.CreateBody(&bd);

            b2PolygonShape * shape = new b2PolygonShape;
            shape->SetAsEdge(b2Vec2(-40, 0), b2Vec2(40, 0));

            ground->CreateFixture(shape, 0.0f);
            delete shape;
        }

        {
            b2PolygonShape * shape = new b2PolygonShape;
            shape->SetAsBox(0.6f, 0.125f);

            b2FixtureDef fd;
            fd.shape = shape;
            fd.density = 20.0f;
            fd.friction = 0.2f;

            b2RevoluteJointDef jd;
            jd.collideConnected = false;

            float y = 25.0f;
            b2Body * prevBody = ground;
            for (int i = 0; i < 30; i++) {
                b2BodyDef bd;
                bd.type = b2_dynamicBody;
                bd.position.Set(0.5f + i, y);
                b2Body * body = world.CreateBody(&bd);
                body->CreateFixture(&fd);

                b2Vec2 * anchor = new b2Vec2(i, y);
                jd.Initialize(prevBody, body, *anchor);
                world.CreateJoint(&jd);
                prevBody = body;
            }
            delete shape;
        }

    }
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);
	}
}
// Adds a static box body to the given Box2D world.
void AddStaticBox(b2World& world, b2Vec2 centre, b2Vec2 dimensions) {
	b2BodyDef bdef;
	bdef.type = b2_staticBody;
	bdef.position = centre;
	b2Body* body = world.CreateBody(&bdef);
	b2PolygonShape box;
	box.SetAsBox(dimensions.x, dimensions.y);
	b2FixtureDef fixture_def;
	fixture_def.shape = &box;
	body->CreateFixture(&fixture_def);
}
Exemple #21
0
	void createEntity(tmx::MapObject mapObject, b2World& box2dWorld, anax::World& anaxWorld) {
		b2Vec2 statingPosition = tmx::SfToBoxVec(mapObject.GetPosition());
		b2Vec2 endPosition = tmx::SfToBoxVec(sf::Vector2f(mapObject.GetPosition().x + mapObject.GetAABB().width, mapObject.GetPosition().y));

		b2Body* firstBodyToJoinWith = createStartingBody(statingPosition, box2dWorld);
		b2Body* prevBody = firstBodyToJoinWith;
		{
			b2PolygonShape shape;
			shape.SetAsBox(0.5f, 0.125f);

			b2FixtureDef fd  = getRopeFixture(shape);

			b2RevoluteJointDef jd;
			jd.collideConnected = false;

			const int32 N = ceilf((mapObject.GetAABB().width / 30) / (1.0f )) + 1;

			for (int32 xVal = 1; xVal < N; ++xVal) {
				b2BodyDef bd;
				bd.type = b2_dynamicBody;
				bd.position.Set(statingPosition.x + xVal , statingPosition.y );
				if (xVal == N - 1) {
					bd.type = b2_staticBody;
					shape.SetAsBox(0.1f, 0.1f);
					fd.density = 1.0f;
					bd.position.Set(endPosition.x, endPosition.y);
				}

				b2Body* body = box2dWorld.CreateBody(&bd);
				body->CreateFixture(&fd);

				auto objectEntity = anaxWorld.createEntity();
				auto& texCoordsComp = objectEntity.addComponent<Texcoords>();
				auto& physComp = objectEntity.addComponent<PhysicsComponent>();
				auto& splitDirectionComp = objectEntity.addComponent<SplitDirectionComponent>();
				auto& breakableJointComp = objectEntity.addComponent<BreakableJointComponent>();
				breakableJointComp.maxWeight = 1 ;


				b2Vec2 anchor( statingPosition.x + xVal  , statingPosition.y );
				jd.Initialize(prevBody, body, anchor);
				box2dWorld.CreateJoint(&jd);

				physComp.physicsBody = body;

				objectEntity.activate();
				prevBody = body;
			}

		}



	}
    void createWorld (b2World& world) {

        {
            b2BodyDef bd;
            b2Body * ground = world.CreateBody(&bd);

            b2EdgeShape* shape = new b2EdgeShape();
            shape->Set(b2Vec2(-20.0f, 0), b2Vec2(20.0f, 0.f));
            ground->CreateFixture(shape, 0);
            delete shape;
        }

        {
            b2BodyDef bd;
            bd.position.Set(0, 10);
            b2Body * body = world.CreateBody(&bd);

            b2PolygonShape * shape = new b2PolygonShape();
            shape->SetAsBox(3, 0.5f);
            m_platform = body->CreateFixture(shape, 0);
            m_bottom = 10.0f - 0.5f;
            m_top = 10.0f + 0.5f;
        }

        {
            b2BodyDef bd;
            bd.type = b2_dynamicBody;
            bd.position.Set(0, 12);
            b2Body * body = world.CreateBody(&bd);

            m_radius = 0.5f;
            b2CircleShape * shape = new b2CircleShape();
            shape->m_radius = m_radius;
            m_character = body->CreateFixture(shape, 20.0f);
            delete shape;

            m_state = Unknown;
        }

        world.SetContactFilter(new TestContactFilter(this));
    }
Exemple #23
0
    void createWorld (b2World& world) {
        b2Body * ground;

        {
            b2BodyDef bd;
            ground = world.CreateBody(&bd);
            b2EdgeShape* shape = new b2EdgeShape();
            shape->Set(b2Vec2(-40, 0), b2Vec2(40, 0));
            ground->CreateFixture(shape, 0);
            delete shape;
        }

        {
            b2PolygonShape * shape = new b2PolygonShape();
            shape->SetAsBox(2, 5);

            b2BodyDef bd;
            bd.type = b2_dynamicBody;
            bd.position.Set(-10, 10);
            bd.angle = 0.5f * (float)gdx::detail::PI;
            bd.allowSleep = false;

            b2Body * body = world.CreateBody(&bd);
            body->CreateFixture(shape, 5.0f);

            b2PrismaticJointDef pjd;

            b2Vec2 axis(2, 1);
            axis.Normalize();
            pjd.Initialize(ground, body, b2Vec2(0, 0), axis);

            pjd.motorSpeed = 10.0f;
            pjd.maxMotorForce = 10000.0f;
            pjd.enableMotor = true;
            pjd.lowerTranslation = 0;
            pjd.upperTranslation = 20.0f;
            pjd.enableLimit = true;

            m_joint = (b2PrismaticJoint *)world.CreateJoint(&pjd);
        }
    }
Exemple #24
0
void createWall(float x, float y, float width, float height, b2World& world) {
	b2BodyDef bodyDef;
	bodyDef.position = Convert::worldToBox2d(x, y);
	bodyDef.type = b2_staticBody;
	b2Body* body = world.CreateBody(&bodyDef);
	b2PolygonShape shape;
	shape.SetAsBox(Convert::worldToBox2d(width / 2.f), Convert::worldToBox2d(height / 2.f));
	b2FixtureDef fixdef;
	fixdef.shape = &shape;
	body->CreateFixture(&fixdef);

}
Exemple #25
0
void Entity::construct(b2World& world)
{
	//body def
	b2BodyDef bodyDef;

	bodyDef.type = (b2BodyType)bodyType;

	bodyDef.angle = angle;
	bodyDef.angularDamping = angularDamping;
	bodyDef.angularVelocity = angularVelocity;
	bodyDef.gravityScale = gravityScale;
	
	bodyDef.linearDamping = linearDamping;
	bodyDef.linearVelocity.x = linearVelocityX;
	bodyDef.linearVelocity.y = linearVelocityY;
	bodyDef.position.x = positionX;
	bodyDef.position.y = positionY;
	
	bodyDef.active = isActive;
	bodyDef.awake = isAwake;
	bodyDef.bullet = isBullet;
	bodyDef.fixedRotation = isFixedRotation;
	bodyDef.allowSleep = isSleepingAllowed;

	//body
	// Call the body factory which allocates memory for the ground body
	// from a pool and creates the ground box shape (also from a pool).
	// The body is also added to the world.
	body = world.CreateBody(&bodyDef);

	//fixture def
	b2PolygonShape polygonShape;
	polygonShape.SetAsBox(shapeWidth, shapeHeight);

	b2FixtureDef fixtureDef;
	fixtureDef.shape = &polygonShape;
	fixtureDef.restitution = 0.5f;	// air resistance / fluid resistance
	fixtureDef.density = 1.0f;
	fixtureDef.friction = 1.0f;

	//fixture
	body->CreateFixture(&fixtureDef);
	
	//set mass inertia and center
	b2MassData md;
	md.center = b2Vec2_zero;
	md.I = intertia;
	md.mass = mass;
	body->SetMassData(&md);

	//these cannot be predefined for some reason, seems like a bug in box2d
	body->SetFixedRotation(isFixedRotation);
}
CBox::CBox(b2World & world) {
	m_shape = sf::RectangleShape(sf::Vector2f(20,20));//(0,0,50,50,sf::Color(255,255,255));
	m_shape.SetOrigin(10,10);
	m_shape.SetFillColor(sf::Color(255, 0, 255, 255));
    m_bodyDef.position.Set(300.0f/PPM,-300.0f/PPM-n*5/PPM);
    m_bodyDef.type = b2_dynamicBody;
    m_bodyShape.SetAsBox(10.0f/PPM,10.0f/PPM);
    m_bodyFix.shape = &m_bodyShape;
    m_bodyFix.density = 0.3f;
    m_bodyFix.friction = 0.5f;
    m_body = world.CreateBody(&m_bodyDef);
    m_body->CreateFixture(&m_bodyFix);
}
Exemple #27
0
Bomb::Bomb(b2World& world, float x, float y) : GameObject(world, x, y,"Bomb", new BombDrawable(x,y)) {
	b2BodyDef bodyDef;
	bodyDef.position.Set(x, y);
	b2Body* body_ptr = world.CreateBody(&bodyDef);
	b2PolygonShape boxShape;
	boxShape.SetAsBox(1.3,1.3);
	b2FixtureDef fixtureDef;
	fixtureDef.shape = &boxShape;
	body_ptr->CreateFixture(&fixtureDef);
	exploded = false;
	body_ptr->SetUserData(this);
	bodies.push_back(PhysBody(body_ptr, body_ptr->GetPosition(), body_ptr->GetAngle()));
}
Exemple #28
0
void createGround(b2World& world) {
    b2BodyDef bodyDef;
    bodyDef.position = b2Vec2(40.0f, 18.75f);
    bodyDef.type = b2_staticBody;
    b2Body* body = world.CreateBody(&bodyDef);

    b2PolygonShape shape;
    shape.SetAsBox(40.f, 1.0f);
    b2FixtureDef fixtureDef;
    fixtureDef.density = 0.0f;
    fixtureDef.shape = &shape;
    body->CreateFixture(&fixtureDef);
}
    void createWorld (b2World& world) {
        {
            b2PolygonShape * shape = new b2PolygonShape();
            shape->SetAsEdge(b2Vec2(-40.0f, 0), b2Vec2(40, 0));

            b2FixtureDef fd;
            fd.shape = shape;
            fd.friction = 0.3f;

            b2BodyDef bd;
            b2Body * ground = world.CreateBody(&bd);
            ground->CreateFixture(&fd);
            delete shape;
        }

        {
            b2PolygonShape * shape = new b2PolygonShape();
            shape->SetAsBox(1, 2.f);

            b2BodyDef def;
            def.position.y = 10;
            def.angle = (float)gdx_cpp::math::utils::toRadians(90);
            def.type = b2_dynamicBody;

            b2Body * body = world.CreateBody(&def);
            body->CreateFixture(shape, 1);

            def = b2BodyDef();
            def.position.x = 10;
            def.position.y = 10;
            def.angle = 0;
            def.type = b2_dynamicBody;

            body = world.CreateBody(&def);
            body->CreateFixture(shape, 1);

            delete shape;
        }
    }
Exemple #30
0
Teleport::Teleport(b2World& world, float x, float y, int flipped) : GameObject(world, x, y, "Teleport", new TeleportDrawable(x,y)), flipped(flipped) {

	b2BodyDef bodyDef;
	bodyDef.position.Set(x, y);
	b2Body* body_ptr = world.CreateBody(&bodyDef);
	b2PolygonShape boxShape;
	boxShape.SetAsBox(4,0.2);
	b2FixtureDef fixtureDef;
	fixtureDef.shape = &boxShape;
	fixtureDef.isSensor=true;
	my_check=body_ptr->CreateFixture(&fixtureDef);
	body_ptr->SetUserData(this);
	bodies.push_back(PhysBody(body_ptr, body_ptr->GetPosition(), body_ptr->GetAngle()));

	b2PolygonShape leftShape;
	leftShape.SetAsBox(0.5,1, b2Vec2(-4.5,-0.8),0);
	b2FixtureDef leftDef;
	leftDef.shape= &leftShape;

	b2PolygonShape rightShape;
	rightShape.SetAsBox(0.5,1, b2Vec2(4.5,-0.8),0);
	b2FixtureDef rightDef;
	rightDef.shape= &rightShape;
	body_ptr->CreateFixture(&leftDef);
	body_ptr->CreateFixture(&rightDef);
	
	b2PolygonShape blocker;
	blocker.SetAsBox(5,1.3);
	b2FixtureDef blockerDef;
	blockerDef.shape = &blocker;
	blockerDef.isSensor=true;
	my_check2=body_ptr->CreateFixture(&blockerDef);
	


	teleports.push_back(this);
	//Rotate:
	if (flipped==0) {
		bodies[0].body_ptr->SetTransform(bodies[0].body_ptr->GetPosition(),0);
	}
	if (flipped==3) {
		bodies[0].body_ptr->SetTransform(bodies[0].body_ptr->GetPosition(),3.14159f/2.0f);
	}
	if (flipped==2) {
		bodies[0].body_ptr->SetTransform(bodies[0].body_ptr->GetPosition(),3.14159f);
	}
	if (flipped==1) {
		bodies[0].body_ptr->SetTransform(bodies[0].body_ptr->GetPosition(),3.0f*3.14159f/2.0f);
	}
	bodies[0].original_rot=bodies[0].body_ptr->GetAngle();
}