diff --git a/assets b/assets index 5b022cc..9d3f71b 160000 --- a/assets +++ b/assets @@ -1 +1 @@ -Subproject commit 5b022ccb9bbe502e14556831a41636b602260574 +Subproject commit 9d3f71bee6e0a930a4bd265eca3f5fceac619709 diff --git a/src/Components/Health.h b/src/Components/Health.h new file mode 100644 index 0000000..5100aaf --- /dev/null +++ b/src/Components/Health.h @@ -0,0 +1,21 @@ +#ifndef Components_Health_h__ +#define Components_Health_h__ + +#include "Component.h" + +namespace Components +{ + + struct Health : Component + { + Health() + : health(1.0f){ } + + float health; + + virtual Health* Clone() const override { return new Health(*this); } + }; + +} + +#endif // Components_Health_h__ diff --git a/src/Components/HelicopterSteering.h b/src/Components/HelicopterSteering.h new file mode 100644 index 0000000..630ec67 --- /dev/null +++ b/src/Components/HelicopterSteering.h @@ -0,0 +1,16 @@ +#ifndef HelicopterSteering_h__ +#define HelicopterSteering_h__ + +#include "Component.h" + +namespace Components +{ + +struct HelicopterSteering : Component +{ + HelicopterSteering* Clone() const override { return new HelicopterSteering(*this); } +}; + +} + +#endif // HelicopterSteering_h__ \ No newline at end of file diff --git a/src/Components/Wheel.h b/src/Components/Wheel.h index be3638b..4a6a66d 100644 --- a/src/Components/Wheel.h +++ b/src/Components/Wheel.h @@ -14,7 +14,7 @@ struct Wheel : Component Wheel() : AxleID(0), Radius(0), Width(0), Mass(0), Steering(false), DownDirection(glm::vec3(0, -1, 0)), Friction(1.5f), SlipAngle(0.0f), - MaxBreakingTorque(1500.0f), ConnectedToHandbrake(false), SuspensionStrength(50.0f), TorqueRatio(0.25f) { } + MaxBreakingTorque(50000.f), ConnectedToHandbrake(false), SuspensionStrength(50.0f), TorqueRatio(0.25f) { } // The Hardpoint MUST be positioned INSIDE the chassis. glm::vec3 Hardpoint; diff --git a/src/Events/ApplyForce.h b/src/Events/ApplyForce.h new file mode 100644 index 0000000..2008764 --- /dev/null +++ b/src/Events/ApplyForce.h @@ -0,0 +1,18 @@ +#ifndef Events_ApplyForce_h__ +#define Events_ApplyForce_h__ +#include "Entity.h" +#include "EventBroker.h" + +namespace Events +{ + +struct ApplyForce : Event +{ + EntityID Entity; + double DeltaTime; + glm::vec3 Force; +}; + +} + +#endif // Events_ApplyForce_h__ \ No newline at end of file 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 468d356..63a6a9f 100755 --- a/src/GameWorld.cpp +++ b/src/GameWorld.cpp @@ -236,10 +236,12 @@ 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 = 45000; + physics->Mass = 63000 - 16000; physics->Static = false; auto vehicle = AddComponent(tank); - vehicle->MaxTorque = 5200.f; + vehicle->MaxTorque = 8000.f; + vehicle->MaxSteeringAngle = 90.f; + vehicle->MaxSpeedFullSteeringAngle = 4.f; AddComponent(tank); AddComponent(tank); @@ -292,7 +294,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"; @@ -345,7 +347,7 @@ void GameWorld::Initialize() //Create wheels float wheelOffset = 0.4f; float springLength = 0.3f; - float suspensionStrength = 25.f; + float suspensionStrength = 15.f; { auto wheel = CreateEntity(tank); @@ -361,7 +363,7 @@ void GameWorld::Initialize() Wheel->Radius = 0.6f; Wheel->Steering = true; Wheel->SuspensionStrength = suspensionStrength; - Wheel->Friction = 4.f; + Wheel->Friction = 3.f; Wheel->ConnectedToHandbrake = true; Wheel->TorqueRatio = 0.125f; Wheel->Width = 0.6f; @@ -391,7 +393,7 @@ void GameWorld::Initialize() Wheel->Radius = 0.6f; Wheel->Steering = false; Wheel->SuspensionStrength = suspensionStrength; - Wheel->Friction = 4.f; + Wheel->Friction = 3.f; Wheel->ConnectedToHandbrake = true; Wheel->TorqueRatio = 0.125f; Wheel->Width = 0.6f; @@ -422,7 +424,7 @@ void GameWorld::Initialize() Wheel->Radius = 0.6f; Wheel->Steering = true; Wheel->SuspensionStrength = suspensionStrength; - Wheel->Friction = 4.f; + Wheel->Friction = 3.f; Wheel->ConnectedToHandbrake = true; Wheel->TorqueRatio = 0.125f; Wheel->Width = 0.6f; @@ -450,9 +452,9 @@ void GameWorld::Initialize() Wheel->AxleID = 0; Wheel->Mass = 2000; Wheel->Radius = 0.6f; - Wheel->Steering = false; + Wheel->Steering = true; Wheel->SuspensionStrength = suspensionStrength; - Wheel->Friction = 4.f; + Wheel->Friction = 3.f; Wheel->ConnectedToHandbrake = true; Wheel->TorqueRatio = 0.125f; Wheel->Width = 0.6f; @@ -484,7 +486,7 @@ void GameWorld::Initialize() Wheel->Radius = 0.6f; Wheel->Steering = false; Wheel->SuspensionStrength = suspensionStrength; - Wheel->Friction = 4.f; + Wheel->Friction = 3.f; Wheel->ConnectedToHandbrake = true; Wheel->TorqueRatio = 0.125f; Wheel->Width = 0.6f; @@ -513,7 +515,7 @@ void GameWorld::Initialize() Wheel->Radius = 0.6f; Wheel->Steering = false; Wheel->SuspensionStrength = suspensionStrength; - Wheel->Friction = 4.f; + Wheel->Friction = 3.f; Wheel->ConnectedToHandbrake = true; Wheel->TorqueRatio = 0.125f; Wheel->Width = 0.6f; @@ -568,7 +570,7 @@ void GameWorld::Initialize() Wheel->Radius = 0.6f; Wheel->Steering = false; Wheel->SuspensionStrength = suspensionStrength; - Wheel->Friction = 4.f; + Wheel->Friction = 3.f; Wheel->ConnectedToHandbrake = true; Wheel->TorqueRatio = 0.125f; Wheel->Width = 0.6f; @@ -597,7 +599,7 @@ void GameWorld::Initialize() Wheel->Radius = 0.6f; Wheel->Steering = false; Wheel->SuspensionStrength = suspensionStrength; - Wheel->Friction = 4.f; + Wheel->Friction = 3.f; Wheel->ConnectedToHandbrake = true; Wheel->TorqueRatio = 0.125f; Wheel->Width = 0.6f; diff --git a/src/GameWorld.h b/src/GameWorld.h index 2823a7f..e4f7403 100755 --- a/src/GameWorld.h +++ b/src/GameWorld.h @@ -13,6 +13,7 @@ //#include "Systems/PlayerSystem.h" #include "Systems/FreeSteeringSystem.h" #include "Systems/TankSteeringSystem.h" +#include "Systems/HelicopterSteeringSystem.h" #include "Systems/RenderSystem.h" #include "Systems/SoundSystem.h" #include "Systems/PhysicsSystem.h" diff --git a/src/InputManager.cpp b/src/InputManager.cpp index 2e89d9f..8c15fea 100644 --- a/src/InputManager.cpp +++ b/src/InputManager.cpp @@ -110,6 +110,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/Physics/VehicleSetup.cpp b/src/Physics/VehicleSetup.cpp index 7db47af..e319834 100644 --- a/src/Physics/VehicleSetup.cpp +++ b/src/Physics/VehicleSetup.cpp @@ -22,7 +22,7 @@ void VehicleSetup::buildVehicle(World *world, const hkpWorld* physicsWorld, hkpV // vehicle.m_data = new hkpVehicleData; vehicle.m_driverInput = new hkpVehicleDefaultAnalogDriverInput; - vehicle.m_steering = new hkpVehicleDefaultSteering; + vehicle.m_steering = new TankSteering; vehicle.m_engine = new hkpVehicleDefaultEngine; vehicle.m_transmission = new hkpVehicleDefaultTransmission; vehicle.m_brake = new hkpVehicleDefaultBrake; @@ -201,7 +201,7 @@ void VehicleSetup::setupComponent(const hkpVehicleData& data, hkpVehicleDefaultT transmission.m_upshiftRPM = 7000.0f; transmission.m_clutchDelayTime = 0.0f; - transmission.m_reverseGearRatio = 1.2f; + transmission.m_reverseGearRatio = 1.0f; transmission.m_gearsRatio[0] = 3.0f; transmission.m_gearsRatio[1] = 2.25f; transmission.m_gearsRatio[2] = 1.5f; diff --git a/src/Physics/VehicleSetup.h b/src/Physics/VehicleSetup.h index 7f78204..bf75840 100644 --- a/src/Physics/VehicleSetup.h +++ b/src/Physics/VehicleSetup.h @@ -33,6 +33,32 @@ #include "Components/Wheel.h" #include "Components/Transform.h" + +/// Tank specific steering implementation. Rear wheels steer in opposite direction +/// to front wheels. +class TankSteering: public hkpVehicleDefaultSteering +{ +public: + virtual void calcSteering(const hkReal deltaTime, const hkpVehicleInstance* vehicle, const hkpVehicleDriverInput::FilteredDriverInputOutput& filteredInfoOutput, SteeringAnglesOutput& steeringOutput ) + { + hkpVehicleDefaultSteering::calcMainSteeringAngle( deltaTime, vehicle, filteredInfoOutput, steeringOutput ); + + // Wheels. + for (int w_it = 0; w_it < m_doesWheelSteer.getSize(); w_it++) + { + if ( m_doesWheelSteer[w_it] ) + { + steeringOutput.m_wheelsSteeringAngle [w_it] = steeringOutput.m_mainSteeringAngle; + } + else + { + // Steer with front and back wheels to simulate a tank. + steeringOutput.m_wheelsSteeringAngle [w_it] = -steeringOutput.m_mainSteeringAngle; + } + } + } +}; + class VehicleSetup { public: diff --git a/src/Systems/FreeSteeringSystem.cpp b/src/Systems/FreeSteeringSystem.cpp index 8a5d638..efbfde1 100755 --- a/src/Systems/FreeSteeringSystem.cpp +++ b/src/Systems/FreeSteeringSystem.cpp @@ -60,27 +60,27 @@ void Systems::FreeSteeringSystem::UpdateEntity(double dt, EntityID entity, Entit bool Systems::FreeSteeringSystem::FreeSteeringInputController::OnCommand(const Events::InputCommand &event) { // Movement - if (event.Command == "vertical") + if (event.Command == "cam_vertical") { Movement.z = -event.Value; } - else if (event.Command == "horizontal") + else if (event.Command == "cam_horizontal") { Movement.x = event.Value; } - else if (event.Command == "normal") + else if (event.Command == "cam_normal") { Movement.y = event.Value; } // Speed - else if (event.Command == "speed") + else if (event.Command == "cam_speed") { SpeedMultiplier = event.Value; } // Mouse click - else if (event.Command == "attack") + else if (event.Command == "cam_attack") { OrientationActive = event.Value > 0; @@ -96,11 +96,11 @@ bool Systems::FreeSteeringSystem::FreeSteeringInputController::OnCommand(const E } } - else if (event.Command == "vertical2") + else if (event.Command == "cam_vertical2") { ControllerOrientation.x = event.Value; } - else if (event.Command == "horizontal2") + else if (event.Command == "cam_horizontal2") { ControllerOrientation.y = -event.Value; } diff --git a/src/Systems/HelicopterSteeringSystem.cpp b/src/Systems/HelicopterSteeringSystem.cpp new file mode 100644 index 0000000..94431c2 --- /dev/null +++ b/src/Systems/HelicopterSteeringSystem.cpp @@ -0,0 +1,67 @@ +#include "PrecompiledHeader.h" +#include "HelicopterSteeringSystem.h" +#include "World.h" + +void Systems::HelicopterSteeringSystem::RegisterComponents(ComponentFactory* cf) +{ + cf->Register([]() { return new Components::HelicopterSteering(); }); +} + +void Systems::HelicopterSteeringSystem::Initialize() +{ + m_InputController = std::unique_ptr(new HelicopterSteeringInputController(EventBroker)); +} + +void Systems::HelicopterSteeringSystem::Update(double dt) +{ + +} + +void Systems::HelicopterSteeringSystem::UpdateEntity(double dt, EntityID entity, EntityID parent) +{ + auto transform = m_World->GetComponent(entity); + if (!transform) + return; + + auto helicopterComponent = m_World->GetComponent(entity); + if (helicopterComponent) + { + glm::vec3 controllerRotationEuler = m_InputController->Rotation * (float)dt; + transform->Orientation *= glm::quat(controllerRotationEuler); + + Events::ApplyForce e; + e.Entity = entity; + e.DeltaTime = dt; + e.Force = glm::normalize(transform->Orientation * glm::vec3(0, 1, 0)) * (m_InputController->Power * 3000.f * 9.82f * 8.f); + EventBroker->Publish(e); + } +} + +bool Systems::HelicopterSteeringSystem::HelicopterSteeringInputController::OnCommand(const Events::InputCommand &event) +{ + if (event.Command == "horizontal") + { + Rotation.z = -event.Value; + } + else if (event.Command == "vertical") + { + Rotation.x = -event.Value; + } + + else if (event.Command == "normal") + { + Power = event.Value; + } + + return true; +} + +bool Systems::HelicopterSteeringSystem::HelicopterSteeringInputController::OnMouseMove(const Events::MouseMove &event) +{ + return true; +} + +void Systems::HelicopterSteeringSystem::HelicopterSteeringInputController::Update(double dt) +{ + +} diff --git a/src/Systems/HelicopterSteeringSystem.h b/src/Systems/HelicopterSteeringSystem.h new file mode 100644 index 0000000..ed91096 --- /dev/null +++ b/src/Systems/HelicopterSteeringSystem.h @@ -0,0 +1,54 @@ +#include "System.h" +#include "Components/Transform.h" +#include "Components/HelicopterSteering.h" +#include "Events/SetVelocity.h" +#include "Events/ApplyForce.h" +#include "InputController.h" + +namespace Systems +{ + +class HelicopterSteeringSystem : public System +{ +public: + HelicopterSteeringSystem(World* world, std::shared_ptr<::EventBroker> eventBroker) + : System(world, eventBroker) { } + + void RegisterComponents(ComponentFactory* cf) override; + void Initialize() override; + + void Update(double dt) override; + void UpdateEntity(double dt, EntityID entity, EntityID parent) override; + +private: + class HelicopterSteeringInputController; + std::unique_ptr m_InputController; + + std::map m_TimeSinceLastShot; +}; + +class HelicopterSteeringSystem::HelicopterSteeringInputController : InputController +{ +public: + HelicopterSteeringInputController(std::shared_ptr<::EventBroker> eventBroker) + : InputController(eventBroker) + , Power(0.f) + { } + + float Power; + glm::vec3 Rotation; + + void Update(double dt); + +protected: + bool OnCommand(const Events::InputCommand &event) override; + bool OnMouseMove(const Events::MouseMove &event) override; + + +}; + + + + + +} \ No newline at end of file diff --git a/src/Systems/PhysicsSystem.cpp b/src/Systems/PhysicsSystem.cpp index 1fd737b..03aade8 100644 --- a/src/Systems/PhysicsSystem.cpp +++ b/src/Systems/PhysicsSystem.cpp @@ -27,12 +27,16 @@ void Systems::PhysicsSystem::Initialize() { + + m_Accumulator = 0; // Events 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); @@ -102,6 +106,8 @@ void Systems::PhysicsSystem::Initialize() SetupVisualDebugger(m_Context); m_PhysicsWorld->unmarkForWrite(); + + m_collisionResolution = new MyCollisionResolution; } } @@ -140,13 +146,13 @@ void Systems::PhysicsSystem::Update(double dt) if (parent) { auto absoluteTransform = m_World->GetSystem()->AbsoluteTransform(entity); - position = ConvertPosition(absoluteTransform.Position); - rotation = ConvertRotation(absoluteTransform.Orientation); + position = GLMVEC3_TO_HKVECTOR4(absoluteTransform.Position); + rotation = GLMQUAT_TO_HKQUATERNION(absoluteTransform.Orientation); } else { - position = ConvertPosition(transformComponent->Position); - rotation = ConvertRotation(transformComponent->Orientation); + position = GLMVEC3_TO_HKVECTOR4(transformComponent->Position); + rotation = GLMQUAT_TO_HKQUATERNION(transformComponent->Orientation); } m_PhysicsWorld->markForWrite(); m_RigidBodies[entity]->setPositionAndRotation(position, rotation); @@ -172,10 +178,7 @@ void Systems::PhysicsSystem::Update(double dt) // Clear accumulated timer data in this thread and all slave threads hkMonitorStream::getInstance().reset(); m_ThreadPool->clearTimerData(); - } - - - + } } void Systems::PhysicsSystem::UpdateEntity(double dt, EntityID entity, EntityID parent) @@ -201,7 +204,7 @@ void Systems::PhysicsSystem::UpdateEntity(double dt, EntityID entity, EntityID p hkQuaternion steeringOrientation = m_Vehicles[car]->m_wheelsInfo[wheelComponent->ID].m_steeringOrientationChassisSpace; hkReal spinAngle = -m_Vehicles[car]->m_wheelsInfo[wheelComponent->ID].m_spinAngle; - glm::quat orientation = ConvertRotation(steeringOrientation) * glm::angleAxis(spinAngle, glm::vec3(1, 0, 0)); + glm::quat orientation = HKQUATERNION_TO_GLMQUAT(steeringOrientation) * glm::angleAxis(spinAngle, glm::vec3(1, 0, 0)); transformComponent->Orientation = orientation * wheelComponent->OriginalOrientation; m_PhysicsWorld->unmarkForWrite(); } @@ -210,8 +213,8 @@ void Systems::PhysicsSystem::UpdateEntity(double dt, EntityID entity, EntityID p { auto transformComponentParent = m_World->GetComponent(parent); - transformComponent->Position = ConvertPosition(m_RigidBodies[entity]->getPosition()); - transformComponent->Orientation = ConvertRotation(m_RigidBodies[entity]->getRotation()); + transformComponent->Position = HKVECTOR4_TO_GLMVEC3(m_RigidBodies[entity]->getPosition()); + transformComponent->Orientation = HKQUATERNION_TO_GLMQUAT(m_RigidBodies[entity]->getRotation()); // TODO: No support for Scale, MIGHT be possible if (transformComponentParent) @@ -274,13 +277,9 @@ void Systems::PhysicsSystem::OnEntityCommit( EntityID entity ) hkpListShape* listShape = new hkpListShape(shapeArray.begin(), shapeArray.getSize(), hkpShapeContainer::REFERENCE_POLICY_INCREMENT); // Save the listShape for further use m_ListShapes[entity] = listShape; - shape = listShape; - - ////////////////////////////////// - //******************************// - // Add a hkpBvShape // - //******************************// - ////////////////////////////////// + //shape = listShape; + hkpBoxShape* box = new hkpBoxShape(listShape->m_aabbHalfExtents, 0.0f); + shape = new hkpBvShape(listShape, box); // Clean up for less memory usage m_Shapes.erase(entity); @@ -293,8 +292,8 @@ void Systems::PhysicsSystem::OnEntityCommit( EntityID entity ) rigidBodyInfo.m_shape = shape; rigidBodyInfo.m_motionType = hkpMotion::MOTION_DYNAMIC; auto absoluteTransform = m_World->GetSystem()->AbsoluteTransform(entity); - hkVector4 position = ConvertPosition(absoluteTransform.Position); - hkQuaternion rotation = ConvertRotation(absoluteTransform.Orientation); + hkVector4 position = GLMVEC3_TO_HKVECTOR4(absoluteTransform.Position); + hkQuaternion rotation = GLMQUAT_TO_HKQUATERNION(absoluteTransform.Orientation); rigidBodyInfo.m_position.set(position(0), position(1), position(2), position(3)); rigidBodyInfo.m_rotation.set(rotation(0), rotation(1), rotation(2), rotation(3)); @@ -324,9 +323,11 @@ void Systems::PhysicsSystem::OnEntityCommit( EntityID entity ) m_PhysicsWorld->markForWrite(); vehicleSetup.buildVehicle(m_World, m_PhysicsWorld, *m_Vehicles[entity], entity, m_Wheels); // Add the vehicle's entities and phantoms to the world + rigidBody->addContactListener( m_collisionResolution ); m_Vehicles[entity]->addToWorld(m_PhysicsWorld); - m_RigidBodies[entity] = rigidBody; + m_collisionResolution->m_RigidBodies[rigidBody] = entity; + // The vehicle is an action m_PhysicsWorld->addAction(m_Vehicles[entity]); @@ -340,8 +341,10 @@ void Systems::PhysicsSystem::OnEntityCommit( EntityID entity ) else { m_PhysicsWorld->markForWrite(); + rigidBody->addContactListener( m_collisionResolution ); m_PhysicsWorld->addEntity(rigidBody); m_RigidBodies[entity] = rigidBody; + m_collisionResolution->m_RigidBodies[rigidBody] = entity; m_PhysicsWorld->unmarkForWrite(); shape->removeReference(); @@ -359,9 +362,9 @@ void Systems::PhysicsSystem::OnEntityCommit( EntityID entity ) auto childTransformComponent = m_World->GetComponent(shapeData.Entity); - hkVector4 position = ConvertPosition(childTransformComponent->Position); - hkQuaternion rotation = ConvertRotation(childTransformComponent->Orientation); - hkVector4 scale = ConvertScale(childTransformComponent->Scale); + hkVector4 position = GLMVEC3_TO_HKVECTOR4(childTransformComponent->Position); + hkQuaternion rotation = GLMQUAT_TO_HKQUATERNION(childTransformComponent->Orientation); + hkVector4 scale = GLMVEC3_TO_HKVECTOR4(childTransformComponent->Scale); hkQsTransform transform(position, rotation, scale); staticCompoundShape->addInstance(shapeData.Shape, transform); @@ -379,8 +382,8 @@ void Systems::PhysicsSystem::OnEntityCommit( EntityID entity ) rigidBodyInfo.m_shape = shape; rigidBodyInfo.m_motionType = hkpMotion::MOTION_FIXED; auto absoluteTransform = m_World->GetSystem()->AbsoluteTransform(entity); - hkVector4 position = ConvertPosition(absoluteTransform.Position); - hkQuaternion rotation = ConvertRotation(absoluteTransform.Orientation); + hkVector4 position = GLMVEC3_TO_HKVECTOR4(absoluteTransform.Position); + hkQuaternion rotation = GLMQUAT_TO_HKQUATERNION(absoluteTransform.Orientation); rigidBodyInfo.m_position.set(position(0), position(1), position(2), position(3)); rigidBodyInfo.m_rotation.set(rotation(0), rotation(1), rotation(2), rotation(3)); @@ -394,6 +397,7 @@ void Systems::PhysicsSystem::OnEntityCommit( EntityID entity ) m_PhysicsWorld->markForWrite(); m_PhysicsWorld->addEntity(rigidBody); m_RigidBodies[entity] = rigidBody; + m_collisionResolution->m_RigidBodies[rigidBody] = entity; m_PhysicsWorld->unmarkForWrite(); shape->removeReference(); @@ -411,7 +415,7 @@ void Systems::PhysicsSystem::OnEntityCommit( EntityID entity ) { hkpSphereShape* sphereShape = new hkpSphereShape(sphereComponent->Radius); - hkQsTransform transform( ConvertPosition(transformComponent->Position), ConvertRotation(transformComponent->Orientation), ConvertScale(transformComponent->Scale)); + hkQsTransform transform( GLMVEC3_TO_HKVECTOR4(transformComponent->Position), GLMQUAT_TO_HKQUATERNION(transformComponent->Orientation), GLMVEC3_TO_HKVECTOR4(transformComponent->Scale)); hkpConvexTransformShape* transformedSphereShape = new hkpConvexTransformShape( sphereShape, transform ); m_Shapes[entityParent].push_back(ShapeArrayData(entity, transformedSphereShape)); @@ -424,7 +428,7 @@ void Systems::PhysicsSystem::OnEntityCommit( EntityID entity ) hkReal thickness = 0.05; hkpBoxShape* boxShape = new hkpBoxShape(hkVector4(boxComponent->Width- thickness, boxComponent->Height -thickness, boxComponent->Depth - thickness), thickness); - hkQsTransform transform( ConvertPosition(transformComponent->Position), ConvertRotation(transformComponent->Orientation), ConvertScale(transformComponent->Scale)); + hkQsTransform transform( GLMVEC3_TO_HKVECTOR4(transformComponent->Position), GLMQUAT_TO_HKQUATERNION(transformComponent->Orientation), GLMVEC3_TO_HKVECTOR4(transformComponent->Scale)); hkpConvexTransformShape* transformedBoxShape = new hkpConvexTransformShape( boxShape, transform ); m_Shapes[entityParent].push_back(ShapeArrayData(entity, transformedBoxShape)); boxShape->removeReference(); @@ -532,50 +536,19 @@ void HK_CALL Systems::PhysicsSystem::HavokErrorReport(const char* msg, void*) LOG_INFO("%s", msg); } - -glm::vec3 Systems::PhysicsSystem::ConvertPosition(const hkVector4 &hkPosition) -{ - return glm::vec3(hkPosition(0), hkPosition(1), hkPosition(2)); -} - -const hkVector4& Systems::PhysicsSystem::ConvertPosition(glm::vec3 glmPosition) -{ - return hkVector4( glmPosition.x, glmPosition.y, glmPosition.z); -} - -glm::quat Systems::PhysicsSystem::ConvertRotation(const hkQuaternion &hkRotation) -{ - return glm::quat(hkRotation(3), hkRotation(0), hkRotation(1), hkRotation(2)); -} - -const hkQuaternion& Systems::PhysicsSystem::ConvertRotation(glm::quat glmRotation) -{ - hkQuaternion quat = hkQuaternion(glmRotation.x, glmRotation.y, glmRotation.z, glmRotation.w); - quat.normalize(); - return quat; -} - -glm::vec3 Systems::PhysicsSystem::ConvertScale(const hkVector4 &hkScale) -{ - return glm::vec3(hkScale(0), hkScale(1), hkScale(2)); -} - -const hkVector4& Systems::PhysicsSystem::ConvertScale(glm::vec3 glmScale) -{ - return hkVector4(glmScale.x, glmScale.y, glmScale.z); -} - bool Systems::PhysicsSystem::OnTankSteer(const Events::TankSteer &event) { auto vehicleComponent = m_World->GetComponent(event.Entity); if (vehicleComponent && m_Vehicles.find(event.Entity) != m_Vehicles.end() && m_RigidBodies.find(event.Entity) != m_RigidBodies.end()) { - m_PhysicsWorld->markForWrite(); hkpVehicleDriverInputAnalogStatus* deviceStatus = (hkpVehicleDriverInputAnalogStatus*)m_Vehicles[event.Entity]->m_deviceStatus; deviceStatus->m_positionX = event.PositionX; deviceStatus->m_positionY = event.PositionY; + if(event.PositionY > 0) + { + deviceStatus->m_reverseButtonPressed = true; + } deviceStatus->m_handbrakeButtonPressed = event.Handbrake; - m_PhysicsWorld->unmarkForWrite(); } return true; @@ -584,7 +557,23 @@ bool Systems::PhysicsSystem::OnTankSteer(const Events::TankSteer &event) bool Systems::PhysicsSystem::OnSetVelocity( const Events::SetVelocity &event ) { m_PhysicsWorld->markForWrite(); - m_RigidBodies[event.Entity]->setLinearVelocity(ConvertPosition(event.Velocity)); + m_RigidBodies[event.Entity]->setLinearVelocity(GLMVEC3_TO_HKVECTOR4(event.Velocity)); + m_PhysicsWorld->unmarkForWrite(); + return true; +} + +bool Systems::PhysicsSystem::OnApplyForce(const Events::ApplyForce &event) +{ + m_PhysicsWorld->markForWrite(); + m_RigidBodies[event.Entity]->applyForce(event.DeltaTime, GLMVEC3_TO_HKVECTOR4(event.Force)); + m_PhysicsWorld->unmarkForWrite(); + return true; +} + +bool Systems::PhysicsSystem::OnApplyPointImpulse( const Events::ApplyPointImpulse &event ) +{ + m_PhysicsWorld->markForWrite(); + m_RigidBodies[event.Entity]->applyPointImpulse(GLMVEC3_TO_HKVECTOR4(event.Impulse), GLMVEC3_TO_HKVECTOR4(event.Position)); m_PhysicsWorld->unmarkForWrite(); return true; } diff --git a/src/Systems/PhysicsSystem.h b/src/Systems/PhysicsSystem.h index 7b2d259..54e61f7 100644 --- a/src/Systems/PhysicsSystem.h +++ b/src/Systems/PhysicsSystem.h @@ -1,6 +1,16 @@ #ifndef PhysicsSystem_h__ #define PhysicsSystem_h__ +#define HKVECTOR4_TO_GLMVEC3(hkvec) \ + glm::vec3(hkvec(0), hkvec(1), hkvec(2)) +#define GLMVEC3_TO_HKVECTOR4(glmvec) \ + hkVector4(glmvec.x, glmvec.y, glmvec.z) + +#define HKQUATERNION_TO_GLMQUAT(gkquat) \ + glm::quat(gkquat(3), gkquat(0), gkquat(1), gkquat(2)) +#define GLMQUAT_TO_HKQUATERNION(glmquat) \ + hkQuaternion(glmquat.x, glmquat.y, glmquat.z, glmquat.w) + #include "System.h" #include "Systems/TransformSystem.h" #include "Components/Transform.h" @@ -15,6 +25,8 @@ #include "Components/TowerSteering.h" #include "Events/TankSteer.h" #include "Events/SetVelocity.h" +#include "Events/ApplyForce.h" +#include "Events/ApplyPointImpulse.h" #include "OBJ.h" // Math and base include @@ -60,9 +72,27 @@ #include "Physics/VehicleSetup.h" #include + +#include + +class MyCollisionResolution: public hkReferencedObject, public hkpContactListener +{ +public: + std::unordered_map m_RigidBodies; + + virtual void contactPointCallback( const hkpContactPointEvent& event ) + { + + EntityID entity1 = m_RigidBodies[event.getBody(0)]; + EntityID entity2 = m_RigidBodies[event.getBody(1)]; + //LOG_INFO("Entities colliding: %i, %i ", entity1, entity2); + + } +}; + + namespace Systems { - class PhysicsSystem : public System { public: @@ -77,7 +107,7 @@ public: void OnComponentCreated(std::string type, std::shared_ptr component) override; void OnComponentRemoved(std::string type, Component* component) override; void OnEntityCommit(EntityID entity) override; - + private: double m_Accumulator; hkpWorld* m_PhysicsWorld; @@ -85,9 +115,12 @@ private: // Events EventRelay m_ETankSteer; bool OnTankSteer(const Events::TankSteer &event); - EventRelay m_ESetVelocity; 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); @@ -98,16 +131,6 @@ private: static void HK_CALL HavokErrorReport(const char* msg, void*); void SetupPhysics(hkpWorld* physicsWorld); - // Converterfunctions - glm::vec3 ConvertPosition(const hkVector4 &hkPosition); - const hkVector4& ConvertPosition(glm::vec3 glmPosition); - - glm::quat ConvertRotation(const hkQuaternion &hkRotation); - const hkQuaternion& ConvertRotation(glm::quat glmRotation); - - glm::vec3 ConvertScale(const hkVector4 &hkScale); - const hkVector4&ConvertScale(glm::vec3 glmScale); - std::unordered_map m_RigidBodies; hkJobThreadPool* m_ThreadPool; @@ -145,8 +168,9 @@ private: hkpMoppBvTreeShape* MoppShape; }; std::unordered_map m_ExtendedMeshShapes; + + MyCollisionResolution* m_collisionResolution; }; } - #endif // PhysicsSystem_h__ diff --git a/src/Systems/TankSteeringSystem.cpp b/src/Systems/TankSteeringSystem.cpp index dca3441..11c48fe 100644 --- a/src/Systems/TankSteeringSystem.cpp +++ b/src/Systems/TankSteeringSystem.cpp @@ -24,6 +24,9 @@ void Systems::TankSteeringSystem::Update(double dt) void Systems::TankSteeringSystem::UpdateEntity(double dt, EntityID entity, EntityID parent) { auto tankSteeringComponent = m_World->GetComponent(entity); + auto towerSteeringComponent = m_World->GetComponent(entity); + auto barrelSteeringComponent = m_World->GetComponent(entity); + if(tankSteeringComponent) { Events::TankSteer e; @@ -33,17 +36,13 @@ void Systems::TankSteeringSystem::UpdateEntity(double dt, EntityID entity, Entit e.Handbrake = m_TankInputController->Handbrake; EventBroker->Publish(e); } - - auto towerSteeringComponent = m_World->GetComponent(entity); - if(towerSteeringComponent) + else if(towerSteeringComponent) { auto transformComponent = m_World->GetComponent(entity); glm::quat orientation = glm::angleAxis(towerSteeringComponent->TurnSpeed * m_TowerInputController->TowerDirection * (float)dt, towerSteeringComponent->Axis); transformComponent->Orientation *= orientation; } - - auto barrelSteeringComponent = m_World->GetComponent(entity); - if(barrelSteeringComponent) + else if(barrelSteeringComponent) { auto transformComponent = m_World->GetComponent(entity); auto absoluteTransform = m_World->GetSystem()->AbsoluteTransform(entity); @@ -62,6 +61,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,10 +93,13 @@ void Systems::TankSteeringSystem::TowerSteeringInputController::Update( double d bool Systems::TankSteeringSystem::TankSteeringInputController::OnCommand(const Events::InputCommand &event) { + float val = event.Value; + if (event.Command == "horizontal") { m_Horizontal = val; + m_Vertical = -0.4f; } else if (event.Command == "vertical") { 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 8700594..9f7083a 100755 --- a/vs11/Returngeance/Returngeance.vcxproj +++ b/vs11/Returngeance/Returngeance.vcxproj @@ -60,7 +60,7 @@ false Default ProgramDatabase - MaxSpeed + Disabled true @@ -111,6 +111,7 @@ + @@ -131,6 +132,8 @@ + + @@ -153,6 +156,8 @@ + + @@ -189,6 +194,7 @@ + diff --git a/vs11/Returngeance/Returngeance.vcxproj.filters b/vs11/Returngeance/Returngeance.vcxproj.filters index a3379e2..d7a5449 100755 --- a/vs11/Returngeance/Returngeance.vcxproj.filters +++ b/vs11/Returngeance/Returngeance.vcxproj.filters @@ -63,6 +63,9 @@ Physics\Systems + + Gameplay\Vehicles\Helicopter\Systems + @@ -137,6 +140,21 @@ {42ae084f-ade8-402c-87ba-f03f6b846bc8} + + {5b992473-73e6-485e-8f13-17e0801fb2ca} + + + {8a78be3a-a3c5-4054-a2f1-1456f3fcbab0} + + + {8ccc0abe-5bdd-4656-ab11-5708a39c71ae} + + + {b2416d05-a49e-4fef-a5c4-cd03d8cc2efd} + + + {b34c0c95-a887-4cf4-af24-cf7c1f459aa8} + @@ -355,9 +373,25 @@ Physics\Components + + Physics\Components + Input\Events + + Gameplay\Vehicles\Helicopter\Components + + + Gameplay\Vehicles\Helicopter\Systems + + + Physics\Events + + + + Physics\Events +