bool keyDown (int keyCode) { if (keyCode == gdx_cpp::Input::Keys::COMMA) { if (m_bullet != NULL) { world->DestroyBody(m_bullet); m_bullet = NULL; } { b2CircleShape * shape = new b2CircleShape(); shape->m_radius = 0.25f; b2FixtureDef fd; fd.shape = shape; fd.density = 20.0f; fd.restitution = 0.05f; b2BodyDef bd; bd.type = b2_dynamicBody; bd.bullet = true; bd.position.Set(-31, 5); m_bullet = world->CreateBody(&bd); m_bullet->CreateFixture(&fd); m_bullet->SetLinearVelocity(b2Vec2(400, 0)); } } return false; }
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; } }
//constructor Car(b2World* world){ b2BodyDef bodyDef; bodyDef.type = b2_dynamicBody; w_body = world->CreateBody(&bodyDef); b2PolygonShape polygonShape; polygonShape.SetAsBox(0.5f, 1.25f); b2Fixture* fixture = w_body->CreateFixture(&polygonShape, 1);//shape, density fixture->SetUserData(new CarTireFUD()); w_body->SetUserData(this); currentTraction = 1; }
// Create a horizontal box fixture attached to the ground body. b2Fixture* BodyContactTests::CreateGroundBox() { b2Assert(m_groundBody); b2PolygonShape shape; shape.SetAsBox(10.0f * m_particleDiameter, m_particleDiameter); return m_groundBody->CreateFixture(&shape, 0.0f); }
Pelota(b2World* _world ,RenderWindow *_wnd){ //guardamos puntero a ventana para dibujar luego wnd=_wnd; _image= new Image(); _image->LoadFromFile("pelota.jpg"); //cargamos el sprite _sprite= new Sprite(*_image); //definimos el body y lo creamos b2BodyDef bodyDef; bodyDef.type = b2_dynamicBody; bodyDef.position.Set(100.0f, 0.0f); _body = _world->CreateBody(&bodyDef); //creamos su figura de colisiĆ³n //en este caso suponemos que la figura de //colision es una caja cuadrada b2PolygonShape dynamicBox; dynamicBox.SetAsBox(20.0f, 20.0f); //creamos el fixture, le seteamos //la figura de colision //y agregamos el fixture al body b2FixtureDef fixtureDef; fixtureDef.shape = &dynamicBox; fixtureDef.density = 10.0f; fixtureDef.friction = 0.3f; fixtureDef.restitution=1.0f; _body->CreateFixture(&fixtureDef); }
Value CreateFixture(b2Body &body, b2Shape &shape) { auto fixture = body.CreateFixture(&shape, 1.0f); auto r = new Renderable("color"); fixture->SetUserData(r); return Value((int)fixtures->Add(fixture)); }
void Box2dContainer::assignNode(b2Body& body, cocos2d::Node& node) { if (!nodeToId.count(&node)) { auto newId = generateId(); nodeToId[&node] = newId; idToNode[newId] = &node; } body.SetUserData(static_cast<void*>(nodeToId[&node])); }
Ball(b2World* world, float radius) { m_body = NULL; m_radius = radius; b2BodyDef myBodyDef; myBodyDef.type = b2_dynamicBody; myBodyDef.position.Set(0, 20); m_body = world->CreateBody(&myBodyDef); m_contacting = false; //add circle fixture b2CircleShape circleShape; circleShape.m_p.Set(0, 0); circleShape.m_radius = m_radius; //use class variable b2FixtureDef myFixtureDef; myFixtureDef.shape = &circleShape; myFixtureDef.density = 1; m_body->CreateFixture(&myFixtureDef); m_body->SetUserData( this ); }
// Create a valley (similar to the Anti-Pointy test in the Testbed) and // attach to the ground body. void BodyContactTests::CreateValley() { b2Assert(m_groundBody); float32 i; const float32 step = 1.0f; for (i = -10.0f; i < 10.0f; i+=step) { b2PolygonShape shape; const b2Vec2 vertices[] = { b2Vec2(i, -10.0f), b2Vec2(i+step, -10.0f), b2Vec2(0.0f, 15.0f) }; shape.Set(vertices, B2_ARRAY_SIZE(vertices)); m_groundBody->CreateFixture(&shape, 0.0f); } for (i = -10.0f; i < 35.0f; i+=step) { b2PolygonShape shape; const b2Vec2 vertices[] = { b2Vec2(-10.0f, i), b2Vec2(-10.0f, i+step), b2Vec2(0.0f, 15.0f) }; shape.Set(vertices, B2_ARRAY_SIZE(vertices)); m_groundBody->CreateFixture(&shape, 0.0f); const b2Vec2 vertices2[] = { b2Vec2(10.0f, i), b2Vec2(10.0f, i+step), b2Vec2(0.0f, 15.0f) }; shape.Set(vertices2, B2_ARRAY_SIZE(vertices2)); m_groundBody->CreateFixture(&shape, 0.0f); } }
block(float w,float h,float x,float y,int a, b2World* m_world,int s_d=0, float friction =1.0f,float density=1.0f) { b2PolygonShape shape; shape.SetAsBox(w, h); bd.angle = a; //set the starting angle bd.position.Set(x, y); if(s_d==1)bd.type = b2_dynamicBody; if(s_d==0)bd.type = b2_staticBody; body = m_world->CreateBody(&bd); b2FixtureDef *fd = new b2FixtureDef; fd->density = density; fd->shape = new b2PolygonShape; fd->shape = &shape; fd->friction = friction; fd->restitution = 0.0f; body->CreateFixture(fd); }
void updateDrive(int controlState) { //find desired speed float desiredSpeed = 0; switch (controlState & (TDC_UP | TDC_DOWN)) { case TDC_UP: desiredSpeed = maxForwardSpeed; break; case TDC_DOWN: desiredSpeed = maxBackwardSpeed; break; default: return;//do nothing } //find current speed in forward direction b2Vec2 currentForwardNormal = w_body->GetWorldVector(b2Vec2(0, 1)); float currentSpeed = b2Dot(getForwardVelocity(), currentForwardNormal); //apply necessary force float force = 0; if (desiredSpeed > currentSpeed) force = maxDriveForce; else if (desiredSpeed < currentSpeed) force = -maxDriveForce; else return; //w_body->ApplyForce(currentTraction * force * currentForwardNormal, w_body->GetWorldCenter()); }
b2Vec2 getLateralVelocity() { b2Vec2 currentRightNormal = w_body->GetWorldVector(b2Vec2(1, 0)); return b2Dot(currentRightNormal, w_body->GetLinearVelocity()) * currentRightNormal; }
//metodo que posiciona el sprites //segun la posicion del body void ActualizarPosiciones(){ b2Vec2 pos=_body->GetPosition(); _sprite->SetPosition(pos.x,pos.y); }
//destructor ~Car(){ w_body->GetWorld()->DestroyBody(w_body); }
/// Apply a force to the center of the rigid body virtual void applyForceToCenter(Vector2D force) { _body->ApplyLinearImpulse(b2Vec2(force.x, force.y), _body->GetLocalCenter()); }
Value CreateFixture(VM &vm, b2Body &body, b2Shape &shape) { auto fixture = body.CreateFixture(&shape, 1.0f); auto po = new PhysicsObject(Renderable("color"), fixture); fixture->SetUserData(po); return Value(vm.NewResource(po, &physics_type)); }
/// Apply a angular impulse to the object (2D) virtual void applyAngularImpulse(float impulse2d) { _body->ApplyAngularImpulse(impulse2d); }
void launch () { m_body->SetTransform(b2Vec2(0, 20), 0); m_angularVelocity = (float)gdx_cpp::math::utils::random() * 100 - 50; m_body->SetLinearVelocity(b2Vec2(0, -100)); m_body->SetAngularVelocity(m_angularVelocity); }
b2Fixture * operator()(const CollisionPolygonDef & aDef) const { b2Fixture *fixture = mBody->CreateFixture(&aDef.mFixture); fixture->SetUserData(mBody->GetUserData()); return fixture; }
sf::FloatRect geometry() { sf::Vector2f bod_pos = vHelper::toSF(m_box_body->GetPosition()); sf::Vector2f position(bod_pos.x - (m_size.x / 2), bod_pos.y - (m_size.y / 2)); return sf::FloatRect{ position.x, position.y, (float)m_size.x, (float)m_size.y }; }
virtual float getRotation2D() { return _body->GetAngle(); }
virtual void setRotation(float angle) { _body->SetTransform(_body->GetPosition(), angle); }
const maths::vec2& getPosition() override { OSprite::setPosition(maths::vec2(m_Body->GetPosition().x * O_PTM_RATIO, m_Body->GetPosition().y * O_PTM_RATIO)); return m_Position; }
const b2Vec2 Box2dService::retrieveTopLeftVertex(const b2Body &objectBody) { return objectBody.GetWorldPoint(dynamic_cast<const b2PolygonShape*>(findNonSensorFixture(*objectBody.GetFixtureList()).GetShape())->GetVertex(3)); }
b2Vec2 getForwardVelocity() { b2Vec2 currentForwardNormal = w_body->GetWorldVector(b2Vec2(0, 1)); return b2Dot(currentForwardNormal, w_body->GetLinearVelocity()) * currentForwardNormal; }
/// Check if the rigid body is sleeping, if supported virtual bool isAwake() { return _body->IsAwake(); }
/// Enable or disable rigid body rotation virtual void setFixedRotation(bool enable) { _body->SetFixedRotation(enable); }
void setPosition(const maths::vec2& position) override { if (m_Body) m_Body->SetTransform(b2Vec2(position.x / O_PTM_RATIO, position.y / O_PTM_RATIO), 0); }
const b2Vec2 Box2dService::retrieveTopRightVertex(const b2Body &objectBody, const b2Fixture &fixture) { return objectBody.GetWorldPoint(dynamic_cast<const b2PolygonShape*>(fixture.GetShape())->GetVertex(2)); }