diff --git a/src/Components/Wheel.h b/src/Components/Wheel.h index f000058..212aad5 100644 --- a/src/Components/Wheel.h +++ b/src/Components/Wheel.h @@ -37,3 +37,10 @@ private: } #endif // Components_Wheel_h__ + +/* +m_currentSuspensionLength +m_suspension +m_steeringOrientationChassisSpace +m_spinAngle +*/ \ No newline at end of file diff --git a/src/GameWorld.cpp b/src/GameWorld.cpp index 6c9e5d4..f40ac63 100755 --- a/src/GameWorld.cpp +++ b/src/GameWorld.cpp @@ -24,14 +24,14 @@ void GameWorld::Initialize() auto ground = CreateEntity(); auto transform = AddComponent(ground, "Transform"); transform->Position = glm::vec3(0, -5, 0); - transform->Scale = glm::vec3(1000.0f, 10.0f, 1000.0f); + transform->Scale = glm::vec3(400.0f, 10.0f, 400.0f); transform->Orientation = glm::quat(glm::vec3(0.0f, 0.0f, 0.0f)); auto model = AddComponent(ground, "Model"); model->ModelFile = "Models/Placeholders/PhysicsTest/Cube.obj"; auto box = AddComponent(ground, "Box"); - box->Width = 500; + box->Width = 200; box->Height = 5; - box->Depth = 500; + box->Depth = 200; auto physics = AddComponent(ground, "Physics"); physics->Mass = 10; @@ -66,13 +66,14 @@ void GameWorld::Initialize() auto ent = CreateEntity(car); auto transform = AddComponent(ent, "Transform"); transform->Scale = glm::vec3(1)/glm::vec3(3, 1, 5); - transform->Position = glm::vec3(1.1f, -0.5f, -1.3f); + transform->Position = glm::vec3(1.1f, -1.5f, -1.3f); auto Wheel = AddComponent(ent, "Wheel"); Wheel->Hardpoint = glm::vec3(1.1f, 0.f, -1.3f);// HACK: make into component Wheel->AxleID = 0; Wheel->Mass = 10; Wheel->Radius = 0.5f; Wheel->Steering = true; + Wheel->SuspensionStrength = 20.f; auto model = AddComponent(ent, "Model"); model->ModelFile = "Models/Placeholders/PhysicsTest/Cube2.obj"; CommitEntity(ent); @@ -82,13 +83,14 @@ void GameWorld::Initialize() auto ent = CreateEntity(car); auto transform = AddComponent(ent, "Transform"); transform->Scale = glm::vec3(1)/glm::vec3(3, 1, 5); - transform->Position = glm::vec3(-1.1f, -0.5f, -1.3f); + transform->Position = glm::vec3(-1.1f, -1.5f, -1.3f); auto Wheel = AddComponent(ent, "Wheel"); Wheel->Hardpoint = glm::vec3(-1.1f, 0.f, -1.3f); Wheel->AxleID = 0; Wheel->Mass = 10; Wheel->Radius = 0.5f; Wheel->Steering = true; + Wheel->SuspensionStrength = 20.f; auto model = AddComponent(ent, "Model"); model->ModelFile = "Models/Placeholders/PhysicsTest/Cube2.obj"; CommitEntity(ent); @@ -98,7 +100,7 @@ void GameWorld::Initialize() auto ent = CreateEntity(car); auto transform = AddComponent(ent, "Transform"); transform->Scale = glm::vec3(1)/glm::vec3(3, 1, 5); - transform->Position = glm::vec3(1.1f, -0.5f, 1.3f); + transform->Position = glm::vec3(1.1f, -1.5f, 1.3f); auto Wheel = AddComponent(ent, "Wheel"); Wheel->Hardpoint = glm::vec3(1.1f, 0.f, 1.3f); Wheel->AxleID = 1; @@ -106,6 +108,7 @@ void GameWorld::Initialize() Wheel->Radius = 0.5f; Wheel->Steering = false; Wheel->ConnectedToHandbrake = true; + Wheel->SuspensionStrength = 20.f; auto model = AddComponent(ent, "Model"); model->ModelFile = "Models/Placeholders/PhysicsTest/Cube2.obj"; CommitEntity(ent); @@ -115,7 +118,7 @@ void GameWorld::Initialize() auto ent = CreateEntity(car); auto transform = AddComponent(ent, "Transform"); transform->Scale = glm::vec3(1)/glm::vec3(3, 1, 5); - transform->Position = glm::vec3(-1.1f, -0.5f, 1.3f); + transform->Position = glm::vec3(-1.1f, -1.5f, 1.3f); auto Wheel = AddComponent(ent, "Wheel"); Wheel->Hardpoint = glm::vec3(-1.1f, 0.f, 1.3f); Wheel->AxleID = 1; @@ -123,6 +126,7 @@ void GameWorld::Initialize() Wheel->Radius = 0.5f; Wheel->Steering = false; Wheel->ConnectedToHandbrake = true; + Wheel->SuspensionStrength = 20.f; auto model = AddComponent(ent, "Model"); model->ModelFile = "Models/Placeholders/PhysicsTest/Cube2.obj"; CommitEntity(ent); @@ -133,22 +137,23 @@ void GameWorld::Initialize() - for(int i = 0; i < 50; i++) + for(int i = 0; i < 10; i++) { - auto ball = CreateEntity(); - auto transform = AddComponent(ball, "Transform"); - transform->Position = glm::vec3(243, 10 + i*10, 0); - transform->Scale = glm::vec3(10); + auto cube = CreateEntity(); + auto transform = AddComponent(cube, "Transform"); + transform->Position = glm::vec3(20, 10 + i*2, 0); + transform->Scale = glm::vec3(1); transform->Orientation = glm::quat(glm::vec3(0.0f, 0.0f, 0.0f)); - auto model = AddComponent(ball, "Model"); + auto model = AddComponent(cube, "Model"); model->ModelFile = "Models/Placeholders/PhysicsTest/Cube2.obj"; - auto physics = AddComponent(ball, "Physics"); + + auto physics = AddComponent(cube, "Physics"); physics->Mass = 100; - auto box = AddComponent(ball, "Box"); - box->Width = 5.f; - box->Height = 5.f; - box->Depth = 5.f; - CommitEntity(ball); + auto box = AddComponent(cube, "Box"); + box->Width = 0.5f; + box->Height = 0.5f; + box->Depth = 0.5f; + CommitEntity(cube); } { diff --git a/src/Systems/PhysicsSystem.cpp b/src/Systems/PhysicsSystem.cpp index 0ba5a2e..837ed5a 100644 --- a/src/Systems/PhysicsSystem.cpp +++ b/src/Systems/PhysicsSystem.cpp @@ -76,10 +76,13 @@ void Systems::PhysicsSystem::Update(double dt) continue; - - hkVector4 position(transformComponent->Position.x, transformComponent->Position.y, transformComponent->Position.z); - hkQuaternion rotation(transformComponent->Orientation.x, transformComponent->Orientation.y, transformComponent->Orientation.z, transformComponent->Orientation.w); - m_RigidBodies[entity]->setPositionAndRotation(position, rotation); + if(m_RigidBodies[entity]->isActive()) + { + hkVector4 position(transformComponent->Position.x, transformComponent->Position.y, transformComponent->Position.z); + hkQuaternion rotation(transformComponent->Orientation.x, transformComponent->Orientation.y, transformComponent->Orientation.z, transformComponent->Orientation.w); + m_RigidBodies[entity]->setPositionAndRotation(position, rotation); + } + } @@ -109,26 +112,31 @@ void Systems::PhysicsSystem::UpdateEntity(double dt, EntityID entity, EntityID p EntityID car = m_World->GetEntityParent(entity); if(m_Vehicles.find(car) != m_Vehicles.end()) { - hkVector4 position; - hkQuaternion orientation; - m_Vehicles[car]->calcCurrentPositionAndRotation(m_RigidBodies[car], m_Vehicles[car]->m_suspension, wheelComponent->ID, position, orientation); - hkVector4 chassisPosition = m_RigidBodies[car]->getPosition(); - hkQuaternion chassisOrientation = m_RigidBodies[car]->getRotation(); - glm::vec3 relativePos = glm::vec3(chassisPosition(0), chassisPosition(1), chassisPosition(2)) - glm::vec3(position(0), position(1), position(2)); - glm::quat relativeOrientation = glm::quat(orientation(3), orientation(0), orientation(1), orientation(2)); + m_Vehicles[car]->getChassis()->activate(); - transformComponent->Position = relativePos; - //transformComponent->Orientation = relativeOrientation; + hkVector4 hardPoint = m_Vehicles[car]->m_suspension->m_wheelParams[wheelComponent->ID].m_hardpointChassisSpace; + hkVector4 suspensionDirection = m_Vehicles[car]->m_suspension->m_wheelParams[wheelComponent->ID].m_directionChassisSpace; + hkReal suspensionLength = m_Vehicles[car]->m_wheelsInfo[wheelComponent->ID].m_currentSuspensionLength; + glm::vec3 position = glm::vec3(hardPoint(0) + (suspensionDirection(0) * suspensionLength), hardPoint(1) + (suspensionDirection(1) * suspensionLength), hardPoint(2) + (suspensionDirection(2) * suspensionLength)); + transformComponent->Position = position; + + 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 = glm::quat(steeringOrientation(3), steeringOrientation(0), steeringOrientation(1), steeringOrientation(2)) * glm::angleAxis(spinAngle, glm::vec3(1, 0, 0)); + transformComponent->Orientation = orientation; } } else { if(m_Vehicles.find(entity) != m_Vehicles.end()) { - hkVector4 position = m_RigidBodies[entity]->getPosition(); - transformComponent->Position = glm::vec3(position(0), position(1), position(2)); - hkQuaternion orientation = m_RigidBodies[entity]->getRotation(); - transformComponent->Orientation = glm::quat(orientation(3),orientation(0), orientation(1), orientation(2)); + if(m_RigidBodies[entity]->isActive()) + { + hkVector4 position = m_RigidBodies[entity]->getPosition(); + transformComponent->Position = glm::vec3(position(0), position(1), position(2)); + hkQuaternion orientation = m_RigidBodies[entity]->getRotation(); + transformComponent->Orientation = glm::quat(orientation(3),orientation(0), orientation(1), orientation(2)); + } } }