Advertisement
Guest User

Untitled

a guest
Mar 12th, 2016
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.83 KB | None | 0 0
  1.  
  2. //
  3. // WHERE I'M CREATING BODIES
  4. //
  5. bool PhysicsComponent::Initialize()
  6. {
  7.     auto tc = parent->GetComponent<TransformComponent>();
  8.     physics = Game::GetPhysics();
  9.  
  10.     position.Set(tc->GetX(), tc->GetY());
  11.     dimensions.Set(componentTable["w"].cast<float>(), componentTable["h"].cast<float>());
  12.     offset.Set(componentTable["xOffset"].cast<float>(), componentTable["yOffset"].cast<float>());
  13.  
  14.     std::string type = componentTable["bodyType"];
  15.     b2BodyType bodyType = b2_staticBody;;
  16.    
  17.  
  18.     if (type.compare("Static") == 0)
  19.         bodyType = b2_staticBody;
  20.     else if (type.compare("Dynamic") == 0)
  21.         bodyType = b2_dynamicBody;
  22.  
  23.     physics->PixelsToMeters(&position.x, &position.y);
  24.     physics->PixelsToMeters(&dimensions.x, &dimensions.y);
  25.  
  26.     bodyDef = new b2BodyDef;
  27.     bodyDef->position.Set(position.x, position.y);
  28.     bodyDef->type = bodyType;
  29.     body = physics->CreateBody(bodyDef);
  30.  
  31.     shape = new b2PolygonShape;
  32.     shape->SetAsBox(dimensions.x/4, dimensions.y/4);
  33.     body->CreateFixture(shape, 0);
  34.  
  35.  
  36.     initialized = true;
  37.     return true;
  38. }
  39.  
  40.  
  41. //
  42. // WHERE I'M RENDERING THE DEBUG OF THE BODIES
  43. //
  44. void PhysicsDebugRenderer::DrawSolidPolygon(const b2Vec2* vertices, int32 vertexCount, const b2Color& color)
  45. {
  46.     Physics *p = Game::GetPhysics();
  47.  
  48.     b2Vec2 one = vertices[0];
  49.     b2Vec2 two = vertices[1];
  50.     b2Vec2 three = vertices[2];
  51.     b2Vec2 four = vertices[3];
  52.  
  53.     float w = (vertices[1].x - vertices[0].x);
  54.     float h = (vertices[3].y - vertices[0].y);
  55.     float x = vertices[3].x;
  56.     float y = vertices[3].y;
  57.  
  58.     p->MetersToPixels(&x, &y);
  59.     p->MetersToPixels(&w, &h);
  60.  
  61.     p->ToSDL2Coordinates(&x, &y, w, h);
  62.  
  63.  
  64.     Rectangle *rec = new Rectangle;
  65.     rec->Initialize(0x00, 0x00, 0xFF, 100, w, h, 1);
  66.     rectangles.push_back(rec);
  67.     Game::GetGraphics()->Render(x, y, 9, 1, rec);
  68. }
  69.  
  70. //
  71. // WHERE I'M MOVING MY BODY
  72. //
  73. void InputSystem::Process(const EntityPtr &entity)
  74. {
  75.     auto rc = entity->GetComponent<RenderableComponent>();
  76.     auto pc = entity->GetComponent<PhysicsComponent>();
  77.  
  78.  
  79.     Direction direction = Direction::DOWN;
  80.     bool moving = false;
  81.     b2Vec2 velocity(0, 0);
  82.     float speed = 5;
  83.  
  84.     const Uint8* currentKeyStates = SDL_GetKeyboardState(NULL);
  85.     if (currentKeyStates[SDL_SCANCODE_UP])
  86.     {
  87.         velocity.y = -speed;
  88.         moving = true;
  89.         direction = Direction::UP;
  90.     }
  91.     else if (currentKeyStates[SDL_SCANCODE_DOWN])
  92.     {
  93.         velocity.y = speed;
  94.         moving = true;
  95.         direction = Direction::DOWN;
  96.     }
  97.     else if (currentKeyStates[SDL_SCANCODE_LEFT])
  98.     {
  99.         velocity.x = -speed;
  100.         moving = true;
  101.         direction = Direction::LEFT;
  102.     }
  103.     else if (currentKeyStates[SDL_SCANCODE_RIGHT])
  104.     {
  105.         velocity.x = speed;
  106.         moving = true;
  107.         direction = Direction::RIGHT;
  108.     }
  109.     else
  110.     {
  111.         direction = (Direction)rc->GetCurrentAnimation();
  112.         rc->SetCurrentIdleClip(direction);
  113.     }
  114.  
  115.     if (moving)
  116.     {
  117.         rc->SetCurrentAnimation(direction);
  118.     }
  119.  
  120.     pc->GetBody()->SetLinearVelocity(velocity);
  121.  
  122. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement