From d7e65ff118cdccd2ff6d947080a8da3e0f341a31 Mon Sep 17 00:00:00 2001 From: ViktorLjung Date: Fri, 11 Apr 2014 23:54:00 +0200 Subject: [PATCH] Vehicle kind of working. --- src/Components/Vehicle.h | 18 ++++++ src/Components/Wheel.h | 31 +++++++++ src/GameWorld.cpp | 18 ++++-- src/GameWorld.h | 3 + src/Systems/PhysicsSystem.cpp | 64 ++++++++++++++++++- src/Systems/PhysicsSystem.h | 18 ++++++ vs12/Returngeance/Returngeance.vcxproj | 2 + .../Returngeance/Returngeance.vcxproj.filters | 6 ++ 8 files changed, 153 insertions(+), 7 deletions(-) create mode 100644 src/Components/Vehicle.h create mode 100644 src/Components/Wheel.h diff --git a/src/Components/Vehicle.h b/src/Components/Vehicle.h new file mode 100644 index 0000000..a4b3b3b --- /dev/null +++ b/src/Components/Vehicle.h @@ -0,0 +1,18 @@ +#ifndef Components_Vehicle_h__ +#define Components_Vehicle_h__ + +#include "Component.h" + +namespace Components +{ + + struct Vehicle : Component +{ + + + +}; + +} + +#endif // Components_Vehicle_h__ diff --git a/src/Components/Wheel.h b/src/Components/Wheel.h new file mode 100644 index 0000000..bcb617a --- /dev/null +++ b/src/Components/Wheel.h @@ -0,0 +1,31 @@ +#ifndef Components_Wheel_h__ +#define Components_Wheel_h__ + +#include "Component.h" + + +namespace Components +{ + + struct Wheel : Component +{ + EntityID CarID; + + glm::vec3 ConnectionPoint; + glm::vec3 Direction; + glm::vec3 Axle; + float SuspensionRestLength; + float Radius; + bool IsFrontWheel; + + float SuspensionStiffness; + float SuspensionCompression; + float SuspensionDamping; + float MaxSuspensionTravelCm; + float FrictionSlip; + float MaxSuspensionForce; +}; + +} + +#endif // Components_Wheel_h__ diff --git a/src/GameWorld.cpp b/src/GameWorld.cpp index 9f51477..f420ab7 100755 --- a/src/GameWorld.cpp +++ b/src/GameWorld.cpp @@ -17,22 +17,25 @@ void GameWorld::Initialize() { auto terrain = CreateEntity(); auto transform = AddComponent(terrain, "Transform"); - transform->Scale = glm::vec3(1000.0f); - transform->Orientation = glm::quat(glm::vec3(0.0f, 0.0f, glm::pi() / 20.f)); + transform->Position = glm::vec3(0, -5, 0); + transform->Scale = glm::vec3(1000.0f, 1, 1000.0f); + transform->Orientation = glm::quat(glm::vec3(0.0f, 0.0f, 0.0f)); auto model = AddComponent(terrain, "Model"); model->ModelFile = "Models/Placeholders/PhysicsTest/Plane.obj"; auto physics = AddComponent(terrain, "Physics"); + physics->Friction = 0.5f; auto Box = AddComponent(terrain, "BoxShape"); Box->Width = 0.5f; - Box->Height = 0; + Box->Height = 0.5; Box->Depth = 0.5f; - physics->Friction = 0.5f; + } { + for (int i = 0; i < 1; i++) { auto entity = CreateEntity(); auto transform = AddComponent(entity, "Transform"); @@ -41,7 +44,6 @@ void GameWorld::Initialize() transform->Orientation = glm::quat(glm::vec3(0.0f, 0.0f, 0.0f)); auto model = AddComponent(entity, "Model"); model->ModelFile = "Models/Placeholders/PhysicsTest/ArrowCube.obj"; - auto physics = AddComponent(entity, "Physics"); physics->Mass = 10; @@ -52,6 +54,9 @@ void GameWorld::Initialize() { auto entity1 = CreateEntity(); + + + auto transform = AddComponent(entity1, "Transform"); transform->Scale = glm::vec3(1.0f); transform->Position = glm::vec3(-5.f, 10+i, 0); @@ -123,6 +128,9 @@ void GameWorld::RegisterComponents() m_ComponentFactory.Register("HingeConstraint", []() { return new Components::HingeConstraint(); }); m_ComponentFactory.Register("BallSocketConstraint", []() { return new Components::BallSocketConstraint(); }); m_ComponentFactory.Register("SliderConstraint", []() { return new Components::SliderConstraint(); }); + + m_ComponentFactory.Register("Vehicle", []() { return new Components::Vehicle(); }); + m_ComponentFactory.Register("Wheel", []() { return new Components::Wheel(); }); } void GameWorld::RegisterSystems() diff --git a/src/GameWorld.h b/src/GameWorld.h index 5218d26..fb2c2a0 100755 --- a/src/GameWorld.h +++ b/src/GameWorld.h @@ -39,6 +39,9 @@ #include "Components/HingeConstraint.h" #include "Components/SliderConstraint.h" +#include "Components/Vehicle.h" +#include "Components/Wheel.h" + class GameWorld : public World { public: diff --git a/src/Systems/PhysicsSystem.cpp b/src/Systems/PhysicsSystem.cpp index b3e6c3b..5c8a566 100644 --- a/src/Systems/PhysicsSystem.cpp +++ b/src/Systems/PhysicsSystem.cpp @@ -11,7 +11,6 @@ Systems::PhysicsSystem::PhysicsSystem(World* world) : System(world) m_Solver = new btSequentialImpulseConstraintSolver(); m_DynamicsWorld = new btDiscreteDynamicsWorld(m_Dispatcher, m_Broadphase, m_Solver, m_CollisionConfiguration); - m_DynamicsWorld->setGravity(btVector3(0, -9.82f, 0)); } @@ -57,8 +56,10 @@ void Systems::PhysicsSystem::UpdateEntity(double dt, EntityID entity, EntityID p auto boxShapeComponent = m_World->GetComponent(entity, "BoxShape"); auto meshShapeComponent = m_World->GetComponent(entity, "MeshShape"); auto staticMeshShapeComponent = m_World->GetComponent(entity, "StaticMeshShape"); + auto vehicleComponent = m_World->GetComponent(entity, "Vehicle"); + auto wheelComponent = m_World->GetComponent(entity, "Wheel"); - if (physicsComponent || sphereShapeComponent || boxShapeComponent || meshShapeComponent || staticMeshShapeComponent) + if (physicsComponent || sphereShapeComponent || boxShapeComponent || meshShapeComponent || staticMeshShapeComponent) { if (m_PhysicsData.find(entity) == m_PhysicsData.end()) { SetUpPhysicsState(entity, parent); } @@ -99,6 +100,7 @@ void Systems::PhysicsSystem::UpdateEntity(double dt, EntityID entity, EntityID p m_Constraints[std::make_pair(entityA, entityB)] = new btPoint2PointConstraint(*m_PhysicsData[entityA].RigidBody, *m_PhysicsData[entityB].RigidBody, pivotA, pivotB); m_DynamicsWorld->addConstraint(m_Constraints[std::make_pair(entityA, entityB)]); + } } } @@ -141,6 +143,64 @@ void Systems::PhysicsSystem::UpdateEntity(double dt, EntityID entity, EntityID p } } + + if (vehicleComponent ) + { + if (m_Vehicles.find(entity) == m_Vehicles.end()) { + if (m_PhysicsData.find(entity) == m_PhysicsData.end()){ + LOG_WARNING("Chassi missing rigidbody on vehicle i%", entity); + return; + } + + m_Vehicles[entity].VehicleRaycaster = new btDefaultVehicleRaycaster(m_DynamicsWorld); + m_Vehicles[entity].RaycastVehicle = new btRaycastVehicle(m_Vehicles[entity].Tuning, m_PhysicsData[entity].RigidBody, m_Vehicles[entity].VehicleRaycaster); + + //m_Vehicles[entity].RaycastVehicle->setCoordinateSystem(0, 1, 2); + m_DynamicsWorld->addVehicle(m_Vehicles[entity].RaycastVehicle); + + } + + + +// for (int i = 0; i < m_Vehicles[entity].RaycastVehicle->getNumWheels(); i++) +// { +// m_Vehicles[entity].RaycastVehicle->applyEngineForce(10, i); +// +// } + } + if (wheelComponent) + { + + EntityID carID = wheelComponent->CarID; + if (m_Vehicles.find(carID) != m_Vehicles.end()){ + auto wheels = &m_Vehicles[carID].Wheels; + if (wheels->find(entity) == wheels->end()) + { + wheels->insert(entity); + + btVector3 connectionPoint = btVector3(wheelComponent->ConnectionPoint.x, wheelComponent->ConnectionPoint.y, wheelComponent->ConnectionPoint.z); + btVector3 direction = btVector3(wheelComponent->Direction.x, wheelComponent->Direction.y, wheelComponent->Direction.z); + btVector3 axle = btVector3(wheelComponent->Axle.x, wheelComponent->Axle.y, wheelComponent->Axle.z); + + btWheelInfo& wheel = m_Vehicles[carID].RaycastVehicle->addWheel(connectionPoint, direction, axle, wheelComponent->SuspensionRestLength, wheelComponent->Radius, m_Vehicles[entity].Tuning, wheelComponent->IsFrontWheel); + + wheel.m_suspensionStiffness = wheelComponent->SuspensionStiffness; + wheel.m_wheelsDampingRelaxation = wheelComponent->SuspensionDamping; + wheel.m_wheelsDampingCompression = wheelComponent->SuspensionCompression; + wheel.m_frictionSlip = wheelComponent->FrictionSlip; + wheel.m_rollInfluence = 0.1f; + + + } + } + + + } + + + + + } void Systems::PhysicsSystem::OnComponentCreated(std::string type, std::shared_ptr component) diff --git a/src/Systems/PhysicsSystem.h b/src/Systems/PhysicsSystem.h index a094249..90a9049 100644 --- a/src/Systems/PhysicsSystem.h +++ b/src/Systems/PhysicsSystem.h @@ -12,9 +12,12 @@ #include "Components/BallSocketConstraint.h" #include "Components/HingeConstraint.h" #include "Components/SliderConstraint.h" +#include "Components/Vehicle.h" +#include "Components/Wheel.h" #include "btBulletDynamicsCommon.h" #include "LinearMath/btMatrix3x3.h" +#include namespace Systems { @@ -35,6 +38,10 @@ private: btSequentialImpulseConstraintSolver* m_Solver; btDiscreteDynamicsWorld* m_DynamicsWorld; + + //m_DynamicsWorld = new btDiscreteDynamicsWorld(m_Dispatcher, m_Broadphase, m_Solver, m_CollisionConfiguration); + + struct PhysicsData { btRigidBody* RigidBody; btMotionState* MotionState; @@ -43,6 +50,17 @@ private: std::map m_PhysicsData; std::map, btTypedConstraint*> m_Constraints; + + struct Vehicle + { + btVehicleRaycaster* VehicleRaycaster; + btRaycastVehicle* RaycastVehicle; + btRaycastVehicle::btVehicleTuning Tuning; + std::unordered_set Wheels; + }; + + std::map m_Vehicles; + void SetUpPhysicsState(EntityID entity, EntityID parent); void TearDownPhysicsState(EntityID entity, EntityID parent); }; diff --git a/vs12/Returngeance/Returngeance.vcxproj b/vs12/Returngeance/Returngeance.vcxproj index 81e3b0e..93ac2a5 100755 --- a/vs12/Returngeance/Returngeance.vcxproj +++ b/vs12/Returngeance/Returngeance.vcxproj @@ -137,6 +137,8 @@ + + diff --git a/vs12/Returngeance/Returngeance.vcxproj.filters b/vs12/Returngeance/Returngeance.vcxproj.filters index 0c5d439..03449ab 100755 --- a/vs12/Returngeance/Returngeance.vcxproj.filters +++ b/vs12/Returngeance/Returngeance.vcxproj.filters @@ -158,8 +158,14 @@ Components + Components + + Components + + Components + Components