void GameWorld::DoCollisionResponse(float dt) { // safe checking if the platform's fixture is deleted before this function is called if(!platform_->GetBody()->GetFixtureList()) return; // fetch the shape from the platform body's fixture b2EdgeShape* platform_shape = (b2EdgeShape*)platform_->GetBody()->GetFixtureList()->GetShape(); // get the difference vector b2Vec2 diff = platform_shape->m_vertex2 - platform_shape->m_vertex1; // calculate magnitude of the impulse based on the length of the platform float distance = WORLD_TO_SCREEN(diff.Length()) / MAX_PLATFORM_WIDTH; float magnitude = PLATFORM_IMPULSE * (1.5f - (distance > 1.0f ? 1.0f : distance)); // impulse wil be in the direction of the contact normal b2Vec2 impulse = contact_normal_; // ensure impulse throws the clown up not down impulse.y = impulse.y < 0 ? impulse.y * -1 : impulse.y; impulse *= magnitude; // apply a linear impulse to the center of the clown's body clown_->GetBody()->ApplyLinearImpulse(impulse, clown_->GetBody()->GetWorldCenter()); // collision response finished, now remove the platform RemovePlatform(); }
StarPhysicsNode::StarPhysicsNode(const char* f, CCPoint p, AroundMeLayer* layer) : PhysicsNode(layer) { m_image = QString(f); b2BodyDef bd; bd.type = b2_dynamicBody; bd.position.Set(p.x / PTM_RATIO, p.y / PTM_RATIO); bd.userData = this; b2Body *body = m_world->CreateBody(&bd); b2Vec2 vertices[3]; { b2PolygonShape triangle; vertices[0] = b2Vec2(-3.0f, 0.78f); vertices[1] = b2Vec2(-1.98f, -0.84f); vertices[2] = b2Vec2(-1.2f, 1.56f); triangle.Set(vertices, 3); b2FixtureDef fixtureDef; fixtureDef.shape = ▵ fixtureDef.density = 1.0f; fixtureDef.restitution = 0.5; fixtureDef.friction = 0.3f; fixtureDef.filter.categoryBits = CHEERIOS; body->CreateFixture(&fixtureDef); } { b2PolygonShape triangle; vertices[0] = b2Vec2(-1.2f, 1.56f); vertices[1] = b2Vec2(1.2f, 1.56f); vertices[2] = b2Vec2(0.0f, 3.0f); triangle.Set(vertices, 3); b2FixtureDef fixtureDef; fixtureDef.shape = ▵ fixtureDef.density = 1.0f; fixtureDef.restitution = 0.5; fixtureDef.friction = 0.3f; fixtureDef.filter.categoryBits = CHEERIOS; body->CreateFixture(&fixtureDef); } { b2PolygonShape triangle; vertices[0] = b2Vec2(1.2f, 1.56f); vertices[1] = b2Vec2(1.98f, -0.84f); vertices[2] = b2Vec2(3.0f, 0.78f); triangle.Set(vertices, 3); b2FixtureDef fixtureDef; fixtureDef.shape = ▵ fixtureDef.density = 1.0f; fixtureDef.restitution = 0.5; fixtureDef.friction = 0.3f; fixtureDef.filter.categoryBits = CHEERIOS; body->CreateFixture(&fixtureDef); } { b2PolygonShape triangle; vertices[0] = b2Vec2(1.98f, -0.84f); vertices[1] = b2Vec2(0.0f, -2.22f); vertices[2] = b2Vec2(1.8f, -3.0f); triangle.Set(vertices, 3); b2FixtureDef fixtureDef; fixtureDef.shape = ▵ fixtureDef.density = 1.0f; fixtureDef.restitution = 0.5; fixtureDef.friction = 0.3f; fixtureDef.filter.categoryBits = CHEERIOS; body->CreateFixture(&fixtureDef); } { b2PolygonShape triangle; vertices[0] = b2Vec2(0.0f, -2.22f); vertices[1] = b2Vec2(-1.98f, -0.84f); vertices[2] = b2Vec2(-1.8f, -3.0f); triangle.Set(vertices, 3); b2FixtureDef fixtureDef; fixtureDef.shape = ▵ fixtureDef.density = 1.0f; fixtureDef.restitution = 0.5; fixtureDef.friction = 0.3f; fixtureDef.filter.categoryBits = CHEERIOS; body->CreateFixture(&fixtureDef); } { b2Vec2 vertices[5]; b2PolygonShape pentagon; vertices[0] = b2Vec2(-1.98f, -0.84f); vertices[1] = b2Vec2(0.0f, -2.22f); vertices[2] = b2Vec2(1.98f, -0.84f); vertices[3] = b2Vec2(1.2f, 1.56f); vertices[4] = b2Vec2(-1.2f, 1.56f); pentagon.Set(vertices, 5); b2FixtureDef fixtureDef; fixtureDef.shape = &pentagon; fixtureDef.density = 1.0f; fixtureDef.restitution = 0.5; fixtureDef.friction = 0.3f; fixtureDef.filter.categoryBits = CHEERIOS; body->CreateFixture(&fixtureDef); } setContentSize(CCSize(WORLD_TO_SCREEN(3*2), WORLD_TO_SCREEN(3*2))); setScale(WORLD_TO_SCREEN(3) / 100); setPosition(ccp(p.x, p.y)); setB2Body(body); m_sprite = CCSprite::create(f); addChild(m_sprite); }
//============================================================================ void CPhysicsBody::InitialComponent(b2World *poWorld, b2Vec2 *poGravity, eShapeTypes eType, Point *poPosition, eBodyTypes eBody, float sngScale, std::string strImage) { // Declare Variables //-------------------------------------------------- m_poWorld = poWorld; m_poGravity = poGravity; m_poSprite = Sprite::create(strImage); m_poSprite->setPosition(*poPosition); m_poSprite->setScale(sngScale); switch(eBody) { case STATIC_BODY: m_oBodyDef.type = b2_staticBody; break; case DYNAMIC_BODY: m_oBodyDef.type = b2_dynamicBody; break; case UN_ASSIGNED_BODY: default: m_oBodyDef.type = b2_staticBody; break; } m_oBodyDef.position = b2Vec2(poPosition->x, poPosition->y); m_oBodyDef.userData = m_poSprite; m_poBody = m_poWorld->CreateBody(&m_oBodyDef); switch(eType) { case CIRCLE_SHAPE: { // Configure Circle Object //---------------------------------------------- b2CircleShape oCircle; oCircle.m_radius = WORLD_TO_SCREEN(DEFAULT_RADIUS_VALUE); m_oFixtureDef.shape = &oCircle; m_poShape = &oCircle; break; } case EDGE_SHAPE: { // Configure edge Object //---------------------------------------------- b2EdgeShape oEdge; oEdge.Set(b2Vec2(0,0), b2Vec2(WORLD_TO_SCREEN(DEFAULT_RADIUS_VALUE), 0)); m_oFixtureDef.shape = &oEdge; m_poShape = &oEdge; break; } case CHAIN_SHAPE: { // Configure chain Object //---------------------------------------------- b2ChainShape oChain; m_oFixtureDef.shape = &oChain; m_poShape = &oChain; break; } case POLYGON_SHAPE: { // Configure Polygon Object //---------------------------------------------- b2PolygonShape oPolygon; m_oFixtureDef.shape = &oPolygon; m_poShape = &oPolygon; break; } case UN_ASSIGNED_SHAPE: default: { // Configure Default Object //---------------------------------------------- b2PolygonShape oPolygon; m_oFixtureDef.shape = &oPolygon; m_poShape = &oPolygon; break; } } m_oFixtureDef.density = DEFAULT_DENSITY; m_oFixtureDef.friction = DEFAULT_FICTION; m_oFixtureDef.restitution = DEFAULT_RESTITUTION; m_poBody->CreateFixture(&m_oFixtureDef); //-------------------------------------------------- } // End of InitialComponent Method