本文整理汇总了C++中b2World类的典型用法代码示例。如果您正苦于以下问题:C++ b2World类的具体用法?C++ b2World怎么用?C++ b2World使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了b2World类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: createWorld
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;
}
}
示例2: createWorld
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;
}
}
示例3: createWorld
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;
}
}
示例4: gravity
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);
}
示例5: addStaticShapes
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);
}
示例6: step
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();
}
示例7: processRemoval
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);
}
}
示例8: createEntity
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;
}
}
}
示例9: if
void Box2DDebugRenderer::renderBodies ( b2World& world) {
renderer.begin(ShapeRenderer::ShapeType::Line);
if (mDrawBodies || mDrawAABBs) {
for (b2Body * body = world.GetBodyList(); body; body = body->GetNext()) {
const b2Transform& transform = body->GetTransform();
for (b2Fixture* fixture = body->GetFixtureList(); fixture; fixture = fixture->GetNext()) {
if (mDrawBodies) {
if (body->IsActive() == false)
drawShape(*fixture, transform, SHAPE_NOT_ACTIVE);
else if (body->GetType() == b2_staticBody)
drawShape(*fixture, transform, SHAPE_STATIC);
else if (body->GetType() == b2_kinematicBody)
drawShape(*fixture, transform, SHAPE_KINEMATIC);
else if (body->IsAwake() == false)
drawShape(*fixture, transform, SHAPE_NOT_AWAKE);
else
drawShape(*fixture, transform, SHAPE_AWAKE);
}
if (mDrawAABBs) {
drawAABB(*fixture, transform);
}
}
}
}
if (mDrawJoints) {
for (b2Joint* joint = world.GetJointList(); joint; joint= joint->GetNext())
{
drawJoint(*joint);
}
}
renderer.end();
if (gl10 != NULL) {
gl10->glPointSize(3);
}
renderer.begin(ShapeRenderer::ShapeType::Point);
for (b2Contact* contact = world.GetContactList(); contact; contact = contact->GetNext()) {
drawContact(*contact);
}
renderer.end();
if (gl10 != NULL) {
gl10->glPointSize(1);
}
}
示例10: GameObject
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;
}
}
示例11:
void Box2DRenderer::render (Graphics& g, b2World& world,
float left, float top, float right, float bottom,
const Rectangle<float>& target)
{
graphics = &g;
g.addTransform (AffineTransform::fromTargetPoints (left, top, target.getX(), target.getY(),
right, top, target.getRight(), target.getY(),
left, bottom, target.getX(), target.getBottom()));
world.SetDebugDraw (this);
world.DrawDebugData();
}
示例12: create
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);
}
示例13: Create
void RevoluteJoint::Create(b2World &world,
const BasicShape &joinedBody1,
const BasicShape &joinedBody2,
const b2Vec2 &anchor1,
const b2Vec2 &anchor2,
const bool collideConnected,
const float lowerAngle,
const float upperAngle,
const bool enableLimit,
const float maxMotorTorque,
const float motorSpeed,
const bool enableMotor,
const float referenceAngle)
{
b_ptrJoinedBody1 = const_cast<b2Body*>(joinedBody1.GetBody());
b_ptrJoinedBody2 = const_cast<b2Body*>(joinedBody2.GetBody());
//b2RevoluteJointDef revoluteJointDefinition;
_revoluteJointDefinition.bodyA = b_ptrJoinedBody1;
_revoluteJointDefinition.bodyB = b_ptrJoinedBody2;
_revoluteJointDefinition.localAnchorA = anchor1;
_revoluteJointDefinition.localAnchorB = anchor2;
_revoluteJointDefinition.collideConnected = collideConnected;
_revoluteJointDefinition.lowerAngle = lowerAngle;
_revoluteJointDefinition.upperAngle = upperAngle;
_revoluteJointDefinition.enableLimit = enableLimit;
_revoluteJointDefinition.maxMotorTorque = maxMotorTorque;
_revoluteJointDefinition.motorSpeed = motorSpeed;
_revoluteJointDefinition.enableMotor = enableMotor;
_revoluteJointDefinition.referenceAngle = referenceAngle;
b_ptrJoint = world.CreateJoint(&_revoluteJointDefinition);
}
示例14: Shape
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();
}
示例15: display_world
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();
}