bool ModulePhysics::Start() { LOG("Creating Physics 2D environment"); // TODO 2: Create a private variable for the world // - You need to send it a default gravity // - You need init the world in the constructor // - Remember to destroy the world after using it b2Vec2 gravity(0.0f, -10.0f); world = new b2World(gravity); // TODO 4: Create a a big static circle as "ground" b2BodyDef bodyDef; bodyDef.type = b2_staticBody; bodyDef.position.Set(PixelToMeter(500), PixelToMeter(400)); b2Body* body = world->CreateBody(&bodyDef); b2CircleShape shape; shape.m_radius = PixelToMeter(30); b2FixtureDef fixture; fixture.shape = &shape; body->CreateFixture(&fixture); return true; }
void SmokeParticle::CreateBody(b2Vec2 center, float scale, float _angle, b2Vec2 linVel, float angleVel) { this->angle = _angle; this->scaleX = scale; this->scaleY = scale; this->posX = center.x; this->posY = center.y; //this->drawSprite = false; this->drawDebug = false; // Body Def b2BodyDef BodyDef; BodyDef.type = b2BodyType::b2_dynamicBody; BodyDef.angle = _angle; BodyDef.fixedRotation = true; // rotation not necessary BodyDef.bullet = false; // prevent tunneling at high speed BodyDef.linearDamping = 1.0f; // drag due to moving through air BodyDef.gravityScale = 0.0f; // ignore gravity BodyDef.position.Set(PixelToMeter(center.x), PixelToMeter(center.y)); // start at blast center BodyDef.linearVelocity.Set((linVel.x), (linVel.y)); BodyDef.angularVelocity = angleVel; BodyDef.angularDamping = 1.0f; // Shape b2CircleShape Circle; // relative - I keep forgetting Circle.m_p.x = PixelToMeter(0.0f); Circle.m_p.y = PixelToMeter(0.0f); // Might want to make it very small 0.05 ish Circle.m_radius = PixelToMeter(10 * 0.5f); // Body b2Body *pParticleBody = World::getWorld()->CreateBody(&BodyDef); // Fixture b2FixtureDef FixtureDef; FixtureDef.shape = &Circle; FixtureDef.density = 0.0f; FixtureDef.friction = 0.0f; FixtureDef.restitution = 0.0f; FixtureDef.userData = this; FixtureDef.isSensor = true; FixtureDef.filter.groupIndex = -1; // particles should not collide with each other pParticleBody->CreateFixture(&FixtureDef); // Body points to GameObject pParticleBody->SetUserData(this); // GameObject points to Body this->pBody = pParticleBody; }
StoneBlock * StoneBlock::Create(GameObjectName::Name gObjName, ImageName::Name imageName, Rect rect,float _angle, b2BodyType type) { GameObject *pObj = GameObjectMan::Find(GameObjectName::Blocks_Sort); // input validation assert(pObj); GameSortBucket *pSort = (GameSortBucket *)pObj; GraphicsObject_Sprite *pSprite = new GraphicsObject_Sprite(imageName, rect); StoneBlock *pBlock = new StoneBlock(gObjName, pSprite, pSort); pBlock->angle = _angle; b2BodyDef BodyDef; BodyDef.type = type; BodyDef.position.Set(PixelToMeter(pBlock->posX* pBlock->scaleFactor), PixelToMeter(pBlock->posY*pBlock->scaleFactor)); BodyDef.angle = pBlock->angle; BodyDef.angularDamping = 2.5f; /*BodyDef.linearDamping = 5.0f;*/ // Shape b2PolygonShape shape; shape.SetAsBox(PixelToMeter(pBlock->origWidth * 0.5f * pBlock->scaleFactor), PixelToMeter(pBlock->origHeight * 0.5f * pBlock->scaleFactor)); // Body b2Body *pBlockBody = World::getWorld()->CreateBody(&BodyDef); // Fixture b2FixtureDef FixtureDef; FixtureDef.shape = &shape; FixtureDef.density = 15; FixtureDef.friction = 2.5; FixtureDef.restitution = 0; FixtureDef.userData = pBlock; pBlockBody->CreateFixture(&FixtureDef); // Body points to GameObject pBlockBody->SetUserData(pBlock); // GameObject points to Body pBlock->pBody = pBlockBody; return pBlock; }
StoneBall *StoneBall::Create(GameObjectName::Name gObjName, ImageName::Name imageName, Rect rect, b2BodyType type) { GameObject *pObj = GameObjectMan::Find(GameObjectName::Blocks_Sort); // input validation assert(pObj); GameSortBucket *pSort = (GameSortBucket *)pObj; GraphicsObject_Sprite *pSprite = new GraphicsObject_Sprite(imageName, rect); StoneBall *pBlock = new StoneBall(gObjName, pSprite, pSort); // Body Def b2BodyDef BodyDef; BodyDef.type = type; BodyDef.position.Set(PixelToMeter(pBlock->posX* pBlock->scaleFactor), PixelToMeter(pBlock->posY*pBlock->scaleFactor)); BodyDef.angle = pBlock->angle; // Shape b2CircleShape shape; // relative - I keep forgetting shape.m_p.x = PixelToMeter(0.0f); shape.m_p.y = PixelToMeter(0.0f); shape.m_radius = PixelToMeter(pBlock->origWidth * 0.5f); // Body b2Body *pBlockBody = World::getWorld()->CreateBody(&BodyDef); // Fixture b2FixtureDef FixtureDef; FixtureDef.shape = &shape; FixtureDef.density = 20.0f; FixtureDef.friction = 0.5f; FixtureDef.restitution = 0.1f; FixtureDef.userData = pBlock; pBlockBody->CreateFixture(&FixtureDef); // Body points to GameObject pBlockBody->SetUserData(pBlock); // GameObject points to Body pBlock->pBody = pBlockBody; return pBlock; }
update_status ModulePhysics::PostUpdate() { // TODO 5: On space bar press, create a circle on mouse position // - You need to transform the position / radius if (App->input->GetKey(SDL_SCANCODE_SPACE) == KEY_DOWN) { b2BodyDef bodyDef; bodyDef.type = b2_staticBody; bodyDef.position.Set(PixelToMeter(App->input->GetMouseX()), PixelToMeter(App->input->GetMouseY())); b2Body* body = world->CreateBody(&bodyDef); b2CircleShape shape; shape.m_radius = PixelToMeter(30); b2FixtureDef fixture; fixture.shape = &shape; body->CreateFixture(&fixture); } if(App->input->GetKey(SDL_SCANCODE_F1) == KEY_DOWN) debug = !debug; if(!debug) return UPDATE_CONTINUE; // Bonus code: this will iterate all objects in the world and draw the circles // You need to provide your own macro to translate meters to pixels for(b2Body* b = world->GetBodyList(); b; b = b->GetNext()) { for(b2Fixture* f = b->GetFixtureList(); f; f = f->GetNext()) { switch(f->GetType()) { case b2Shape::e_circle: { b2CircleShape* shape = (b2CircleShape*)f->GetShape(); b2Vec2 pos = f->GetBody()->GetPosition(); App->renderer->DrawCircle(MeterToPixel(pos.x), MeterToPixel(pos.y), MeterToPixel(shape->m_radius), 255, 255, 255); } break; // You will have to add more cases to draw boxes, edges, and polygons ... } } } return UPDATE_CONTINUE; }
bool ModulePhysics::Start() { LOG("Creating Physics 2D environment"); // TODO 2: Create a private variable for the world b2World world = new b2World(b2Vec2(0, -10)); b2Vec2 gravity(GRAVITY1, GRAVITY2); // - You need to send it a default gravity // - You need init the world in the constructor // - Remember to destroy the world after using it // TODO 4: Create a a big static circle as "ground" //Creem un body i li posem tots els parametres int radius = 225; int x = 525; int y = 400; b2BodyDef bodydef; b2Body* body = world->CreateBody(&bodydef); bodydef.type = b2_staticBody; bodydef.position.Set(PixelToMeter(x), PixelToMeter(y)); //creem la shape b2CircleShape shape; shape.m_radius = PixelToMeter(radius); //creem la fixture b2FixtureDef fixture; fixture.shape = &shape; body->CreateFixture(&fixture); return true; }
void RedBird::CreateBody(b2Vec2 center, float scale, float angle, b2Vec2 linVel, float angleVel) { center; scale; angle; linVel; angleVel; b2BodyDef BodyDef; BodyDef.type = b2BodyType::b2_dynamicBody; BodyDef.position.Set(PixelToMeter(this->posX), PixelToMeter(this->posY)); BodyDef.angle = this->angle; BodyDef.active = false; BodyDef.angularDamping = 1.5f; BodyDef.userData = this; // Shape b2CircleShape Circle; // relative - I keep forgetting Circle.m_p.x = PixelToMeter(1.20f); Circle.m_p.y = PixelToMeter(0.40f); Circle.m_radius = PixelToMeter(this->origWidth * 0.5f); // Body b2Body *pBirdBody = World::getWorld()->CreateBody(&BodyDef); // Fixture b2FixtureDef FixtureDef; FixtureDef.shape = &Circle; FixtureDef.density = 5.0f; FixtureDef.friction = 0.3f; FixtureDef.restitution = 0.50f; FixtureDef.userData = this; //Later Add FixtureDef.filter.categoryBits = 0x0008; pBirdBody->CreateFixture(&FixtureDef); // GameObject points to Body this->pBody = pBirdBody; this->debugColor = Color::Type::Red; }
void MouseTest( GLFWwindow* window, GameObjectBird *pBird, ScreenLine* pLine1, ScreenLine* pLine2, std::list<GameObject *> gameObjectList ) { // Quick and dirty test, if these work the rest do. // --> try move the mouse inside the window, click right, click left if (!pBird) return; if (pBird->bState==SLING) { pBird->pBody->SetActive(false); b2Vec2 newPos( PixelToMeter((float)slingX), PixelToMeter((float)slingY) ); pBird->pBody->SetTransform( newPos, 0.0f ); } double xpos; double ypos; static float lastXPos; static float lastYPos; // get mouse position glfwGetCursorPos( window, &xpos, &ypos); Camera *pCam = Camera::Instance(); pCam; // correct for origin double t = ypos / 600.0; ypos = 600.0 + t * (-600.0); MouseState mState = NONE; PositionState pState = UNKNOWN; GameObject* b = pBird; Matrix viewMatrix = pCam->getViewMatrix(); Matrix projMatrix = pCam->getProjMatrix(); Matrix worldMatrix = b->pGameSprite->returnWorld(); Vect vout = Vect(0.0f,0.0f,0.0f) * worldMatrix * viewMatrix * projMatrix; float zoom = vout[w]; vout[x] = vout[x]/vout[w]; vout[y] = vout[y]/vout[w]; float X = (vout[x]+1.0f)*(pCam->viewport_width/2.0f); float Y= (vout[y]+1.0f)*(pCam->viewport_height/2.0f); Vect birdPos(pBird->pos.x, pBird->pos.y,0.0f); Vect mousePos((float ) xpos,(float ) ypos, 0.0f); Vect local( X, Y, 0.0f); Vect Dist = mousePos - local; if ( Dist.mag() < (10.0f / zoom) ) { pState = INSIDE; } else { pState = OUTSIDE; } //printf("%f - %f", Dist[x],Dist[y] ); //printf(" | %f - %f", X, Y ); //xpos = xpos + Dist[x]*zoom; //ypos = ypos + Dist[y]*zoom; //printf("%f - %f ",xpos, ypos); mState = NONE; if( glfwGetMouseButton (window, GLFW_MOUSE_BUTTON_RIGHT ) == GLFW_PRESS) { mState = RIGHT; } if( glfwGetMouseButton (window, GLFW_MOUSE_BUTTON_LEFT ) == GLFW_PRESS) { mState = LEFT; } if (mState == LEFT && pBird->bState == NORMAL1) { pBird->bState = NORMAL2; pBird->onMouseAction(); } // Enter MOVING state if( mState == LEFT && pState == INSIDE) { ; pBird->bState = MOVING; pBird->pBody->SetActive(false); } // small sublty here, once moving, left dictates mode if ( pBird->bState == MOVING) { if( mState == LEFT )//this drags the bird around { /*b2Vec2 newPos( PixelToMeter((float)xpos), PixelToMeter((float)ypos) ); pBird->pBody->SetTransform( newPos, 0.0f ); pLine1->posB=B; pLine2->posB=B;*/ b2Vec2 slingPos(PixelToMeter((float)slingX), PixelToMeter((float)slingY)); b2Vec2 newPos(PixelToMeter((float)pBird->pos.x+Dist[x]*zoom), PixelToMeter((float)pBird->pos.y+Dist[y]*zoom)); b2Vec2 check(PixelToMeter((float)pBird->pos.x+Dist[x]*zoom-slingX), PixelToMeter((float)pBird->pos.y+Dist[y]*zoom - slingY)); if (check.Length() > 2) { check.Normalize(); newPos = slingPos + 2*check; } else { newPos.Set( PixelToMeter((float)pBird->pos.x+Dist[x]*zoom), PixelToMeter((float)pBird->pos.y+Dist[y]*zoom) ); } //printf("%f - %f ",newPos.x, newPos.y); pBird->pBody->SetTransform( newPos, 0.0f ); Vect2D B(MeterToPixel((float)newPos.x), MeterToPixel((float)newPos.y)); pLine1->posB=B; pLine2->posB=B; } else//this fires bird from the slingshot { b2Vec2 slingshot(PixelToMeter(slingX-pBird->pos.x), PixelToMeter(slingY-pBird->pos.y)); pBird->bState = NORMAL1; pBird->pBody->SetActive(true); float32 mag = slingshot.Length(); if (mag>2) mag=2; slingshot.Normalize(); if (pBird->pBody->GetMass()<10) mag*=20; else mag*=200; b2Vec2 vel; vel = mag * slingshot; pBird->pBody->ApplyLinearImpulse( vel, pBird->pBody->GetWorldCenter(), true ); pBird->pBody->SetActive(true); Vect2D B(slingX, slingY) ; pLine1->posB=B; pLine2->posB=B; pBird->launch(); AzulCore::clearTrails(); std::list< GameObject *>::iterator it=gameObjectList.begin(); while( it!=gameObjectList.end() ) { GameObject *pGameObj = *it++; pGameObj->damageActive=true; } } } if (pBird->bState==SLING) { pBird->pBody->SetActive(false); b2Vec2 newPos( PixelToMeter((float)slingX), PixelToMeter((float)slingY) ); pBird->pBody->SetTransform( newPos, 0.0f ); } if (pBird->bState==SLING||pBird->bState==MOVING) { AzulCore::setTargetAndSpeed((slingX), (slingY), -0.2f,0.01f); } else { AzulCore::setTargetAndSpeed(pBird->pos.x, pBird->pos.y, 0.1f,10.0f); } }