int main(){ wrldmgr.res.loadTexture("art/ChestBlue.png"); wrldmgr.res.loadTexture("art/Coin.png"); wrldmgr.res.loadTexture("art/Gem.png"); ///////////////////////////////////////////////////////////////////// bool doSleep = true; world.SetAllowSleeping(doSleep); controlled obj1;{ b2BodyDef bodyDef; //bodyDef.fixedRotation = true; bodyDef.type = b2_dynamicBody; bodyDef.position.Set(300.0f/SCALE, 15.f/SCALE); b2PolygonShape dynamicBox; dynamicBox.SetAsBox((40.f/2)/SCALE, (40.f/2)/SCALE); b2FixtureDef fixtureDef; fixtureDef.shape = &dynamicBox; fixtureDef.density = 1.0f; fixtureDef.friction = 0.3f; fixtureDef.restitution = 0.4f; obj1.animations.push_back(animation(&wrldmgr.res.textures[0], sf::Vector2i(100,100), 2, 300));//wrldmgr.res.textures[0].getSize().x, wrldmgr.res.textures[0].getSize().y))); obj1.animations.push_back(animation(&wrldmgr.res.textures[2], sf::Vector2i(50,50))); obj1.animations[1].pos = sf::Vector2f(0,400); obj1.bd = world.CreateBody(&bodyDef); obj1.fx = obj1.bd->CreateFixture(&fixtureDef); obj1.refresh(); } wrldmgr.playerObj = obj1; object obj2;{ b2BodyDef bbd; bbd.position.Set(400.f/SCALE,30.f/SCALE); bbd.type = b2_dynamicBody; b2CircleShape ballshape; ballshape.m_radius = 50.f/SCALE; b2FixtureDef ballFixtureDef; ballFixtureDef.shape = &ballshape; ballFixtureDef.density = 1.0f; ballFixtureDef.friction = 0.3f; ballFixtureDef.restitution = 0.4f; obj2.animations.push_back(animation(&wrldmgr.res.textures[1], sf::Vector2i(100,100)));//wrldmgr.res.textures[1].getSize().x, wrldmgr.res.textures[1].getSize().y))); obj2.bd = world.CreateBody(&bbd); obj2.fx = obj2.bd->CreateFixture(&ballFixtureDef); obj2.refresh(); } wrldmgr.addObject(obj2); b2Body* ground;{ b2BodyDef bd; bd.position.Set(0.0f/SCALE, 0.0f/SCALE); ground = world.CreateBody(&bd); b2EdgeShape shape; b2FixtureDef sd; sd.shape = &shape; sd.density = 0.0f; sd.restitution = 0.4f; // Left vertical shape.Set(b2Vec2(10.0f/SCALE, 10.0f/SCALE), b2Vec2(10.0f/SCALE, (winh - 10.0f)/SCALE)); ground->CreateFixture(&sd); // Right vertical shape.Set(b2Vec2((winw - 10.0f)/SCALE, 10.0f/SCALE), b2Vec2((winw - 10.0f)/SCALE, (winh - 10.0f)/SCALE)); ground->CreateFixture(&sd); // Top horizontal shape.Set(b2Vec2(10.0f/SCALE, (winh - 10.0f)/SCALE), b2Vec2((winw - 10.0f)/SCALE, (winh - 10.0f)/SCALE)); ground->CreateFixture(&sd); // Bottom horizontal shape.Set(b2Vec2(10.0f/SCALE, 10.0f/SCALE), b2Vec2((winw - 10.0f)/SCALE, 10.0f/SCALE)); ground->CreateFixture(&sd); } ///////////////////////////////////////////// wrldmgr.initialize(); font.loadFromFile("art/Grundschrift-Normal.otf"); text.push_back(sf::Text("Hello!",font,20)); text.push_back(sf::Text("",font,15)); text.push_back(sf::Text("",font,15)); text.push_back(sf::Text("",font,15)); while (win.isOpen()){ while (win.pollEvent(event)){ if (event.type == sf::Event::Closed) win.close(); } if (sf::Keyboard::isKeyPressed(sf::Keyboard::Escape)){ win.close(); } float dt = gameclock.getElapsedTime().asSeconds(); gameclock.restart(); pupd(dt); text[1].setString("UPS: ");// + std::to_string(int(1/dt))); text[2].setString("Player Speed: ");// + std::to_string(wrldmgr.playerObj.bd->GetLinearVelocity().Length())); text[3].setString("Player Angular Speed: ");// + std::to_string(wrldmgr.playerObj.bd->GetAngularVelocity())); renderEdge(ground); oupd(); } return 0; }
dominos_t::dominos_t() { b2Body* b1; //!b1 is b2Body type variable representing the body of ground\n { b2EdgeShape ground_shape; // this is for shape of ground ground_shape.Set(b2Vec2(-50.0f, -8.0f), b2Vec2(90.0f, -8.0f)); b2BodyDef ground_body_def; // body definition of ground b1 = m_world->CreateBody(&ground_body_def); b1->CreateFixture(&ground_shape, 0.0f); } { b2BodyDef *pulley_system_common_body_def = new b2BodyDef; pulley_system_common_body_def->type = b2_dynamicBody; //! this is the common b2BodyDef variable used for various elements of pulley system. float y=15, b=2, a=2; b2PolygonShape pulley_shape; //pulley_shape is pulley shape variable used for two square boxes\n pulley_shape.SetAsBox(a, b); b2FixtureDef *pulley_fixture_def = new b2FixtureDef; // this is used to define the fixture for pulley shape variable used for two square boxes pulley_fixture_def->density = 20.0f; pulley_system_common_body_def->position.Set(70.0f, y); b2Body* pulley_body_1 = m_world->CreateBody(pulley_system_common_body_def); pulley_body_1->CreateFixture(&pulley_shape, 0.5f); // body of square box 1 pulley_system_common_body_def->position.Set(80.0f, y); b2Body* pulley_body_2 = m_world->CreateBody(pulley_system_common_body_def); pulley_body_2->CreateFixture(&pulley_shape, 0.5f); // body of square box 2 b2PulleyJointDef pulley_joint; //! pulley_joint is b2PulleyJointDef type variable used as definition of joint of pulley b2Vec2 anchor1(70.0f, y + b); b2Vec2 anchor2(80.0f, y + b); b2Vec2 groundAnchor1(70.0f, y +30); b2Vec2 groundAnchor2(80.0f, y +30); pulley_joint.Initialize(pulley_body_1, pulley_body_2, groundAnchor1, groundAnchor2, anchor1, anchor2, 1.5f); m_world->CreateJoint(&pulley_joint); } /** BLOCK 9 : BALL ON THE T SHAPE PLATFORM */ { b2Body* sphere_body_common; //! sphere_body_common is b2Body shape variable used for all the four spheres b2CircleShape circle; // for the two smallest ball circle.m_radius = 1.0; b2CircleShape circle1; // for the medium sized ball circle1.m_radius = 1.5; b2CircleShape circle2; // for the largest ball circle2.m_radius = 2.0; b2FixtureDef ballfd; // for the two smallest ball ballfd.shape = &circle; ballfd.density = 10.0f; ballfd.friction = 0.2f; ballfd.restitution = 0.0f; b2FixtureDef ballfd1; // for the medium sized ball circle ballfd1.shape = &circle1; ballfd1.density = 10.0f; ballfd1.friction = 0.2f; ballfd1.restitution = 0.0f; b2FixtureDef ballfd2; // for the largest ball ballfd2.shape = &circle2; ballfd2.density = 10.0f; ballfd2.friction = 0.2f; ballfd2.restitution = 0.0f; b2BodyDef ball_common_body_def; //!ball_common_body_def is b2BodyDef type common body definition for all four balls ball_common_body_def.type = b2_dynamicBody; ball_common_body_def.position.Set(-41.9f, 41.4f); sphere_body_common = m_world->CreateBody(&ball_common_body_def); sphere_body_common->CreateFixture(&ballfd); ball_common_body_def.position.Set(-39.5f, 41.4f); sphere_body_common = m_world->CreateBody(&ball_common_body_def); sphere_body_common->CreateFixture(&ballfd); ball_common_body_def.position.Set(-36.8f, 41.9f); sphere_body_common = m_world->CreateBody(&ball_common_body_def); sphere_body_common->CreateFixture(&ballfd1); ball_common_body_def.position.Set(-33.f, 42.9f); sphere_body_common = m_world->CreateBody(&ball_common_body_def); sphere_body_common->CreateFixture(&ballfd2); } { b2BodyDef killer_body_def; //! killer_body_def is b2BodyDef type variable for killer's body definition killer_body_def.position.Set(-58.f, 25.5f); b2Body* killer_body = m_world->CreateBody(&killer_body_def); // b2Body* type variable for killer's body b2PolygonShape killer_mid_body_shape; // b2PolygonShape type variable for killer's mid_body shape killer_mid_body_shape.SetAsBox(2.f, 2.5f, b2Vec2(0,-3.5),0); b2FixtureDef *killer_mid_body_fixture = new b2FixtureDef; // b2FixtureDef * type variable for killer's mid_body fixture killer_mid_body_fixture->shape = &killer_mid_body_shape; b2CircleShape killer_face_shape; // b2CircleShape type variable for killer's face shape killer_face_shape.m_radius = 1.0; b2FixtureDef *killer_face_fixture = new b2FixtureDef; // b2FixtureDef * type variable for killer's face fixture killer_face_fixture->shape = &killer_face_shape; b2PolygonShape killer_left_leg_shape; // b2PolygonShape type variable for killer's left leg shape killer_left_leg_shape.SetAsBox(0.3f, 2.f, b2Vec2(-1.5,-7),0); b2FixtureDef *killer_left_leg_fixture = new b2FixtureDef; // b2FixtureDef * type variable for killer's left_leg fixture killer_left_leg_fixture->shape = &killer_left_leg_shape; b2PolygonShape killer_right_leg_shape; // b2PolygonShape type variable for killer's right_leg shape killer_right_leg_shape.SetAsBox(0.3f, 2.f, b2Vec2(1.5,-7),0); b2FixtureDef *killer_right_leg_fixture = new b2FixtureDef; // b2FixtureDef type variable for killer's right_leg fixture killer_right_leg_fixture->shape = &killer_right_leg_shape; b2PolygonShape killer_right_hand_shape; // b2PolygonShape type variable for killer's right_hand shape killer_right_hand_shape.SetAsBox(0.3f, 2.f, b2Vec2(2.5,-3),0.3); b2FixtureDef *killer_right_hand_fixture = new b2FixtureDef; // b2FixtureDef type variable for killer's right_hand fixture killer_right_hand_fixture->shape = &killer_right_hand_shape; b2PolygonShape killer_left_hand_shape; //b2PolygonShape type variable for killer's left_hand shape killer_left_hand_shape.SetAsBox(0.3f, 2.f, b2Vec2(-3.8,-1),-1.6); b2FixtureDef *killer_left_hand_fixture = new b2FixtureDef; // type variable for killer_left_hand_fixture->shape = &killer_left_hand_shape; killer_body->CreateFixture(killer_mid_body_fixture); killer_body->CreateFixture(killer_face_fixture); killer_body->CreateFixture(killer_left_leg_fixture); killer_body->CreateFixture(killer_right_leg_fixture); killer_body->CreateFixture(killer_right_hand_fixture); killer_body->CreateFixture(killer_left_hand_fixture); } { float x=0; b2BodyDef human_body_def; //!human_body_def is b2BodyDef type variable for human's body definition human_body_def.position.Set(-80.f, 25.5f); b2Body* human_body = m_world->CreateBody(&human_body_def); //b2Body* type variable for human's body b2PolygonShape human_mid_body_shape; // b2PolygonShape type variable for human's face human_mid_body_shape.SetAsBox(1.6f, 2.5f, b2Vec2(0-x,-3.5),0); b2FixtureDef *human_mid_body_fixture = new b2FixtureDef; // b2FixtureDef type variable for human's mid_body fixture human_mid_body_fixture->shape = &human_mid_body_shape; b2CircleShape human_face_shape; // b2CircleShape type variable for human's face shape human_face_shape.m_radius = 1.0; b2FixtureDef *human_face_fixture = new b2FixtureDef; // b2FixtureDef type variable for human's face fixture human_face_fixture->shape = &human_face_shape; b2PolygonShape human_left_leg_shape; // b2PolygonShape type variable for human's left leg shape human_left_leg_shape.SetAsBox(0.3f, 2.f, b2Vec2(-1.3-x,-7),0); b2FixtureDef *human_left_leg_fixture = new b2FixtureDef; // type variable for human's left_leg fixture human_left_leg_fixture->shape = &human_left_leg_shape; b2PolygonShape human_right_leg_shape; // b2PolygonShape type variable for human's right_leg shape human_right_leg_shape.SetAsBox(0.3f, 2.f, b2Vec2(1.3-x,-7),0); b2FixtureDef *human_right_leg_fixture = new b2FixtureDef; // type variable for human's right_leg fixture human_right_leg_fixture->shape = &human_right_leg_shape; b2PolygonShape human_right_hand_shape; // b2PolygonShape type variable for human's right_hand shape human_right_hand_shape.SetAsBox(0.3f, 2.2f, b2Vec2(2-x,1.2),3); b2FixtureDef *human_right_hand_fixture = new b2FixtureDef; // type variable for human's right_hand fixture human_right_hand_fixture->shape = &human_right_hand_shape; b2PolygonShape human_left_hand_shape; // b2PolygonShape type variable for human's left_hand shape human_left_hand_shape.SetAsBox(0.3f, 2.2f, b2Vec2(-2-x,1.2),-3); b2FixtureDef *human_left_hand_fixture = new b2FixtureDef; // b2PolygonShape type variable for human's left_hand shape human_left_hand_fixture->shape = &human_left_hand_shape; human_body->CreateFixture(human_mid_body_fixture); human_body->CreateFixture(human_face_fixture); human_body->CreateFixture(human_left_leg_fixture); human_body->CreateFixture(human_right_leg_fixture); human_body->CreateFixture(human_right_hand_fixture); human_body->CreateFixture(human_left_hand_fixture); } { b2Body* hydrogen_sphere_body; //!hydrogen_sphere_body is b2Body* type variable for hydrogen sphere body b2CircleShape hydrogen_sphere_shape; //b2CircleShape type variable for hydrogen sphere shape hydrogen_sphere_shape.m_radius = 1.0; b2FixtureDef hydrogen_sphere_fixture_def; //b2FixtureDef type variable for hydrogen sphere fixture definition hydrogen_sphere_fixture_def.shape = &hydrogen_sphere_shape; hydrogen_sphere_fixture_def.density = 10.0f; hydrogen_sphere_fixture_def.friction = 0.1f; hydrogen_sphere_fixture_def.restitution = 0.0f; b2BodyDef hydrogen_sphere_body_def; // b2BodyDef type variable for hydrogen sphere body definition hydrogen_sphere_body_def.type = b2_dynamicBody; hydrogen_sphere_body_def.position.Set(-72.f, -5.f); hydrogen_sphere_body = m_world->CreateBody(&hydrogen_sphere_body_def); hydrogen_sphere_body->CreateFixture(&hydrogen_sphere_fixture_def); hydrogen_sphere_body->SetGravityScale(-1); hydrogen_sphere_body_def.position.Set(-54.f, -5.f); hydrogen_sphere_body = m_world->CreateBody(&hydrogen_sphere_body_def); hydrogen_sphere_body->CreateFixture(&hydrogen_sphere_fixture_def); hydrogen_sphere_body->SetGravityScale(-1); hydrogen_sphere_shape.m_radius = 0.5f; hydrogen_sphere_fixture_def.density = 5.0f; hydrogen_sphere_fixture_def.friction = 0.0f; hydrogen_sphere_fixture_def.restitution = 0.6f; hydrogen_sphere_body_def.type = b2_dynamicBody; hydrogen_sphere_body_def.position.Set(-78.f, 17.f); hydrogen_sphere_body = m_world->CreateBody(&hydrogen_sphere_body_def); hydrogen_sphere_body->CreateFixture(&hydrogen_sphere_fixture_def); hydrogen_sphere_body->SetLinearVelocity(b2Vec2(5,2)); } { b2BodyDef hinge_body_def; //!hinge_body_def is b2BodyDef type variable for hinge body definition hinge_body_def.position.Set(-80.f, 4.f); b2Body* hinge_body = m_world->CreateBody(&hinge_body_def); b2PolygonShape shape1; // b2PolygonShape type variable for body1 = box below human shape1.SetAsBox(6.f, 4.f, b2Vec2(0,8),0); b2FixtureDef *fd1 = new b2FixtureDef; fd1->shape = &shape1; b2PolygonShape shape2; // b2PolygonShape type variable for body2 = box below killer shape2.SetAsBox(8.f, 4.f, b2Vec2(17,8),0); b2FixtureDef *fd2 = new b2FixtureDef; fd2->shape = &shape2; b2PolygonShape shape3; // b2PolygonShape type variable for body3 = tower shape3.SetAsBox(0.6f, 16.f, b2Vec2(36,28),0); b2FixtureDef *fd3 = new b2FixtureDef; fd3->shape = &shape3; b2PolygonShape shape4; // b2PolygonShape type variable for body4 = box below tower shape4.SetAsBox(4.f, 4.f, b2Vec2(34,8),0); b2FixtureDef *fd4 = new b2FixtureDef; fd4->shape = &shape4; b2PolygonShape shape10; //! shape10 is b2PolygonShape type variable for body 10. body10 = horizontal bar shape10.SetAsBox(36.f, 0.4f, b2Vec2(-20.f,0.f),0); b2PolygonShape shape11; //!shape11 b2PolygonShape type variable for body11. body11 = verticla bar. shape11.SetAsBox(0.2f, 3.0f , b2Vec2(-56.f,2.f),0); b2PolygonShape shape12; //!shape11 b2PolygonShape type variable for body11. body11 = verticla bar. shape12.SetAsBox(0.2f, 6.0f , b2Vec2(-59.f,2.f),0); b2PolygonShape shape13; //!shape11 b2PolygonShape type variable for body11. body11 = verticla bar. shape13.SetAsBox(9.f, 0.3f , b2Vec2(-52.f,-5.f),-0.2); b2BodyDef bd10; // b2BodyDef type variable for body10 bd10.position.Set(-44.0f, 40.0f); bd10.type = b2_dynamicBody; b2Body* body10 = m_world->CreateBody(&bd10); b2FixtureDef *fd10 = new b2FixtureDef; // b2FixtureDef type variable for body10 fd10->density = 0.52f; fd10->restitution = 0.0f; fd10->shape = new b2PolygonShape; fd10->shape = &shape10; b2FixtureDef *fd11 = new b2FixtureDef; // b2BodyDef type variable for body11 fd11->density = 0.2f; fd11->shape = new b2PolygonShape; fd11->shape = &shape11; b2FixtureDef *fd12 = new b2FixtureDef; // b2FixtureDef type variable for body10 fd12->density = 0.2f; fd12->restitution = 0.0f; fd12->shape = new b2PolygonShape; fd12->shape = &shape12; b2FixtureDef *fd13 = new b2FixtureDef; // b2FixtureDef type variable for body10 fd13->density = 0.2f; fd13->restitution = 0.0f; fd13->shape = new b2PolygonShape; fd13->shape = &shape13; body10->CreateFixture(fd10); body10->CreateFixture(fd11); body10->CreateFixture(fd12); body10->CreateFixture(fd13); b2BodyDef bd12; // b2BodyDef type variable for body12 which is hidden object. bd12.position.Set(-44.0f, 40.0f); b2Body* body12 = m_world->CreateBody(&bd12); b2RevoluteJointDef jointDef10; //!jointDef10 is b2RevoluteJointDef type variable used for the revolute joint jointDef10.bodyA = body10; jointDef10.bodyB = body12; jointDef10.localAnchorA.Set(+0.5,0.1); jointDef10.localAnchorB.Set(-0.5,-0.1); jointDef10.collideConnected = false; m_world->CreateJoint(&jointDef10); hinge_body->CreateFixture(fd1); hinge_body->CreateFixture(fd2); hinge_body->CreateFixture(fd3); hinge_body->CreateFixture(fd4); { // b2PolygonShape shape5; // b2PolygonShape type variable for body5 = hanging tower // shape5.SetAsBox(0.3f, 4.f, b2Vec2(18,0),0); // b2FixtureDef *fd5 = new b2FixtureDef; // fd5->shape = &shape5; // // hinge_body->CreateFixture(fd5); b2BodyDef hanger_body_def; //!hinge_body_def is b2BodyDef type variable for hinge body definition hanger_body_def.position.Set(-63, 0); b2Body* hanger_body = m_world->CreateBody(&hanger_body_def); float32 a = -3.0f; b2Vec2 h(63.0f, a); b2BodyDef bodyDef; bodyDef.position = -h; b2Body* body = m_world->CreateBody(&bodyDef); b2PolygonShape shape; shape.SetAsBox(0.4f, 5.f); body->CreateFixture(&shape, 20.f); b2Vec2 p(63.0f, 0.f); b2BodyDef bodyDef1; bodyDef1.type = b2_dynamicBody; bodyDef1.position = -p; b2Body* body1 = m_world->CreateBody(&bodyDef1); b2PolygonShape shape1; shape1.SetAsBox(9.f, 0.4f); body1->CreateFixture(&shape1, 20.f); b2Vec2 h111(0.0f, 2.5f); b2RevoluteJointDef jointDef; jointDef.bodyA = hanger_body; jointDef.bodyB = body1; jointDef.localAnchorA.SetZero(); jointDef.localAnchorB=h111; m_world->CreateJoint(&jointDef); } } // { // b2BodyDef hanger_body_def; //!hinge_body_def is b2BodyDef type variable for hinge body definition // hanger_body_def.position.Set(-80.f, -4.f); // b2Body* hanger_body = m_world->CreateBody(&hanger_body_def); // // float32 a = 5.0f; // b2Vec2 h(0.0f, a); // // float32 density = 20.0f; // b2Vec2 h1(0.0f, a); // // b2Vec2 p = hanger_body->GetPosition() + b2Vec2_zero - h1; // // b2BodyDef bodyDef; // bodyDef.type = b2_dynamicBody; // bodyDef.position = p; // b2Body* body = m_world->CreateBody(&bodyDef); // // b2PolygonShape shape; // shape.SetAsBox(0.25f * a, a); // body->CreateFixture(&shape, density); // //// b2PolygonShape killer_right_hand_shape; // b2PolygonShape type variable for killer's right_hand shape //// killer_right_hand_shape.SetAsBox(0.3f, 2.f, b2Vec2(2.5,-3),0.3); //// b2FixtureDef *killer_right_hand_fixture = new b2FixtureDef; // b2FixtureDef type variable for killer's right_hand fixture //// killer_right_hand_fixture->shape = &killer_right_hand_shape; //// killer_body->CreateFixture(killer_left_leg_fixture); // //// b2PolygonShape killer_right_hand_shape; //// killer_right_hand_shape.SetAsBox(a*4,a*0.25,b2Vec2(2.5,-23),0.3); //// b2FixtureDef *killer_right_hand_fixture = new b2FixtureDef; //// killer_right_hand_fixture->shape = &killer_right_hand_shape; //// hanger_body->CreateFixture(killer_right_hand_fixture); // a=3; // float32 offset=0.0f; // b2Vec2 h111(0.0f, a); // b2Vec2 p1 = p + b2Vec2(offset, -a) - h111; // // b2BodyDef bodyDef1; // bodyDef1.type = b2_dynamicBody; // bodyDef1.position = p1; // b2Body* body1 = m_world->CreateBody(&bodyDef1); // // shape.SetAsBox(12.0f,1.0f); // body1->CreateFixture(&shape, density); // // b2RevoluteJointDef jointDef1; // jointDef1.bodyA = hanger_body; // jointDef1.bodyB = body1; // jointDef1.localAnchorA.SetZero(); // jointDef1.localAnchorB = h111 + 3*h111; // m_world->CreateJoint(&jointDef1); // //// float32 offset=20.0f; //// b2Vec2 a1 = b2Vec2(0, -a); //// b2Vec2 a2 = b2Vec2(0, -a); //// //// b2BodyDef bodyDef1; //// bodyDef1.position = a1; //// //// b2BodyDef bodyDef2; //// bodyDef2.position = a2; //// b2Body* body1 = m_world->CreateBody(&bodyDef1); //// b2Body* body2 = m_world->CreateBody(&bodyDef2); //// //// b2RevoluteJointDef jointDef1,jointDef2,jointDef3; //// jointDef1.bodyA = body; //// jointDef1.localAnchorB = h; //// //// jointDef1.localAnchorA = a1; //// jointDef1.bodyB = body1; //// m_world->CreateJoint(&jointDef1); //// //// jointDef1.localAnchorA = a2; //// jointDef1.bodyB = body2; //// m_world->CreateJoint(&jointDef1); // // //b2Body* root = AddNode(ground, b2Vec2_zero, 0, 3.0f, a); // // b2RevoluteJointDef jointDef; // jointDef.bodyA = hanger_body; // jointDef.bodyB = body; // jointDef.localAnchorA.SetZero(); // jointDef.localAnchorB = h; // m_world->CreateJoint(&jointDef); // } }
void Player::update(GameEngine::InputManager& inputManager){ b2Body* body = m_capsule.getBody(); if (inputManager.isKeyDown(SDLK_a)) { body->ApplyForceToCenter(b2Vec2(-100.0, 0.0), true); m_direction = -1; } else if (inputManager.isKeyDown(SDLK_d)) { body->ApplyForceToCenter(b2Vec2(100.0, 0.0), true); m_direction = 1; } else{ //Apply damping body->SetLinearVelocity(b2Vec2(body->GetLinearVelocity().x * 0.95, body->GetLinearVelocity().y)); } //Check for punch if (inputManager.isKeyDown(SDLK_SPACE)) { m_isPunching = true; } float MAX_SPEED = 10.0f; if (body->GetLinearVelocity().x < (-MAX_SPEED)) { body->SetLinearVelocity(b2Vec2(-MAX_SPEED, body->GetLinearVelocity().y)); } else if (body->GetLinearVelocity().x > MAX_SPEED) { body->SetLinearVelocity(b2Vec2(MAX_SPEED, body->GetLinearVelocity().y)); } ///Detect if player is standing on the ground //Loop through all the contact points m_onGround = false; for (b2ContactEdge* ce = body->GetContactList(); ce != nullptr; ce = ce->next) { b2Contact* c = ce->contact; if (c->IsTouching()) { b2WorldManifold manifold; c->GetWorldManifold(&manifold); //Check if the points are below bool below = false; for (size_t i = 0; i < b2_maxManifoldPoints; i++) { if (manifold.points[i ].y < body->GetPosition().y - m_capsule.getDimensions().y / 2.0f + m_capsule.getDimensions().x /2.0f + 0.01f) { below = true; break; } } if (below) { m_onGround = true; //We can jump if (inputManager.isKeyPressed(SDLK_w)) { body->ApplyLinearImpulse(b2Vec2(0.0f, 30.0f), b2Vec2(0.0f, 0.0f), true); break; } } } } }
void PhysicsWorld::createRope(b2Body* bodyA, b2Body* bodyB,int length) { // About The Small Rectangles // Constructing A Rope float smallRectWidth = 1; float smallRectHeight = 2; float anchorMargin = 0.25; int needSmallRectNumber = length * meterPerPixel / (smallRectHeight - 2*anchorMargin) - 1; cout<<"length: "<<length * meterPerPixel<<endl; cout<<"need small rect number: "<<needSmallRectNumber<<endl; cout<<"smallRectHeight - 2*anchorMargin = "<<smallRectHeight - 2*anchorMargin<<endl; //float angle = -getRectangleInRadian(bodyA, bodyB); b2Body* leftBody; b2Body* rightBody; if(bodyA->GetPosition().x < bodyB->GetPosition().x) { leftBody = bodyA; rightBody = bodyB; } else { leftBody = bodyB; rightBody = bodyA; } b2Body* aboveBody; b2Body* belowBody; if(bodyA->GetPosition().y > bodyB->GetPosition().y) { aboveBody = bodyA; belowBody = bodyB; } else { aboveBody = bodyB; belowBody = bodyA; } float dx = rightBody->GetPosition().x - leftBody->GetPosition().x; float dy = aboveBody->GetPosition().y - belowBody->GetPosition().y; float dLong; float dShort; b2Body* startBody; b2Body* endBody; if(dx > dy) { dLong = dx / needSmallRectNumber; dShort = dy / needSmallRectNumber; startBody = leftBody; endBody = rightBody; } else { // dy >= dx dLong = dy / needSmallRectNumber; dShort = dx / needSmallRectNumber; startBody = belowBody; endBody = aboveBody; } b2RevoluteJointDef revoluteJointDef; revoluteJointDef.collideConnected = false; b2Body* previousBody = startBody; b2Vec2 startPoint = startBody->GetPosition(); b2Body* smallRect; for(int i=0; i<needSmallRectNumber; i++) { PhysicsBody physicsBody(world); smallRect = physicsBody.newBody() ->setBodyPosition(b2Vec2(startPoint.x,startPoint.y)) ->setBodyType(b2BodyType::b2_dynamicBody) ->setPolygonShapeASBox(smallRectWidth/2,smallRectHeight/2) ->setFixtureDensity(0.1) ->setFixtureGroupIndex(-2) ->setFixtureMaskBits(0) ->createFixture() ->createBody(); revoluteJointDef.bodyA = previousBody; revoluteJointDef.bodyB = smallRect; if(previousBody == startBody) revoluteJointDef.localAnchorA = b2Vec2(0, 0); else revoluteJointDef.localAnchorA = b2Vec2(0, (-smallRectHeight/2 + anchorMargin)); revoluteJointDef.localAnchorB = b2Vec2(0, (+smallRectHeight/2 - anchorMargin) ); world->CreateJoint(&revoluteJointDef); previousBody = revoluteJointDef.bodyB; if(dx > dy) { startPoint.x += dLong; startPoint.y += dShort; } else { startPoint.x += dShort; startPoint.y += dLong; } } // end for revoluteJointDef.bodyA = previousBody; revoluteJointDef.bodyB = bodyB; world->CreateJoint(&revoluteJointDef); b2RopeJointDef ropeJointDef; ropeJointDef.collideConnected = true; ropeJointDef.bodyA = bodyA; ropeJointDef.bodyB = bodyB; ropeJointDef.maxLength = length * meterPerPixel; cout<<"Rope Length: "<<ropeJointDef.maxLength<<endl; world->CreateJoint(&ropeJointDef); }
void BallCoreController::ExecuteCommand(int command, void* data) { switch( command ) { case COMMAND_JUMP: { if( !jumping ) { float vel = sqrt(sqrt( spriteRadius.Get()*2*9.81 ));//what body->SetLinearVelocity( b2Vec2( body->GetLinearVelocity().x, -vel ) ); jumping = true; } break; } case COMMAND_MOVE: { CommandData_Move* moveData = (CommandData_Move*)data; if( moveData->start ) { movingDirection[ moveData->direction ] = true; } else { movingDirection[ moveData->direction ] = false; } break; } case COMMAND_ACTION1: { if( (bool)data ) { if( !split ) { SplitBubble(); split = true; } } else { split = false; } break; } case COMMAND_ACTION2: { if( (bool)data ) { absorbing = true; } else { absorbing = false; absorbed = false; } break; } case COMMAND_ACTION3: { Object* obj = ObjectTemplateManager::Instance()->Create( "Bubble" ); obj->GetProperty<float>("Width") = 40; obj->GetProperty<float>("Height") = 40; obj->GetProperty<float>("PosX") = posX.Get(); obj->GetProperty<float>("PosY") = posY.Get() + 50; obj->InitComponents(); ObjectManager::Instance()->AddObject( obj ); break; } case COMMAND_ACTION4: { std::cout << posX.Get() << " " << posY.Get() << std::endl; break; } default: break; } }
void Thruster::up() { thrust(b2Vec2(0, 1)); }
void Thruster::left() { thrust(b2Vec2(-1, 0)); }
void physics_apply_local_impulse(int id, double xlocal, double ylocal, double ximpulse, double yimpulse, bool wake) { get_fixture(sb2dfixture, id); sb2dfixture->body->ApplyLinearImpulse(b2Vec2(ximpulse, yimpulse), b2Vec2(xlocal, ylocal), wake); }
bool FrameFunc() { float dt=hge->Timer_GetDelta(); bool keyD = false; bool keyF = false; bool keyLeft = false; bool keyRight = false; switch (gamestate) { case enter: if(hge->Input_GetKeyState(HGEK_SPACE)) { gamestate = init; } break; case init: initGame(); gamestate = fighting; break; case fighting: //判断左右移动 if(hge->Input_GetKeyState(HGEK_LEFT)) { keyLeft = true; } if(hge->Input_GetKeyState(HGEK_RIGHT)) { keyRight = true; } //判断跳跃 if(hge->Input_GetKeyState(HGEK_D)) { keyD = true; } //判断射击 if(hge->Input_GetKeyState(HGEK_F)) { keyF = true; } for(b2Body* b = world->GetBodyList();b;b=b->GetNext()) { if (b->GetUserData() != NULL) { GameObj* obj = (GameObj*)b->GetUserData(); if(obj->getType() == obj_Fighter) { Fighter* f = (Fighter*)b->GetUserData(); //站立的情况下按跳跃键 if(keyD && f->getState() == standing) { f->setState(jumping); b->ApplyLinearImpulse(b2Vec2(0,b->GetMass()*-FIGHTER_JUMP),b->GetWorldCenter()); } //冷却状态按下射击键 if(keyF && f->getColdDown() <= 0) { f->setColdDown(FIGHTER_CD); //添加勇士的子弹 f->fire(); } //处理移动键 if(keyLeft) { b2Vec2 oldv = b->GetLinearVelocity(); b->SetLinearVelocity(b2Vec2(-(float)FIGHTER_V/PTM_RATIO,oldv.y)); } else if(keyRight) { b2Vec2 oldv = b->GetLinearVelocity(); b->SetLinearVelocity(b2Vec2((float)FIGHTER_V/PTM_RATIO,oldv.y)); } else { b2Vec2 oldv = b->GetLinearVelocity(); b->SetLinearVelocity(b2Vec2(0,oldv.y)); } //根据物理引擎更新fighter的信息 f->setPositionX(b->GetPosition().x*PTM_RATIO); f->setPositionY(b->GetPosition().y*PTM_RATIO); if(f->getColdDown() > 0) { f->setColdDown(f->getColdDown() - dt); } } else if(obj->getType() == obj_boss) { //施加一个力抵消重力 float32 m = b->GetMass(); b->ApplyForceToCenter(b2Vec2(0,-m*10)); float center = WIN_HEIGHT / 2; float top = center - BOSS_MOVE_PATH/2; float button = center + BOSS_MOVE_PATH/2; if((b->GetPosition().y) < top/PTM_RATIO || (b->GetPosition().y) > button/PTM_RATIO) { b2Vec2 oldv = b->GetLinearVelocity(); oldv.x = oldv.x * -1; oldv.y = oldv.y * -1; b->SetLinearVelocity(oldv); } //根据物理引擎更新boss的信息 boss->setPositionX(b->GetPosition().x*PTM_RATIO); boss->setPositionY(b->GetPosition().y*PTM_RATIO); boss->setNormalFireCounter(boss->getNormalFireCounter()-dt); if(boss->getNormalFireCounter() < 0) { boss->fire(B1Bullet); } if(boss->getGoingToFire() == true) { boss->fire(B2Bullet); boss->setGoingToFire(false); } } else if(obj->getType() == obj_bullet) { //施加一个力抵消重力 float32 m = b->GetMass(); b->ApplyForceToCenter(b2Vec2(0,-m*10)); Bullet *bullet = (Bullet*)obj; bullet->setPositionX(b->GetPosition().x*PTM_RATIO); bullet->setPositionY(b->GetPosition().y*PTM_RATIO); } else { } } } for(bodyit=willDelete->begin();bodyit != willDelete->end();) { b2Body *b = (b2Body*)*bodyit; Bullet* bullet = (Bullet*)b->GetUserData(); world->DestroyBody(b); willDelete->erase(bodyit++); bullets->remove(bullet); } world->Step(dt,8,8); break; case over: if(hge->Input_GetKeyState(HGEK_SPACE)) { gamestate = init; } if(hge->Input_GetKeyState(HGEK_ESCAPE)) { return true; } break; case win: if(hge->Input_GetKeyState(HGEK_SPACE)) { } break; } return false; }
void physics_fixture_set_position(int id, double x, double y) { get_fixture(sb2dfixture, id); sb2dfixture->body->SetTransform(b2Vec2(x, y), sb2dfixture->body->GetAngle()); }
void physics_apply_local_force(int id, double xlocal, double ylocal, double xforce, double yforce, bool wake) { get_fixture(sb2dfixture, id); sb2dfixture->body->ApplyForce(b2Vec2(xforce, yforce), b2Vec2(xlocal, ylocal), wake); }
void physics_fixture_set_transform(int id, double x, double y, double angle) { get_fixture(sb2dfixture, id); sb2dfixture->body->SetTransform(b2Vec2(x, y), cs_angle_to_radians(angle)); }
void physics_fixture_add_point(int id, double x, double y) { get_fixture(sb2dfixture, id); sb2dfixture->vertices.push_back(b2Vec2(x, y)); }
void physics_world_gravity(int index, double gx, double gy) { get_world(sb2dworld, index); worlds[index]->world->SetGravity(b2Vec2(gx, gy)); }
void b2RevoluteJoint::SolveVelocityConstraints(const b2SolverData& data) { b2Vec2 vA = data.velocities[m_indexA].v; float32 wA = data.velocities[m_indexA].w; b2Vec2 vB = data.velocities[m_indexB].v; float32 wB = data.velocities[m_indexB].w; float32 mA = m_invMassA, mB = m_invMassB; float32 iA = m_invIA, iB = m_invIB; bool fixedRotation = (iA + iB == 0.0f); // Solve motor constraint. if (m_enableMotor && m_limitState != e_equalLimits && fixedRotation == false) { float32 Cdot = wB - wA - m_motorSpeed; float32 impulse = -m_motorMass * Cdot; float32 oldImpulse = m_motorImpulse; float32 maxImpulse = data.step.dt * m_maxMotorTorque; m_motorImpulse = b2Clamp(m_motorImpulse + impulse, -maxImpulse, maxImpulse); impulse = m_motorImpulse - oldImpulse; wA -= iA * impulse; wB += iB * impulse; } // Solve limit constraint. if (m_enableLimit && m_limitState != e_inactiveLimit && fixedRotation == false) { b2Vec2 Cdot1 = vB + b2Cross(wB, m_rB) - vA - b2Cross(wA, m_rA); float32 Cdot2 = wB - wA; b2Vec3 Cdot(Cdot1.x, Cdot1.y, Cdot2); b2Vec3 impulse = -m_mass.Solve33(Cdot); if (m_limitState == e_equalLimits) { m_impulse += impulse; } else if (m_limitState == e_atLowerLimit) { float32 newImpulse = m_impulse.z + impulse.z; if (newImpulse < 0.0f) { b2Vec2 rhs = -Cdot1 + m_impulse.z * b2Vec2(m_mass.ez.x, m_mass.ez.y); b2Vec2 reduced = m_mass.Solve22(rhs); impulse.x = reduced.x; impulse.y = reduced.y; impulse.z = -m_impulse.z; m_impulse.x += reduced.x; m_impulse.y += reduced.y; m_impulse.z = 0.0f; } else { m_impulse += impulse; } } else if (m_limitState == e_atUpperLimit) { float32 newImpulse = m_impulse.z + impulse.z; if (newImpulse > 0.0f) { b2Vec2 rhs = -Cdot1 + m_impulse.z * b2Vec2(m_mass.ez.x, m_mass.ez.y); b2Vec2 reduced = m_mass.Solve22(rhs); impulse.x = reduced.x; impulse.y = reduced.y; impulse.z = -m_impulse.z; m_impulse.x += reduced.x; m_impulse.y += reduced.y; m_impulse.z = 0.0f; } else { m_impulse += impulse; } } b2Vec2 P(impulse.x, impulse.y); vA -= mA * P; wA -= iA * (b2Cross(m_rA, P) + impulse.z); vB += mB * P; wB += iB * (b2Cross(m_rB, P) + impulse.z); } else { // Solve point-to-point constraint b2Vec2 Cdot = vB + b2Cross(wB, m_rB) - vA - b2Cross(wA, m_rA); b2Vec2 impulse = m_mass.Solve22(-Cdot); m_impulse.x += impulse.x; m_impulse.y += impulse.y; vA -= mA * impulse; wA -= iA * b2Cross(m_rA, impulse); vB += mB * impulse; wB += iB * b2Cross(m_rB, impulse); } data.velocities[m_indexA].v = vA; data.velocities[m_indexA].w = wA; data.velocities[m_indexB].v = vB; data.velocities[m_indexB].w = wB; }
void initGame() { hge->Random_Seed(time(0)); //init b2world world = new b2World(b2Vec2(0,10)); world->SetContinuousPhysics(true); world->SetAllowSleeping(true); MyContactListener* listener = new MyContactListener(); world->SetContactListener(listener); //init fighter fighter = new Fighter(); fighter->setPositionX(WIN_WIDTH/2); fighter->setPositionY(WIN_HEIGHT/2); fighter->setState(jumping); fighter->setType(obj_Fighter); fighter->setColdDown(-1); //init boss boss = new Boss(); boss->setHp(BOSS_MAXHP); boss->setType(obj_boss); boss->setPositionX((float)WIN_WIDTH - BOSS_WIDTH/2 - WALL_DEPTH); boss->setPositionY((float)WIN_HEIGHT/2); boss->setV(BOSS_V1); boss->setNormalFireCounter(BOSS_B1BULLET_COLDDOWN); boss->setSpecialBulletNum(0); boss->setGoingToFire(false); //init wall body wall1 = new GameObj(); wall1->setType(obj_wall); b2BodyDef groundDef; groundDef.position.Set((float)WIN_WIDTH/2/PTM_RATIO,(float)WALL_DEPTH/2/PTM_RATIO); groundDef.type = b2BodyType::b2_staticBody; groundDef.userData = wall1; b2Body* b2ground = world->CreateBody(&groundDef); b2PolygonShape groundShape; groundShape.SetAsBox((float)WIN_WIDTH/2/PTM_RATIO,(float)WALL_DEPTH/2/PTM_RATIO); b2FixtureDef groundFixtureDef; groundFixtureDef.shape = &groundShape; b2ground->CreateFixture(&groundFixtureDef); sprwall1 = new hgeSprite(tex1,0,0,(float)WIN_WIDTH,(float)WALL_DEPTH); sprwall1->SetHotSpot((float)WIN_WIDTH/2,(float)WALL_DEPTH/2); //地板 ground = new GameObj(); ground->setType(obj_ground); groundDef.position.Set((float)WIN_WIDTH/2/PTM_RATIO,((float)WIN_HEIGHT-(float)WALL_DEPTH/2)/PTM_RATIO); groundDef.type = b2BodyType::b2_staticBody; groundDef.userData = ground; b2Body* b2ground2 = world->CreateBody(&groundDef); b2PolygonShape groundShape2; groundShape2.SetAsBox((float)WIN_WIDTH/2/PTM_RATIO,(float)WALL_DEPTH/2/PTM_RATIO); b2FixtureDef groundFixtureDef2; groundFixtureDef2.shape = &groundShape2; b2ground2->CreateFixture(&groundFixtureDef2); sprwall2 = new hgeSprite(tex1,0,0,(float)WIN_WIDTH,(float)WALL_DEPTH); sprwall2->SetHotSpot((float)WIN_WIDTH/2,(float)WALL_DEPTH/2); wall2 = new GameObj(); wall2->setType(obj_wall); groundDef.position.Set((float)WALL_DEPTH/2/PTM_RATIO,(float)WIN_HEIGHT/2/PTM_RATIO); groundDef.type = b2BodyType::b2_staticBody; groundDef.userData = wall2; b2Body* b2ground3 = world->CreateBody(&groundDef); b2PolygonShape groundShape3; groundShape3.SetAsBox((float)WALL_DEPTH/2/PTM_RATIO,(float)WIN_HEIGHT/2/PTM_RATIO); b2FixtureDef groundFixtureDef3; groundFixtureDef3.shape = &groundShape3; b2ground3->CreateFixture(&groundFixtureDef3); sprwall3 = new hgeSprite(tex1,0,0,(float)WALL_DEPTH,(float)WIN_HEIGHT); sprwall3->SetHotSpot((float)WALL_DEPTH/2,(float)WIN_HEIGHT/2); wall3 = new GameObj(); wall3->setType(obj_wall); groundDef.position.Set(((float)WIN_WIDTH-WALL_DEPTH/2)/PTM_RATIO,(float)WIN_HEIGHT/2/PTM_RATIO); groundDef.type = b2BodyType::b2_staticBody; groundDef.userData = wall3; b2Body* b2ground4 = world->CreateBody(&groundDef); b2PolygonShape groundShape4; groundShape4.SetAsBox((float)WALL_DEPTH/2/PTM_RATIO,(float)WIN_HEIGHT/2/PTM_RATIO); b2FixtureDef groundFixtureDef4; groundFixtureDef4.shape = &groundShape4; b2ground4->CreateFixture(&groundFixtureDef4); sprwall4 = new hgeSprite(tex1,0,0,(float)WALL_DEPTH,(float)WIN_HEIGHT); sprwall4->SetHotSpot((float)WALL_DEPTH/2,(float)WIN_HEIGHT/2); //init figter body b2BodyDef fighterBodyDef; fighterBodyDef.position.Set(WIN_WIDTH/2/PTM_RATIO,WIN_HEIGHT/2/PTM_RATIO); fighterBodyDef.type = b2_dynamicBody; fighterBodyDef.userData = fighter; b2Body* fighterBody = world->CreateBody(&fighterBodyDef); b2PolygonShape fighterShape; fighterShape.SetAsBox(FIGHTER_WIDTH/2/PTM_RATIO,FIGHTER_HEIGHT/2/PTM_RATIO); b2FixtureDef fighterFixtureDef; fighterFixtureDef.shape = &fighterShape; fighterFixtureDef.density = 1; fighterFixtureDef.friction = 0; fighterBody->CreateFixture(&fighterFixtureDef); sprfighter = new hgeSprite(texfighter,0,0,FIGHTER_WIDTH,FIGHTER_HEIGHT); sprfighter->SetHotSpot(FIGHTER_WIDTH/2,FIGHTER_HEIGHT/2); //init boss body b2BodyDef bossBodyDef; bossBodyDef.position.Set(((float)WIN_WIDTH-BOSS_WIDTH/2 - WALL_DEPTH)/PTM_RATIO,WIN_HEIGHT/2/PTM_RATIO); bossBodyDef.type = b2_dynamicBody; bossBodyDef.userData = boss; b2Body* bossBody = world->CreateBody(&bossBodyDef); b2PolygonShape bossShape; bossShape.SetAsBox(BOSS_WIDTH/2/PTM_RATIO,BOSS_HEIGHT/2/PTM_RATIO); b2FixtureDef bossFixtureDef; bossFixtureDef.shape = &bossShape; bossFixtureDef.density = 1; bossBody->CreateFixture(&bossFixtureDef); bossBody->SetLinearVelocity(b2Vec2(0,boss->getV()/PTM_RATIO)); sprboss = new hgeSprite(texboss,0,0,BOSS_WIDTH,BOSS_HEIGHT); sprboss->SetHotSpot(BOSS_WIDTH/2,BOSS_HEIGHT/2); //init bullet list bullets = new std::list<Bullet*>(); willDelete = new std::list<b2Body*>(); //init fbullet sprfbullet = new hgeSprite(texfbullet,0,0,BULLET_SIZE,BULLET_SIZE); sprfbullet->SetHotSpot(BULLET_SIZE/2,BULLET_SIZE/2); //init b1bullet sprb1bullet = new hgeSprite(texb1bullet,0,0,BULLET_SIZE,BULLET_SIZE); sprb1bullet->SetHotSpot(BULLET_SIZE/2,BULLET_SIZE/2); //init b2bullet sprb2bullet = new hgeSprite(texb2bullet,0,0,BULLET_SIZE,BULLET_SIZE); sprb2bullet->SetHotSpot(BULLET_SIZE/2,BULLET_SIZE/2); //init hpwin and hp sprhpwin = new hgeSprite(texhpwin,0,0,HPWIN_WIDTH,HPWIN_HEIGHT); sprhpwin->SetHotSpot(0,0); sprhp = new hgeSprite(texhp,0,0,HP_WIDTH,HP_HEIGHT); sprhp->SetHotSpot(0,0); //init game state gamestate = fighting; }
void Physics::SetGravity(glm::vec2 gravity) { physicWorld->SetGravity(b2Vec2(gravity.x, gravity.y)); }
void init() { world=new b2World(b2Vec2(0.0,9.81)); //addRect(WIDTH/2,HEIGHT-50,WIDTH,30,false); addRect(WIDTH/2,HEIGHT-50,790,30,"base", false); }
void Thruster::down() { thrust(b2Vec2(0, -1)); }
Private() : world(b2Vec2(0.f, -10.f)) {}
void Thruster::right() { thrust(b2Vec2(1, 0)); }
void Entity::SetVelocity( sf::Vector2f velocity ) { pImpl->pBody->SetLinearVelocity( b2Vec2( velocity.x, velocity.y ) ); //pImpl->mVelocity = velocity; }
void CCPhysicsSprite::setPosition(const CCPoint &pos) { float angle = m_pB2Body->GetAngle(); m_pB2Body->SetTransform(b2Vec2(pos.x / m_fPTMRatio, pos.y / m_fPTMRatio), angle); }
void Entity::SetVelocity( float vx, float vy ) { pImpl->pBody->SetLinearVelocity( b2Vec2( vx, vy ) ); //pImpl->mVelocity.x = vx; //pImpl->mVelocity.y = vy; }
void BallCoreController::Update(float delta) { float maxSpeed = spriteRadius.Get()/7; if ( abs( sf::Joystick::getAxisPosition(0,sf::Joystick::X) ) > 0.3 ) { float speed = sf::Joystick::getAxisPosition(0,sf::Joystick::X)/100; if( abs(body->GetLinearVelocity().x) < abs(maxSpeed) ) { body->ApplyForceToCenter( b2Vec2( maxSpeed * speed, 0 ) ); } } if( Input::IsKeyDown( sf::Keyboard::Right ) ) { if( body->GetLinearVelocity().x < maxSpeed ) { if( body->GetLinearVelocity().x < 0 ) { body->ApplyForceToCenter( b2Vec2( maxSpeed, 0 ) ); } else { body->ApplyForceToCenter( b2Vec2( maxSpeed, 0 ) ); } } } if( Input::IsKeyDown( sf::Keyboard::Left ) ) { if( body->GetLinearVelocity().x > -maxSpeed ) { if( body->GetLinearVelocity().x > 0 ) { body->ApplyForceToCenter( b2Vec2( -maxSpeed*2, 0 ) ); } else { body->ApplyForceToCenter( b2Vec2( -maxSpeed, 0 ) ); } } } if( !Input::IsKeyDown( sf::Keyboard::Left ) && !Input::IsKeyDown( sf::Keyboard::Right ) ) { body->SetLinearVelocity( b2Vec2(body->GetLinearVelocity().x/1.1, body->GetLinearVelocity().y ) ); } if( absorbing ) { if( !absorbed ) { QueryObjectsAABB callback; b2AABB aabb; aabb.lowerBound = b2Vec2( ( posX.Get() - spriteRadius/2 )/PTM_RATIO, ( posY.Get() - spriteRadius/2 )/PTM_RATIO ); aabb.upperBound = b2Vec2( ( posX.Get() + spriteRadius/2 )/PTM_RATIO, ( posY.Get() + spriteRadius/2 )/PTM_RATIO ); PhysicsManager::Instance()->GetWorld()->QueryAABB( &callback, aabb ); std::vector<b2Fixture*> &fixtures = callback.fixtures; for(std::vector<b2Fixture*>::iterator it = fixtures.begin(); it != fixtures.end(); ++it ) { Object* obj = (Object*)(*it)->GetUserData(); if( obj == go ) continue; if( obj->HasComponent( "BubbleAbsorbable" ) ) { std::cout << "yes" << std::endl; spriteRadius += obj->GetProperty<float>("Width"); height = spriteRadius.Get(); obj->FlagForRemoval(); } } absorbed = true; } } }
void Entity::Accelerate( sf::Vector2f velocity ) { pImpl->pBody->SetLinearVelocity( b2Vec2( pImpl->pBody->GetLinearVelocity().x + velocity.x, pImpl->pBody->GetLinearVelocity().y + velocity.y ) ); //pImpl->mVelocity += velocity; }
Player::Player(ObjectLayer* layer, b2Vec2 position, uint16_t location) : layer(location), Collider(Collider::Player), inPortal(false), portalDirection(Portal::Up){ chdir("assets/player"); this->map = layer->map; b2BodyDef bodyDef; bodyDef.type = b2_dynamicBody; bodyDef.position = position; bodyDef.fixedRotation = true; body = layer->world.CreateBody(&bodyDef); body->SetUserData((Collider*)this); b2PolygonShape dynamicBox; dynamicBox.SetAsBox(0.47f, 0.75f, b2Vec2(0.5, 0.75), 0); b2FixtureDef fixtureDef; fixtureDef.shape = &dynamicBox; fixtureDef.density = 0.1f; fixtureDef.friction = 0; fixtureDef.userData = (b2ContactListener*)this; body->CreateFixture(&fixtureDef); b2CircleShape circle; circle.m_p.Set(0.5f, 1.5f); circle.m_radius = 0.49f; fixtureDef.shape = &circle; fixtureDef.density = 0.1f; fixtureDef.friction = 0.f; fixtureDef.userData = (b2ContactListener*)this; body->CreateFixture(&fixtureDef); b2CircleShape circlef; circlef.m_p.Set(0.5f, 1.9f); circlef.m_radius = 0.1; fixtureDef.shape = &circlef; fixtureDef.density = 0.1f; fixtureDef.friction = 2.f; fixtureDef.userData = (b2ContactListener*)this; body->CreateFixture(&fixtureDef); b2PolygonShape sensorBox; sensorBox.SetAsBox(0.45f, 0.1f, b2Vec2(0.5f, 2.f), 0); fixtureDef.shape = &sensorBox; fixtureDef.isSensor = true; fixtureDef.userData = (b2ContactListener*)this; body->CreateFixture(&fixtureDef); state = PlayerStanding; impulse = ImpulseNone; direction = Right; time = 0; jumpStart = 0; grounded = 0; std::ifstream atlasFile("pack.atlas"); atlas.reset(new spine::Atlas(atlasFile)); spine::SkeletonJson skeletonJson(atlas.get()); std::ifstream skeletonFile("skeleton.json"); skeletonData.reset(skeletonJson.readSkeletonData(skeletonFile)); std::ifstream animationFile("skeleton-run3.json"); run.reset(skeletonJson.readAnimation(animationFile, skeletonData.get())); skeleton.reset(new spine::Skeleton(skeletonData.get())); skeleton->flipX = false; skeleton->flipY = false; skeleton->setToBindPose(); skeleton->getRootBone()->x = X_OFFSET; skeleton->getRootBone()->y = Y_OFFSET; skeleton->updateWorldTransform(); sync(); chdir("../.."); }
void Entity::Accelerate( float vx, float vy ) { pImpl->pBody->SetLinearVelocity( b2Vec2( pImpl->pBody->GetLinearVelocity().x + vx, pImpl->pBody->GetLinearVelocity().y + vy ) ); //pImpl->mVelocity.x += vx; //pImpl->mVelocity.y += vy; }
void b2PrismaticJoint::SolveVelocityConstraints(const b2SolverData& data) { b2Vec2 vA = data.velocities[m_indexA].v; float32 wA = data.velocities[m_indexA].w; b2Vec2 vB = data.velocities[m_indexB].v; float32 wB = data.velocities[m_indexB].w; float32 mA = m_invMassA, mB = m_invMassB; float32 iA = m_invIA, iB = m_invIB; // Solve linear motor constraint. if (m_enableMotor && m_limitState != e_equalLimits) { float32 Cdot = b2Dot(m_axis, vB - vA) + m_a2 * wB - m_a1 * wA; float32 impulse = m_motorMass * (m_motorSpeed - Cdot); float32 oldImpulse = m_motorImpulse; float32 maxImpulse = data.step.dt * m_maxMotorForce; m_motorImpulse = b2Clamp(m_motorImpulse + impulse, -maxImpulse, maxImpulse); impulse = m_motorImpulse - oldImpulse; b2Vec2 P = impulse * m_axis; float32 LA = impulse * m_a1; float32 LB = impulse * m_a2; vA -= mA * P; wA -= iA * LA; vB += mB * P; wB += iB * LB; } b2Vec2 Cdot1; Cdot1.x = b2Dot(m_perp, vB - vA) + m_s2 * wB - m_s1 * wA; Cdot1.y = wB - wA; if (m_enableLimit && m_limitState != e_inactiveLimit) { // Solve prismatic and limit constraint in block form. float32 Cdot2; Cdot2 = b2Dot(m_axis, vB - vA) + m_a2 * wB - m_a1 * wA; b2Vec3 Cdot(Cdot1.x, Cdot1.y, Cdot2); b2Vec3 f1 = m_impulse; b2Vec3 df = m_K.Solve33(-Cdot); m_impulse += df; if (m_limitState == e_atLowerLimit) { m_impulse.z = b2Max(m_impulse.z, 0.0f); } else if (m_limitState == e_atUpperLimit) { m_impulse.z = b2Min(m_impulse.z, 0.0f); } // f2(1:2) = invK(1:2,1:2) * (-Cdot(1:2) - K(1:2,3) * (f2(3) - f1(3))) + f1(1:2) b2Vec2 b = -Cdot1 - (m_impulse.z - f1.z) * b2Vec2(m_K.ez.x, m_K.ez.y); b2Vec2 f2r = m_K.Solve22(b) + b2Vec2(f1.x, f1.y); m_impulse.x = f2r.x; m_impulse.y = f2r.y; df = m_impulse - f1; b2Vec2 P = df.x * m_perp + df.z * m_axis; float32 LA = df.x * m_s1 + df.y + df.z * m_a1; float32 LB = df.x * m_s2 + df.y + df.z * m_a2; vA -= mA * P; wA -= iA * LA; vB += mB * P; wB += iB * LB; } else { // Limit is inactive, just solve the prismatic constraint in block form. b2Vec2 df = m_K.Solve22(-Cdot1); m_impulse.x += df.x; m_impulse.y += df.y; b2Vec2 P = df.x * m_perp; float32 LA = df.x * m_s1 + df.y; float32 LB = df.x * m_s2 + df.y; vA -= mA * P; wA -= iA * LA; vB += mB * P; wB += iB * LB; Cdot1.x = b2Dot(m_perp, vB - vA) + m_s2 * wB - m_s1 * wA; Cdot1.y = wB - wA; if (b2Abs(Cdot1.x) > 0.01f || b2Abs(Cdot1.y) > 0.01f) { Cdot1.x += 0.0f; } } data.velocities[m_indexA].v = vA; data.velocities[m_indexA].w = wA; data.velocities[m_indexB].v = vB; data.velocities[m_indexB].w = wB; }
void GrassNode::Load(stringc filename,s32 frmWidth,s32 frmHeight) { IVideoDriver* driver = SceneManager->getVideoDriver(); //dimension2d<u32> Screensize = driver->getScreenSize(); fx = (float)frmWidth;// /(float)Screensize.Width; fy = (float)frmHeight;// /(float)Screensize.Height; Vertices[0] = S3DVertex(-fx,-fy,0, 0,0,0,SColor(255,255,255,255),0,1); Vertices[1] = S3DVertex( fx,-fy,0, 0,0,0,SColor(255,255,255,255),1,1); Vertices[2] = S3DVertex( fx, fy,0, 0,0,0,SColor(255,255,255,255),1,0); Vertices[3] = S3DVertex(-fx, fy,0, 0,0,0,SColor(255,255,255,255),0,0); Box.reset(Vertices[0].Pos); for (s32 i=1; i<4; ++i) Box.addInternalPoint(Vertices[i].Pos); Texture = driver->getTexture(filename); //driver->makeColorKeyTexture(Texture,position2d<s32>(0,0)); Material.setTexture(0,Texture); Material.TextureLayer[0].BilinearFilter = false; Material.MaterialType = EMT_TRANSPARENT_ALPHA_CHANNEL_REF; dimension2d<u32> size = Texture->getOriginalSize(); fWidth = (float)frmWidth/(float)size.Width; fHeight = (float)frmHeight/(float)size.Height; stepww = size.Width / frmWidth; stephh = size.Height / frmHeight; Vertices[0].TCoords.X = 0; Vertices[0].TCoords.Y = fHeight; Vertices[1].TCoords.X = fWidth; Vertices[1].TCoords.Y = fHeight; Vertices[2].TCoords.X = fWidth; Vertices[2].TCoords.Y = 0; Vertices[3].TCoords.X = 0; Vertices[3].TCoords.Y = 0; // Place Holder < ---------------------------------------- < - < - < - > amount = 4; for (unsigned int i = 0; i < amount; i++) { frames[i].preLoopFrame = 0; frames[i].startFrame = 0; frames[i].endFrame = 0; frames[i].time = 60; } // Box2d body = 0; b2BodyDef myBody; myBody.type = b2_staticBody; myBody.position.Set( 0, 0 ); myBody.angle = 0; myBody.fixedRotation = true; body = physics->world->CreateBody(&myBody); for (int j = -57; j < 13; j++) // TEMP for (int k = 0; k < 1; k++) createParticle((float)frmWidth, (float)frmHeight, b2Vec2(j,k + 0.25)); for (int j = 49; j < 100; j++) // TEMP for (int k = 0; k < 1; k++) createParticle((float)frmWidth, (float)frmHeight, b2Vec2(j,k + 0.25)); SetSpeed(80); }