From 235a03ab4cc84fc8680a973f94a193dedbf360ce Mon Sep 17 00:00:00 2001 From: sippeangelo Date: Fri, 30 May 2014 01:04:09 +0200 Subject: [PATCH] Tank wheel pair! --- src/Components/WheelPair.h | 4 +- src/GUI/GameFrame.h | 17 +- src/GameWorld.cpp | 411 ++++++++++---------------------- src/GameWorld.h | 7 +- src/Systems/WheelPairSystem.cpp | 18 +- 5 files changed, 143 insertions(+), 314 deletions(-) diff --git a/src/Components/WheelPair.h b/src/Components/WheelPair.h index 973b5b5..24bc598 100644 --- a/src/Components/WheelPair.h +++ b/src/Components/WheelPair.h @@ -8,8 +8,8 @@ namespace Components struct WheelPair : Component { - EntityID FakeWheel1; - EntityID FakeWheel2; + EntityID FakeWheelFront; + EntityID FakeWheelBack; virtual WheelPair* Clone() const override { return new WheelPair(*this); } }; diff --git a/src/GUI/GameFrame.h b/src/GUI/GameFrame.h index 078a687..19f66ac 100644 --- a/src/GUI/GameFrame.h +++ b/src/GUI/GameFrame.h @@ -18,7 +18,7 @@ public: GameFrame(Frame* parent, std::string name) : Frame(parent, name) { - EVENT_SUBSCRIBE_MEMBER(m_EInputCommand, &GameFrame::OnCommand); + EVENT_SUBSCRIBE_MEMBER(m_EKeyUp, &GameFrame::OnKeyUp); m_World = std::make_shared(EventBroker, ResourceManager); auto worldFrame = new WorldFrame(this, "GameWorldFrame", m_World); @@ -40,22 +40,21 @@ public: } private: - EventRelay m_EInputCommand; + EventRelay m_EKeyUp; std::shared_ptr m_World; Viewport* vp1; Viewport* vp2; Viewport* m_FreeCamViewport; - bool OnCommand(const Events::InputCommand &event) + bool OnKeyUp(const Events::KeyUp &event) { - if (event.Command == "cam_vertical" || event.Command == "cam_horizontal" || event.Command == "cam_normal" || event.Command == "cam_lock") + if (event.KeyCode == GLFW_KEY_1) { - m_FreeCamViewport->Show(); - } - else - { - m_FreeCamViewport->Hide(); + if (m_FreeCamViewport->Hidden()) + m_FreeCamViewport->Show(); + else + m_FreeCamViewport->Hide(); } return true; diff --git a/src/GameWorld.cpp b/src/GameWorld.cpp index e19e914..6a7e51a 100755 --- a/src/GameWorld.cpp +++ b/src/GameWorld.cpp @@ -56,8 +56,8 @@ void GameWorld::Initialize() auto camera = CreateEntity(); { auto transform = AddComponent(camera); - transform->Position.z = 20.f; - transform->Position.y = 20.f; + transform->Position.z = 150.f; + transform->Position.y = 10.f; //transform->Orientation = glm::quat(glm::vec3(glm::pi() / 8.f, 0.f, 0.f)); auto cameraComp = AddComponent(camera); cameraComp->FarClip = 2000.f; @@ -126,7 +126,7 @@ void GameWorld::Initialize() EntityID tank1 = CreateTank(1); { auto transform = GetComponent(tank1); - transform->Position.z = 50.f; + transform->Position.z = 145.f; Events::SetViewportCamera e; e.CameraEntity = GetProperty(tank1, "Camera"); @@ -480,295 +480,17 @@ EntityID GameWorld::CreateTank(int playerID) #pragma region Wheels //Create wheels - float wheelOffset = 0.4f; - float springLength = 0.3f; - float suspensionStrength = 15.f; - -#pragma region Pair R1 - { - auto wheel1 = CreateEntity(tank); - { - auto transform = AddComponent(wheel1); - transform->Position = glm::vec3(1.68f, -0.83f - wheelOffset, -2.6f); - transform->Orientation = glm::angleAxis(glm::pi(), glm::vec3(0, 1, 0)); - auto model = AddComponent(wheel1); - model->ModelFile = "Models/Tank/Fix/WheelPhysics.obj"; - auto Wheel = AddComponent(wheel1); - Wheel->Hardpoint = transform->Position + glm::vec3(0.f, springLength, 0.f); - Wheel->AxleID = 0; - Wheel->Mass = 2000; - Wheel->Radius = 0.6f; - Wheel->Steering = true; - Wheel->SuspensionStrength = suspensionStrength; - Wheel->Friction = 3.f; - Wheel->ConnectedToHandbrake = true; - Wheel->TorqueRatio = 0.125f; - Wheel->Width = 0.6f; - { - auto shape = CreateEntity(wheel1); - auto shapetransform = AddComponent(shape); - shapetransform->Position = glm::vec3(0.f, 0.32f, 0.f) + transform->Position; - auto boxShape = AddComponent(shape); - boxShape->Width = 0.7f; - boxShape->Height = 0.34f; - boxShape->Depth = 0.7f; - CommitEntity(shape); - } - CommitEntity(wheel1); - } - auto wheel2 = CreateEntity(tank); - { - auto transform = AddComponent(wheel2); - transform->Position = glm::vec3(1.68f, -0.83f - wheelOffset, -0.83f); - transform->Orientation = glm::angleAxis(glm::pi(), glm::vec3(0, 1, 0)); - auto model = AddComponent(wheel2); - model->ModelFile = "Models/Tank/Fix/WheelPhysics.obj"; - auto Wheel = AddComponent(wheel2); - Wheel->Hardpoint = transform->Position + glm::vec3(0.f, springLength, 0.f); - Wheel->AxleID = 0; - Wheel->Mass = 2000; - Wheel->Radius = 0.6f; - Wheel->Steering = false; - Wheel->SuspensionStrength = suspensionStrength; - Wheel->Friction = 3.f; - Wheel->ConnectedToHandbrake = true; - Wheel->TorqueRatio = 0.125f; - Wheel->Width = 0.6f; - { - auto shape = CreateEntity(wheel2); - auto shapetransform = AddComponent(shape); - shapetransform->Position = glm::vec3(0.f, 0.32f, 0.f) + transform->Position; - auto boxShape = AddComponent(shape); - boxShape->Width = 0.7f; - boxShape->Height = 0.34f; - boxShape->Depth = 0.7f; - CommitEntity(shape); - } - CommitEntity(wheel2); - } - - auto wheelPair = CreateEntity(tank); - { - auto transform = AddComponent(wheelPair); - transform->Position = glm::vec3(1.68f, -0.83f - wheelOffset, -1.715f); - auto pair = AddComponent(wheelPair); - pair->FakeWheel1 = wheel1; - pair->FakeWheel2 = wheel2; - auto model = AddComponent(wheel2); - model->ModelFile = "Models/Tank/tankWheel.obj"; - } - } -#pragma endregion -#pragma region Pair L1 - { - auto wheel = CreateEntity(tank); - auto transform = AddComponent(wheel); - transform->Position = glm::vec3(-1.68f, -0.83f - wheelOffset, -2.6f); - transform->Orientation = glm::angleAxis(glm::pi(), glm::vec3(0, 1, 0)); - auto model = AddComponent(wheel); - model->ModelFile = "Models/Tank/Fix/WheelPhysics.obj"; - auto Wheel = AddComponent(wheel); - Wheel->Hardpoint = transform->Position + glm::vec3(0.f, springLength, 0.f); - Wheel->AxleID = 0; - Wheel->Mass = 2000; - Wheel->Radius = 0.6f; - Wheel->Steering = true; - Wheel->SuspensionStrength = suspensionStrength; - Wheel->Friction = 3.f; - Wheel->ConnectedToHandbrake = true; - Wheel->TorqueRatio = 0.125f; - Wheel->Width = 0.6f; - { - auto shape = CreateEntity(wheel); - auto shapetransform = AddComponent(shape); - shapetransform->Position = glm::vec3(0.f, 0.32f, 0.f) + transform->Position; - auto boxShape = AddComponent(shape); - boxShape->Width = 0.7f; - boxShape->Height = 0.34f; - boxShape->Depth = 0.7f; - CommitEntity(shape); - } - CommitEntity(wheel); - } - { - auto wheel = CreateEntity(tank); - auto transform = AddComponent(wheel); - transform->Position = glm::vec3(-1.68f, -0.83f - wheelOffset, -0.83f); - transform->Orientation = glm::angleAxis(glm::pi(), glm::vec3(0, 1, 0)); - auto model = AddComponent(wheel); - model->ModelFile = "Models/Tank/Fix/WheelPhysics.obj"; - auto Wheel = AddComponent(wheel); - Wheel->Hardpoint = transform->Position + glm::vec3(0.f, springLength, 0.f); - Wheel->AxleID = 0; - Wheel->Mass = 2000; - Wheel->Radius = 0.6f; - Wheel->Steering = true; - Wheel->SuspensionStrength = suspensionStrength; - Wheel->Friction = 3.f; - Wheel->ConnectedToHandbrake = true; - Wheel->TorqueRatio = 0.125f; - Wheel->Width = 0.6f; - { - auto shape = CreateEntity(wheel); - auto shapetransform = AddComponent(shape); - shapetransform->Position = glm::vec3(0.f, 0.32f, 0.f) + transform->Position; - auto boxShape = AddComponent(shape); - boxShape->Width = 0.7f; - boxShape->Height = 0.34f; - boxShape->Depth = 0.7f; - CommitEntity(shape); - } - CommitEntity(wheel); - } - -#pragma endregion -#pragma region Pair R2 - //Back - { - auto wheel = CreateEntity(tank); - auto transform = AddComponent(wheel); - transform->Position = glm::vec3(1.68f, -0.83f - wheelOffset, 1.f); - auto model = AddComponent(wheel); - model->ModelFile = "Models/Tank/Fix/WheelPhysics.obj"; - auto Wheel = AddComponent(wheel); - Wheel->Hardpoint = transform->Position + glm::vec3(0.f, springLength, 0.f); - Wheel->AxleID = 1; - Wheel->Mass = 2000; - Wheel->Radius = 0.6f; - Wheel->Steering = false; - Wheel->SuspensionStrength = suspensionStrength; - Wheel->Friction = 3.f; - Wheel->ConnectedToHandbrake = true; - Wheel->TorqueRatio = 0.125f; - Wheel->Width = 0.6f; - { - auto shape = CreateEntity(wheel); - auto shapetransform = AddComponent(shape); - shapetransform->Position = glm::vec3(0.f, 0.32f, 0.f) + transform->Position; - auto boxShape = AddComponent(shape); - boxShape->Width = 0.7f; - boxShape->Height = 0.34f; - boxShape->Depth = 0.7f; - CommitEntity(shape); - } - CommitEntity(wheel); - } - { - auto wheel = CreateEntity(tank); - auto transform = AddComponent(wheel); - transform->Position = glm::vec3(1.68f, -0.83f - wheelOffset, 2.95f); - auto model = AddComponent(wheel); - model->ModelFile = "Models/Tank/Fix/WheelPhysics.obj"; - auto Wheel = AddComponent(wheel); - Wheel->Hardpoint = transform->Position + glm::vec3(0.f, springLength, 0.f); - Wheel->AxleID = 1; - Wheel->Mass = 2000; - Wheel->Radius = 0.6f; - Wheel->Steering = false; - Wheel->SuspensionStrength = suspensionStrength; - Wheel->Friction = 3.f; - Wheel->ConnectedToHandbrake = true; - Wheel->TorqueRatio = 0.125f; - Wheel->Width = 0.6f; - { - auto shape = CreateEntity(wheel); - auto shapetransform = AddComponent(shape); - shapetransform->Position = glm::vec3(0.f, 0.32f, 0.f) + transform->Position; - auto boxShape = AddComponent(shape); - boxShape->Width = 0.7f; - boxShape->Height = 0.34f; - boxShape->Depth = 0.7f; - CommitEntity(shape); - } - CommitEntity(wheel); - - auto entity = CreateEntity(tank); - auto transformComponent = AddComponent(entity); - transformComponent->Position = glm::vec3(2, -1.7, 2.0); - transformComponent->Scale = glm::vec3(3, 3, 3); - transformComponent->Orientation = glm::angleAxis(glm::pi() / 2, glm::vec3(1, 0, 0)); - auto emitterComponent = AddComponent(entity); - emitterComponent->SpawnCount = 2; - emitterComponent->SpawnFrequency = 0.005; - emitterComponent->SpreadAngle = glm::pi(); - emitterComponent->UseGoalVelocity = false; - emitterComponent->LifeTime = 0.5; - //emitterComponent->AngularVelocitySpectrum.push_back(glm::pi() / 100); - emitterComponent->ScaleSpectrum.push_back(glm::vec3(0.05)); - CommitEntity(entity); - - auto particleEntity = CreateEntity(entity); - auto TEMP = AddComponent(particleEntity); - TEMP->Scale = glm::vec3(0); - auto spriteComponent = AddComponent(particleEntity); - spriteComponent->SpriteFile = "Models/Textures/Sprites/Dust.png"; - emitterComponent->ParticleTemplate = particleEntity; - - CommitEntity(particleEntity); - } -#pragma endregion -#pragma region Pair L2 - { - auto wheel = CreateEntity(tank); - auto transform = AddComponent(wheel); - transform->Position = glm::vec3(-1.68f, -0.83f - wheelOffset, 1.f); - transform->Orientation = glm::angleAxis(glm::pi(), glm::vec3(0, 1, 0)); - auto model = AddComponent(wheel); - model->ModelFile = "Models/Tank/Fix/WheelPhysics.obj"; - auto Wheel = AddComponent(wheel); - Wheel->Hardpoint = transform->Position + glm::vec3(0.f, springLength, 0.f); - Wheel->AxleID = 1; - Wheel->Mass = 2000; - Wheel->Radius = 0.6f; - Wheel->Steering = false; - Wheel->SuspensionStrength = suspensionStrength; - Wheel->Friction = 3.f; - Wheel->ConnectedToHandbrake = true; - Wheel->TorqueRatio = 0.125f; - Wheel->Width = 0.6f; - { - auto shape = CreateEntity(wheel); - auto shapetransform = AddComponent(shape); - shapetransform->Position = glm::vec3(0.f, 0.32f, 0.f) + transform->Position; - auto boxShape = AddComponent(shape); - boxShape->Width = 0.7f; - boxShape->Height = 0.34f; - boxShape->Depth = 0.7f; - CommitEntity(shape); - } - CommitEntity(wheel); - } - { - auto wheel = CreateEntity(tank); - auto transform = AddComponent(wheel); - transform->Position = glm::vec3(-1.68f, -0.83f - wheelOffset, 2.95f); - auto model = AddComponent(wheel); - model->ModelFile = "Models/Tank/Fix/WheelPhysics.obj"; - auto Wheel = AddComponent(wheel); - Wheel->Hardpoint = transform->Position + glm::vec3(0.f, springLength, 0.f); - Wheel->AxleID = 1; - Wheel->Mass = 2000; - Wheel->Radius = 0.6f; - Wheel->Steering = false; - Wheel->SuspensionStrength = suspensionStrength; - Wheel->Friction = 3.f; - Wheel->ConnectedToHandbrake = true; - Wheel->TorqueRatio = 0.125f; - Wheel->Width = 0.6f; - { - auto shape = CreateEntity(wheel); - auto shapetransform = AddComponent(shape); - shapetransform->Position = glm::vec3(0.f, 0.32f, 0.f) + transform->Position; - auto boxShape = AddComponent(shape); - boxShape->Width = 0.7f; - boxShape->Height = 0.34f; - boxShape->Depth = 0.7f; - CommitEntity(shape); - } - CommitEntity(wheel); -#pragma endregion + float wheelOffset = -0.83f; + const float suspensionStrength = 15.f; + const float springLength = 0.3f; + + AddTankWheelPair(tank, glm::vec3(1.68f, wheelOffset, -1.715f), 0, true); + AddTankWheelPair(tank, glm::vec3(-1.68f, wheelOffset, -1.715f), 0, true); + AddTankWheelPair(tank, glm::vec3(1.68f, wheelOffset, 2.375), 1, false); + AddTankWheelPair(tank, glm::vec3(-1.68f, wheelOffset, 2.375), 1, false); #pragma endregion + { auto entity = CreateEntity(tank); auto transformComponent = AddComponent(entity); transformComponent->Position = glm::vec3(-2, -1.7, 2.0); @@ -799,6 +521,115 @@ EntityID GameWorld::CreateTank(int playerID) return tank; } +void GameWorld::AddTankWheelPair(EntityID tankEntity, glm::vec3 position, int axleID, bool front) +{ + const float separation = 1.77f; + const float suspensionStrength = 15.f; + const float springLength = 0.3f; + + bool steering = front; + + auto wheelFront = CreateEntity(tankEntity); + { + auto transform = AddComponent(wheelFront); + transform->Position = position - glm::vec3(0, 0, separation / 2.f); // glm::vec3(1.68f, -0.83f - wheelOffset, -0.83f); + //transform->Orientation = glm::angleAxis(glm::pi(), glm::vec3(0, 1, 0)); +#ifdef DEBUG + auto model = AddComponent(wheelFront); + model->ModelFile = "Models/Tank/Fix/WheelPhysics.obj"; +#endif + auto Wheel = AddComponent(wheelFront); + Wheel->Hardpoint = transform->Position + glm::vec3(0.f, springLength, 0.f); + Wheel->AxleID = axleID; + Wheel->Mass = 2000; + Wheel->Radius = 0.6f; + Wheel->Steering = steering; + Wheel->SuspensionStrength = suspensionStrength; + Wheel->Friction = 3.f; + Wheel->ConnectedToHandbrake = true; + Wheel->TorqueRatio = 0.125f; + Wheel->Width = 0.6f; + { + auto shape = CreateEntity(wheelFront); + auto shapetransform = AddComponent(shape); + shapetransform->Position = glm::vec3(0.f, 0.32f, 0.f) + transform->Position; + auto boxShape = AddComponent(shape); + boxShape->Width = 0.7f; + boxShape->Height = 0.34f; + boxShape->Depth = 0.7f; + CommitEntity(shape); + } + CommitEntity(wheelFront); + } + + auto wheelBack = CreateEntity(tankEntity); + { + auto transform = AddComponent(wheelBack); + transform->Position = position + glm::vec3(0, 0, separation / 2.f); // glm::vec3(1.68f, -0.83f - wheelOffset, -2.6f); + //transform->Orientation = glm::angleAxis(glm::pi(), glm::vec3(0, 1, 0)); +#ifdef DEBUG + auto model = AddComponent(wheelBack); + model->ModelFile = "Models/Tank/Fix/WheelPhysics.obj"; +#endif + auto Wheel = AddComponent(wheelBack); + Wheel->Hardpoint = transform->Position + glm::vec3(0.f, springLength, 0.f); + Wheel->AxleID = axleID; + Wheel->Mass = 2000; + Wheel->Radius = 0.6f; + Wheel->Steering = steering; + Wheel->SuspensionStrength = suspensionStrength; + Wheel->Friction = 3.f; + Wheel->ConnectedToHandbrake = true; + Wheel->TorqueRatio = 0.125f; + Wheel->Width = 0.6f; + { + auto shape = CreateEntity(wheelBack); + auto shapetransform = AddComponent(shape); + shapetransform->Position = glm::vec3(0.f, 0.32f, 0.f) + transform->Position; + auto boxShape = AddComponent(shape); + boxShape->Width = 0.7f; + boxShape->Height = 0.34f; + boxShape->Depth = 0.7f; + CommitEntity(shape); + } + CommitEntity(wheelBack); + } + + auto wheelPair = CreateEntity(tankEntity); + { + { + auto transform = AddComponent(wheelPair); + transform->Position = position; + //transform->Orientation = glm::quat(glm::vec3(0, glm::pi() / 4.f, 0)); + + auto pair = AddComponent(wheelPair); + pair->FakeWheelFront = wheelFront; + pair->FakeWheelBack = wheelBack; + } + + auto modelEntity = CreateEntity(wheelPair); + { + auto transform = AddComponent(modelEntity); + //transform->Position = glm::vec3(0.f, 0.35f, 0.f); + if (front) + { + transform->Position = glm::vec3(0.f, 0.03f, 0.f); + transform->Orientation = glm::quat(glm::vec3(0, glm::pi(), 0)); + } + else + { + transform->Position = glm::vec3(0.f, 0.17f, 0.f); + transform->Scale = glm::vec3(1.f, 1.2f, 1.2f); + } + + auto model = AddComponent(modelEntity); + model->ModelFile = "Models/Tank/tankWheel.obj"; + } + CommitEntity(modelEntity); + } + CommitEntity(wheelPair); +} + EntityID GameWorld::CreateJeep(int playerID) { auto jeep = CreateEntity(); diff --git a/src/GameWorld.h b/src/GameWorld.h index faf7ec4..aae2472 100755 --- a/src/GameWorld.h +++ b/src/GameWorld.h @@ -59,9 +59,6 @@ public: void Initialize(); - EntityID CreateTank(int playerID); - EntityID CreateJeep(int playerID); - void RegisterSystems() override; void AddSystems() override; void RegisterComponents() override; @@ -73,6 +70,10 @@ private: void BindMouseButton(int button, std::string command, float value); void BindGamepadAxis(Gamepad::Axis axis, std::string command, float value); void BindGamepadButton(Gamepad::Button button, std::string command, float value); + + EntityID CreateTank(int playerID); + void AddTankWheelPair(EntityID tankEntity, glm::vec3 position, int axleID, bool steering); + EntityID CreateJeep(int playerID); }; #endif // GameWorld_h__ diff --git a/src/Systems/WheelPairSystem.cpp b/src/Systems/WheelPairSystem.cpp index f0cea10..b8b6c4a 100644 --- a/src/Systems/WheelPairSystem.cpp +++ b/src/Systems/WheelPairSystem.cpp @@ -16,20 +16,18 @@ void Systems::WheelPairSystem::UpdateEntity(double dt, EntityID entity, EntityID auto transform = m_World->GetComponent(entity); if (!transform) return; - auto transformFake1 = m_World->GetComponent(wheelPair->FakeWheel1); - if (!transformFake1) + auto transformFakeFront = m_World->GetComponent(wheelPair->FakeWheelFront); + if (!transformFakeFront) return; - auto transformFake2 = m_World->GetComponent(wheelPair->FakeWheel2); - if (!transformFake2) + auto transformFakeBack = m_World->GetComponent(wheelPair->FakeWheelBack); + if (!transformFakeBack) return; - glm::vec3 thing = transformFake2->Position - transformFake1->Position; - glm::vec3 forward = glm::normalize(glm::vec3(thing.x, 0, thing.z)); - glm::vec3 up = glm::vec3(0, 1, 0); + glm::vec3 thing = transformFakeBack->Position - transformFakeFront->Position; - float angle = glm::acos(glm::dot(glm::normalize(thing), forward)); - float height = (thing / 2.f).y; + float height = ((transformFakeFront->Position + transformFakeBack->Position) / 2.f).y; + float angle = std::atan2f(thing.y, thing.z); transform->Position.y = height; - //transform->Orientation = glm::quat(glm::vec3()) + transform->Orientation = glm::quat(glm::eulerAngles(transform->Orientation) * glm::vec3(0, 1, 1) + glm::vec3(-angle, 0, 0)); } \ No newline at end of file