Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // WHERE I'M CREATING BODIES
- //
- bool PhysicsComponent::Initialize()
- {
- auto tc = parent->GetComponent<TransformComponent>();
- physics = Game::GetPhysics();
- position.Set(tc->GetX(), tc->GetY());
- dimensions.Set(componentTable["w"].cast<float>(), componentTable["h"].cast<float>());
- offset.Set(componentTable["xOffset"].cast<float>(), componentTable["yOffset"].cast<float>());
- std::string type = componentTable["bodyType"];
- b2BodyType bodyType = b2_staticBody;;
- if (type.compare("Static") == 0)
- bodyType = b2_staticBody;
- else if (type.compare("Dynamic") == 0)
- bodyType = b2_dynamicBody;
- physics->PixelsToMeters(&position.x, &position.y);
- physics->PixelsToMeters(&dimensions.x, &dimensions.y);
- bodyDef = new b2BodyDef;
- bodyDef->position.Set(position.x, position.y);
- bodyDef->type = bodyType;
- body = physics->CreateBody(bodyDef);
- shape = new b2PolygonShape;
- shape->SetAsBox(dimensions.x/4, dimensions.y/4);
- body->CreateFixture(shape, 0);
- initialized = true;
- return true;
- }
- //
- // WHERE I'M RENDERING THE DEBUG OF THE BODIES
- //
- void PhysicsDebugRenderer::DrawSolidPolygon(const b2Vec2* vertices, int32 vertexCount, const b2Color& color)
- {
- Physics *p = Game::GetPhysics();
- b2Vec2 one = vertices[0];
- b2Vec2 two = vertices[1];
- b2Vec2 three = vertices[2];
- b2Vec2 four = vertices[3];
- float w = (vertices[1].x - vertices[0].x);
- float h = (vertices[3].y - vertices[0].y);
- float x = vertices[3].x;
- float y = vertices[3].y;
- p->MetersToPixels(&x, &y);
- p->MetersToPixels(&w, &h);
- p->ToSDL2Coordinates(&x, &y, w, h);
- Rectangle *rec = new Rectangle;
- rec->Initialize(0x00, 0x00, 0xFF, 100, w, h, 1);
- rectangles.push_back(rec);
- Game::GetGraphics()->Render(x, y, 9, 1, rec);
- }
- //
- // WHERE I'M MOVING MY BODY
- //
- void InputSystem::Process(const EntityPtr &entity)
- {
- auto rc = entity->GetComponent<RenderableComponent>();
- auto pc = entity->GetComponent<PhysicsComponent>();
- Direction direction = Direction::DOWN;
- bool moving = false;
- b2Vec2 velocity(0, 0);
- float speed = 5;
- const Uint8* currentKeyStates = SDL_GetKeyboardState(NULL);
- if (currentKeyStates[SDL_SCANCODE_UP])
- {
- velocity.y = -speed;
- moving = true;
- direction = Direction::UP;
- }
- else if (currentKeyStates[SDL_SCANCODE_DOWN])
- {
- velocity.y = speed;
- moving = true;
- direction = Direction::DOWN;
- }
- else if (currentKeyStates[SDL_SCANCODE_LEFT])
- {
- velocity.x = -speed;
- moving = true;
- direction = Direction::LEFT;
- }
- else if (currentKeyStates[SDL_SCANCODE_RIGHT])
- {
- velocity.x = speed;
- moving = true;
- direction = Direction::RIGHT;
- }
- else
- {
- direction = (Direction)rc->GetCurrentAnimation();
- rc->SetCurrentIdleClip(direction);
- }
- if (moving)
- {
- rc->SetCurrentAnimation(direction);
- }
- pc->GetBody()->SetLinearVelocity(velocity);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement