bool maybeCreateJoint( b2World& world, Stroke* other ) { if ( (m_attributes&ATTRIB_CLASSBITS) != (other->m_attributes&ATTRIB_CLASSBITS) ) { return false; // can only joint matching classes } else if ( hasAttribute(ATTRIB_GROUND) ) { return true; // no point jointing grounds } else if ( m_body && other->body() ) { transform(); int n = m_xformedPath.numPoints(); for ( int end=0; end<2; end++ ) { if ( !m_jointed[end] ) { const Vec2& p = m_xformedPath.point( end ? n-1 : 0 ); if ( other->distanceTo( p ) <= JOINT_TOLERANCE ) { //printf("jointed end %d d=%f\n",end,other->distanceTo( p )); b2Vec2 pw = p; pw *= 1.0f/PIXELS_PER_METREf; JointDef j( m_body, other->m_body, pw ); world.CreateJoint( &j ); m_jointed[end] = true; } } } } if ( m_body ) { return m_jointed[0] && m_jointed[1]; } return true; ///nothing to do }
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); }
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; } } }
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); }
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); }
Rope::Rope(b2World& aWorld, float aX, float aY) : myWorld(aWorld) { b2Body* lastBody; for (int i = 0; i < 10; i++) { float ROPE_LENGTH = 0.2; float ROPE_WIDTH = 0.01; b2BodyDef dropboxDef2; dropboxDef2.position = b2Vec2(aX, aY + i * ROPE_LENGTH); dropboxDef2.linearDamping = 0.8f; //dropboxDef.angle = 0.2; if (i != 0) { dropboxDef2.type = b2_dynamicBody; } b2PolygonShape dropboxShape2; dropboxShape2.SetAsBox(ROPE_WIDTH / 2, ROPE_LENGTH / 2); b2Body* body2 = aWorld.CreateBody(&dropboxDef2); body2->CreateFixture(&dropboxShape2, 0.05f)->SetUserData( (UserData*) this); if (i != 0) { b2RevoluteJointDef jointDef; b2Vec2 anchor = (lastBody->GetPosition() + body2->GetPosition()); anchor *= 0.5; jointDef.Initialize(lastBody, body2, anchor); jointDef.collideConnected = false; jointDef.enableLimit = true; jointDef.upperAngle = 0.8; jointDef.lowerAngle = -0.8; b2RevoluteJoint* joint = (b2RevoluteJoint*) aWorld.CreateJoint( &jointDef); myJoints.push_back(joint); } myParts.push_back(body2); lastBody = body2; } mySprite.LoadTGA("data/rope.tga"); }
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); }
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 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); } }
void createWorld (b2World& world) { world.SetGravity(b2Vec2(0, 0)); float k_restitution = 0.4f; b2Body * ground; { b2BodyDef bd; bd.position.Set(0, 20); ground = world.CreateBody(&bd); b2PolygonShape * shape = new b2PolygonShape; b2FixtureDef sd; sd.shape = shape; sd.density = 0; sd.restitution = k_restitution; shape->SetAsEdge(b2Vec2(-20, -20), b2Vec2(-20, 20)); ground->CreateFixture(&sd); shape->SetAsEdge(b2Vec2(20, -20), b2Vec2(20, 20)); ground->CreateFixture(&sd); shape->SetAsEdge(b2Vec2(-20, 20), b2Vec2(20, 20)); ground->CreateFixture(&sd); shape->SetAsEdge(b2Vec2(-20, -20), b2Vec2(20, -20)); ground->CreateFixture(&sd); delete shape; } { b2Transform xf1; xf1.Set(b2Vec2(0,0), 0.3524f * (float)gdx_cpp::math::utils::detail::PI); xf1.Set(b2Mul(xf1, b2Vec2(1, 0)), xf1.GetAngle()); b2Vec2 vertices[3]; vertices[0] = b2Mul(xf1, b2Vec2(-1, 0)); vertices[1] = b2Mul(xf1, b2Vec2(1, 0)); vertices[2] = b2Mul(xf1, b2Vec2(0, 0.5f)); b2PolygonShape * poly1 = new b2PolygonShape(); poly1->Set(&vertices[0] , 3); b2FixtureDef sd1; sd1.shape = poly1; sd1.density = 4.0f; b2Transform xf2; xf2.Set(b2Vec2(), -0.3524f * (float)gdx_cpp::math::utils::detail::PI); xf2.Set(b2Mul(xf2, b2Vec2(-1, 0)), xf2.GetAngle()); vertices[0] = b2Mul(xf2, b2Vec2(-1, 0)); vertices[1] = b2Mul(xf2, b2Vec2(1, 0)); vertices[2] = b2Mul(xf2, b2Vec2(0, 0.5f)); b2PolygonShape * poly2 = new b2PolygonShape(); poly2->Set(&vertices[0], 3); b2FixtureDef sd2; sd2.shape = poly2; sd2.density = 2.0f; b2BodyDef bd; bd.type = b2_dynamicBody; bd.angularDamping = 5.0f; bd.linearDamping = 0.1f; bd.position.Set(0, 2); bd.angle = (float)gdx_cpp::math::utils::detail::PI; bd.allowSleep = false; m_body = world.CreateBody(&bd); m_body->CreateFixture(&sd1); m_body->CreateFixture(&sd2); delete poly1; delete poly2; } { 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 < 10; i++) { b2BodyDef bd; bd.type = b2_dynamicBody; bd.position.Set(0, 5 + 1.54f * i); b2Body * body = world.CreateBody(&bd); body->CreateFixture(&fd); float gravity = 10.0f; float I = body->GetInertia(); float mass = body->GetMass(); float radius = (float)std::sqrt(2 * I / mass); b2FrictionJointDef jd; jd.localAnchorA.Set(0, 0); jd.localAnchorB.Set(0, 0); jd.bodyA = ground; jd.bodyB = body; jd.collideConnected = true; jd.maxForce = mass * gravity; jd.maxTorque = mass * radius * gravity; world.CreateJoint(&jd); } delete shape; } }
void createWorld (b2World& world) { { b2EdgeShape* shape = new b2EdgeShape(); shape->Set(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; } b2Vec2 vertices[3]; vertices[0].Set(-1, 0); vertices[1].Set(1, 0); vertices[2].Set(0, 2); b2PolygonShape * polygon = new b2PolygonShape; polygon->Set(vertices, 3); b2FixtureDef triangleShapeDef; triangleShapeDef.shape = polygon; triangleShapeDef.density = 1.0f; triangleShapeDef.filter.groupIndex = k_smallGroup; triangleShapeDef.filter.categoryBits = k_triangleCategory; triangleShapeDef.filter.maskBits = k_triangleMask; b2BodyDef triangleBodyDef; triangleBodyDef.type = b2_dynamicBody; triangleBodyDef.position.Set(-5, 2); b2Body * body1 = world.CreateBody(&triangleBodyDef); body1->CreateFixture(&triangleShapeDef); vertices[0].x *= 2; vertices[0].y *= 2; vertices[1].x *= 2; vertices[1].y *= 2; vertices[2].x *= 2; vertices[2].y *= 2; polygon->Set(vertices, 3); triangleShapeDef.filter.groupIndex = k_largeGroup; triangleBodyDef.position.Set(-5, 6); triangleBodyDef.fixedRotation = true; b2Body * body2 = world.CreateBody(&triangleBodyDef); body2->CreateFixture(&triangleShapeDef); { b2BodyDef bd; bd.type = b2_dynamicBody; bd.position.Set(-5, 10); b2Body * body = world.CreateBody(&bd); b2PolygonShape * p = new b2PolygonShape; p->SetAsBox(0.5f, 1.0f); body->CreateFixture(p, 1); b2PrismaticJointDef jd; jd.bodyA = body2; jd.bodyB = body; jd.enableLimit = true; jd.localAnchorA.Set(0, 4); jd.localAnchorB.Set(0, 0); jd.localAxisA.Set(0, 1); jd.lowerTranslation = -1; jd.upperTranslation = 1; world.CreateJoint(&jd); delete p; } polygon->SetAsBox(1, 0.5f); b2FixtureDef boxShapeDef; boxShapeDef.shape = polygon; boxShapeDef.density = 1; boxShapeDef.restitution = 0.1f; boxShapeDef.filter.groupIndex = k_smallGroup; boxShapeDef.filter.categoryBits = k_boxCategory; boxShapeDef.filter.maskBits = k_boxMask; b2BodyDef boxBodyDef; boxBodyDef.type = b2_dynamicBody; boxBodyDef.position.Set(0, 2); b2Body * body3 = world.CreateBody(&boxBodyDef); body3->CreateFixture(&boxShapeDef); polygon->SetAsBox(2, 1); boxShapeDef.filter.groupIndex = k_largeGroup; boxBodyDef.position.Set(0, 6); b2Body * body4 = world.CreateBody(&boxBodyDef); body4->CreateFixture(&boxShapeDef); b2CircleShape * circle = new b2CircleShape; circle->m_radius = 1; b2FixtureDef circleShapeDef; circleShapeDef.shape = circle; circleShapeDef.density = 1.0f; circleShapeDef.filter.groupIndex = k_smallGroup; circleShapeDef.filter.categoryBits = k_circleCategory; circleShapeDef.filter.maskBits = k_circleMask; b2BodyDef circleBodyDef; circleBodyDef.type = b2_dynamicBody; circleBodyDef.position.Set(5, 2); b2Body * body5 = world.CreateBody(&circleBodyDef); body5->CreateFixture(&circleShapeDef); circle->m_radius = 2; circleShapeDef.filter.groupIndex = k_largeGroup; circleBodyDef.position.Set(5, 6); b2Body * body6 = world.CreateBody(&circleBodyDef); body6->CreateFixture(&circleShapeDef); }
void createWorld (b2World& world) { b2Body * ground; { b2BodyDef bd; ground = world.CreateBody(&bd); b2EdgeShape * shape = new b2EdgeShape; shape->Set(b2Vec2(-40, 0), b2Vec2(40.0f, 0)); ground->CreateFixture(shape, 0); delete shape; } { b2PolygonShape * shape = new b2PolygonShape; shape->SetAsBox(0.5f, 0.125f); b2FixtureDef fd ; fd.shape = shape; fd.density = 20.0f; fd.friction = 0.2f; b2RevoluteJointDef jd; b2Body * prevBody = ground; for (int i = 0; i < e_count; i++) { b2BodyDef bd; bd.type = b2_dynamicBody; bd.position.Set(-14.5f + 1.0f * i, 5.0f); b2Body * body = world.CreateBody(&bd); body->CreateFixture(&fd); b2Vec2 anchor(-15.0f + 1.0f * i, 5.0f); jd.Initialize(prevBody, body, anchor); world.CreateJoint(&jd); prevBody = body; } b2Vec2 anchor(-15.0f + 1.0f * e_count, 5.0f); jd.Initialize(prevBody, ground, anchor); world.CreateJoint(&jd); delete shape; } for (int i = 0; i < 2; i++) { b2Vec2 vertices[3]; vertices[0].Set(-0.5f, 0); vertices[1].Set(0.5f, 0); vertices[2].Set(0, 1.5f); b2PolygonShape * shape = new b2PolygonShape(); shape->Set(&vertices[0], 3); b2FixtureDef fd; fd.shape = shape; fd.density = 1.0f; b2BodyDef bd; bd.type = b2_dynamicBody; bd.position.Set(-8.0f + 8.0f * i, 12.0f); b2Body * body = world.CreateBody(&bd); body->CreateFixture(&fd); delete shape; } for (int i = 0; i < 3; i++) { b2CircleShape * shape = new b2CircleShape(); shape->m_radius = 0.5f; b2FixtureDef fd; fd.shape = shape; fd.density = 1.0f; b2BodyDef bd; bd.type = b2_dynamicBody; bd.position.Set(-6.0f + 6.0f * i, 10.0f); b2Body * body = world.CreateBody(&bd); body->CreateFixture(&fd); delete shape; } }