From 02243beceec2a5469965019c313e931e195c6723 Mon Sep 17 00:00:00 2001 From: ViktorLjung Date: Sun, 18 May 2014 23:54:01 +0200 Subject: [PATCH] Recoil while firing, clamp inputs, merge Helicopter --- src/Events/ApplyPointImpulse.h | 18 +++++++++++++++ src/GameWorld.cpp | 4 ++-- src/InputManager.cpp | 13 +++++++++++ src/Systems/HelicopterSteeringSystem.cpp | 6 ++--- src/Systems/PhysicsSystem.cpp | 12 +++++++++- src/Systems/PhysicsSystem.h | 3 +++ src/Systems/TankSteeringSystem.cpp | 22 ++++++++++++++++++- src/Systems/TankSteeringSystem.h | 3 +++ vs11/Returngeance/Returngeance.vcxproj | 3 ++- .../Returngeance/Returngeance.vcxproj.filters | 8 ++++++- 10 files changed, 83 insertions(+), 9 deletions(-) create mode 100644 src/Events/ApplyPointImpulse.h diff --git a/src/Events/ApplyPointImpulse.h b/src/Events/ApplyPointImpulse.h new file mode 100644 index 0000000..a02745b --- /dev/null +++ b/src/Events/ApplyPointImpulse.h @@ -0,0 +1,18 @@ +#ifndef Events_ApplyPointImpulse_h__ +#define Events_ApplyPointImpulse_h__ +#include "Entity.h" +#include "EventBroker.h" + +namespace Events +{ + + struct ApplyPointImpulse : Event + { + EntityID Entity; + glm::vec3 Position; + glm::vec3 Impulse; + }; + +} + +#endif // Events_ApplyPointImpulse_h__ \ No newline at end of file diff --git a/src/GameWorld.cpp b/src/GameWorld.cpp index fa338f2..f0484be 100755 --- a/src/GameWorld.cpp +++ b/src/GameWorld.cpp @@ -222,7 +222,7 @@ void GameWorld::Initialize() transform->Position = glm::vec3(0, 5, 0); //transform->Orientation = glm::angleAxis(0.f, glm::vec3(0, 1, 0)); auto physics = AddComponent(tank); - physics->Mass = 25000; + physics->Mass = 63000 - 16000; physics->Static = false; auto vehicle = AddComponent(tank); vehicle->MaxTorque = 5200.f; @@ -280,7 +280,7 @@ void GameWorld::Initialize() transform->Scale = glm::vec3(3.f); AddComponent(shot); auto physics = AddComponent(shot); - physics->Mass = 10.f; + physics->Mass = 25.f; physics->Static = false; auto modelComponent = AddComponent(shot); modelComponent->ModelFile = "Models/Placeholders/rocket/Rocket.obj"; diff --git a/src/InputManager.cpp b/src/InputManager.cpp index edbb736..f4e81e2 100644 --- a/src/InputManager.cpp +++ b/src/InputManager.cpp @@ -97,6 +97,19 @@ void InputManager::Update(double dt) dwResult = XInputGetState(i, &state); if (dwResult == 0) { + if(std::abs(state.Gamepad.sThumbLX) <= XINPUT_GAMEPAD_LEFT_THUMB_DEADZONE) + state.Gamepad.sThumbLX = 0; + if(std::abs(state.Gamepad.sThumbLY) <= XINPUT_GAMEPAD_LEFT_THUMB_DEADZONE) + state.Gamepad.sThumbLY = 0; + if(std::abs(state.Gamepad.sThumbRX) <= XINPUT_GAMEPAD_RIGHT_THUMB_DEADZONE) + state.Gamepad.sThumbRX = 0; + if(std::abs(state.Gamepad.sThumbRY) <= XINPUT_GAMEPAD_RIGHT_THUMB_DEADZONE) + state.Gamepad.sThumbRY = 0; + if(std::abs(state.Gamepad.bLeftTrigger) <= XINPUT_GAMEPAD_TRIGGER_THRESHOLD) + state.Gamepad.bLeftTrigger = 0; + if(std::abs(state.Gamepad.bRightTrigger) <= XINPUT_GAMEPAD_TRIGGER_THRESHOLD) + state.Gamepad.bRightTrigger = 0; + m_CurrentGamepadAxisState[i][static_cast(Gamepad::Axis::LeftX)] = state.Gamepad.sThumbLX / 32767.f; m_CurrentGamepadAxisState[i][static_cast(Gamepad::Axis::LeftY)] = state.Gamepad.sThumbLY / 32767.f; m_CurrentGamepadAxisState[i][static_cast(Gamepad::Axis::RightX)] = state.Gamepad.sThumbRX / 32767.f; diff --git a/src/Systems/HelicopterSteeringSystem.cpp b/src/Systems/HelicopterSteeringSystem.cpp index 7fd9661..94431c2 100644 --- a/src/Systems/HelicopterSteeringSystem.cpp +++ b/src/Systems/HelicopterSteeringSystem.cpp @@ -4,7 +4,7 @@ void Systems::HelicopterSteeringSystem::RegisterComponents(ComponentFactory* cf) { - cf->Register("HelicopterSteering", []() { return new Components::HelicopterSteering(); }); + cf->Register([]() { return new Components::HelicopterSteering(); }); } void Systems::HelicopterSteeringSystem::Initialize() @@ -19,11 +19,11 @@ void Systems::HelicopterSteeringSystem::Update(double dt) void Systems::HelicopterSteeringSystem::UpdateEntity(double dt, EntityID entity, EntityID parent) { - auto transform = m_World->GetComponent(entity, "Transform"); + auto transform = m_World->GetComponent(entity); if (!transform) return; - auto helicopterComponent = m_World->GetComponent(entity, "HelicopterSteering"); + auto helicopterComponent = m_World->GetComponent(entity); if (helicopterComponent) { glm::vec3 controllerRotationEuler = m_InputController->Rotation * (float)dt; diff --git a/src/Systems/PhysicsSystem.cpp b/src/Systems/PhysicsSystem.cpp index c8ccc6d..c8a899b 100644 --- a/src/Systems/PhysicsSystem.cpp +++ b/src/Systems/PhysicsSystem.cpp @@ -35,7 +35,8 @@ void Systems::PhysicsSystem::Initialize() EVENT_SUBSCRIBE_MEMBER(m_ETankSteer, &Systems::PhysicsSystem::OnTankSteer); EVENT_SUBSCRIBE_MEMBER(m_ESetVelocity, &Systems::PhysicsSystem::OnSetVelocity); EVENT_SUBSCRIBE_MEMBER(m_EApplyForce, &Systems::PhysicsSystem::OnApplyForce); - + EVENT_SUBSCRIBE_MEMBER(m_EApplyPointImpulse, &Systems::PhysicsSystem::OnApplyPointImpulse); + hkMemorySystem::FrameInfo finfo(6000 * 1024); // Allocate 6MB of Physics solver buffer hkMemoryRouter* memoryRouter = hkMemoryInitUtil::initDefault(hkMallocAllocator::m_defaultMallocAllocator, finfo); hkBaseSystem::init(memoryRouter, HavokErrorReport); @@ -605,3 +606,12 @@ bool Systems::PhysicsSystem::OnApplyForce(const Events::ApplyForce &event) return true; } + +bool Systems::PhysicsSystem::OnApplyPointImpulse( const Events::ApplyPointImpulse &event ) +{ + m_PhysicsWorld->markForWrite(); + m_RigidBodies[event.Entity]->applyPointImpulse(ConvertPosition(event.Impulse), ConvertPosition(event.Position)); + m_PhysicsWorld->unmarkForWrite(); + + return true; +} diff --git a/src/Systems/PhysicsSystem.h b/src/Systems/PhysicsSystem.h index 2e0230d..ac73310 100644 --- a/src/Systems/PhysicsSystem.h +++ b/src/Systems/PhysicsSystem.h @@ -16,6 +16,7 @@ #include "Events/TankSteer.h" #include "Events/SetVelocity.h" #include "Events/ApplyForce.h" +#include "Events/ApplyPointImpulse.h" #include "OBJ.h" // Math and base include @@ -108,6 +109,8 @@ private: bool OnSetVelocity(const Events::SetVelocity &event); EventRelay m_EApplyForce; bool OnApplyForce(const Events::ApplyForce &event); + EventRelay m_EApplyPointImpulse; + bool OnApplyPointImpulse(const Events::ApplyPointImpulse &event); void SetUpPhysicsState(EntityID entity, EntityID parent); void TearDownPhysicsState(EntityID entity, EntityID parent); diff --git a/src/Systems/TankSteeringSystem.cpp b/src/Systems/TankSteeringSystem.cpp index 8a67039..7755f1e 100644 --- a/src/Systems/TankSteeringSystem.cpp +++ b/src/Systems/TankSteeringSystem.cpp @@ -62,6 +62,17 @@ void Systems::TankSteeringSystem::UpdateEntity(double dt, EntityID entity, Entit e.Velocity = absoluteTransform.Orientation * (glm::vec3(0.f, 0.f, -1.f) * barrelSteeringComponent->ShotSpeed); EventBroker->Publish(e); m_TimeSinceLastShot[entity] = 0; + + + auto clonePhysicsComponent = m_World->GetComponent(clone); + //1,670m/s + //25kg + EntityID baseParent = m_World->GetEntityBaseParent(entity); + Events::ApplyPointImpulse ePointImpulse ; + ePointImpulse.Entity = baseParent; + ePointImpulse.Position = absoluteTransform.Position; + ePointImpulse.Impulse = glm::normalize(absoluteTransform.Orientation * glm::vec3(0, 0, 1)) * clonePhysicsComponent->Mass * 1670.f; + EventBroker->Publish(ePointImpulse); } m_TimeSinceLastShot[entity] += dt; @@ -83,7 +94,16 @@ void Systems::TankSteeringSystem::TowerSteeringInputController::Update( double d bool Systems::TankSteeringSystem::TankSteeringInputController::OnCommand(const Events::InputCommand &event) { - float val = event.Value; + float val; + if(abs(event.Value) < 0.3f) + { + val = 0.f; + } + else + { + val = event.Value; + } + if (event.Command == "horizontal") { m_Horizontal = val; diff --git a/src/Systems/TankSteeringSystem.h b/src/Systems/TankSteeringSystem.h index d264325..65c0bfc 100644 --- a/src/Systems/TankSteeringSystem.h +++ b/src/Systems/TankSteeringSystem.h @@ -3,10 +3,13 @@ #include "System.h" #include "Events/TankSteer.h" #include "Events/SetVelocity.h" +#include "Events/ApplyForce.h" +#include "Events/ApplyPointImpulse.h" #include "Components/Transform.h" #include "Components/TankSteering.h" #include "Components/TowerSteering.h" #include "Components/BarrelSteering.h" +#include "Components/Physics.h" #include "Components/Vehicle.h" #include "Systems/TransformSystem.h" #include "InputController.h" diff --git a/vs11/Returngeance/Returngeance.vcxproj b/vs11/Returngeance/Returngeance.vcxproj index 250e3db..e3cc2e4 100755 --- a/vs11/Returngeance/Returngeance.vcxproj +++ b/vs11/Returngeance/Returngeance.vcxproj @@ -60,7 +60,7 @@ false Default ProgramDatabase - MaxSpeed + Disabled true @@ -156,6 +156,7 @@ + diff --git a/vs11/Returngeance/Returngeance.vcxproj.filters b/vs11/Returngeance/Returngeance.vcxproj.filters index bdab530..3dd634b 100755 --- a/vs11/Returngeance/Returngeance.vcxproj.filters +++ b/vs11/Returngeance/Returngeance.vcxproj.filters @@ -63,7 +63,9 @@ Physics\Systems - + + Gameplay\Vehicles\Helicopter\Systems + @@ -383,6 +385,10 @@ Physics\Events + + + Physics\Events +