diff --git a/src/Components/MeshShape.h b/src/Components/MeshShape.h new file mode 100644 index 0000000..64084ab --- /dev/null +++ b/src/Components/MeshShape.h @@ -0,0 +1,17 @@ +#ifndef Components_MeshShape_h__ +#define Components_MeshShape_h__ + +#include + +#include "Component.h" + +namespace Components +{ + +struct MeshShape : Component +{ + std::string ResourceName; +}; + +} +#endif // !Components_MeshShape_h__ \ No newline at end of file diff --git a/src/Components/Vehicle.h b/src/Components/Vehicle.h index e61c713..9e4a59e 100644 --- a/src/Components/Vehicle.h +++ b/src/Components/Vehicle.h @@ -10,7 +10,7 @@ namespace Components struct Vehicle : Component { Vehicle() - : MaxTorque(500.0f), MinRPM(1000.0f), OptimalRPM(5500.0f), MaxRPM(7500.0f), MaxSteeringAngle(35), TopSpeed(50.0f) { } + : MaxTorque(500.0f), MinRPM(800.0f), OptimalRPM(4000.0f), MaxRPM(6000.0f), MaxSteeringAngle(35), TopSpeed(50.0f) { } float MaxTorque; float MinRPM; diff --git a/src/GameWorld.cpp b/src/GameWorld.cpp index 7926dcb..4ca0ac7 100755 --- a/src/GameWorld.cpp +++ b/src/GameWorld.cpp @@ -16,15 +16,14 @@ void GameWorld::Initialize() { auto ground = CreateEntity(); auto transform = AddComponent(ground, "Transform"); - transform->Position = glm::vec3(0, -5, 0); - transform->Scale = glm::vec3(400.0f, 10.0f, 400.0f); + transform->Position = glm::vec3(0, 0, 0); + //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 = 200; - box->Height = 5; - box->Depth = 200; + model->ModelFile = "Models/Placeholders/Terrain/Terrain.obj"; + auto meshShape = AddComponent(ground, "MeshShape"); + meshShape->ResourceName = "Models/Placeholders/Terrain/Terrain.obj"; + auto physics = AddComponent(ground, "Physics"); physics->Mass = 10; @@ -37,16 +36,28 @@ void GameWorld::Initialize() { auto jeep = CreateEntity(); auto transform = AddComponent(jeep, "Transform"); - transform->Position = glm::vec3(0, 2, 0); + transform->Position = glm::vec3(0, 10, 0); auto physics = AddComponent(jeep, "Physics"); - physics->Mass = 1200; - auto box = AddComponent(jeep, "Box"); - box->Width = 1.487f; - box->Height = 0.727f; - box->Depth = 2.594f; + physics->Mass = 800; + + +// auto box = AddComponent(jeep, "Box"); +// box->Width = 1.487f; +// box->Height = 0.727f; +// box->Depth = 2.594f; + + auto meshShape = AddComponent(jeep, "MeshShape"); + meshShape->ResourceName = "Models/JeepV2/Chassi/chassi.OBJ"; + auto vehicle = AddComponent(jeep, "Vehicle"); + auto light = AddComponent(jeep, "PointLight"); + light->Diffuse = glm::vec3(0, 1, 0); + light->constantAttenuation = 0.0005f; + light->linearAttenuation = 0.001f; + light->quadraticAttenuation = 0.001f; + AddComponent(jeep, "Input"); @@ -69,7 +80,6 @@ void GameWorld::Initialize() transform->Position = glm::vec3(0, -0.6577f, 0); auto model = AddComponent(chassis, "Model"); model->ModelFile = "Models/JeepV2/Chassi/chassi.OBJ"; - } { @@ -86,7 +96,7 @@ void GameWorld::Initialize() Wheel->Radius = 0.837f; Wheel->Steering = true; Wheel->SuspensionStrength = 40.f; - Wheel->Friction = 4.0f; + Wheel->Friction = 3.0f; Wheel->ConnectedToHandbrake = true; CommitEntity(wheel); } @@ -106,7 +116,7 @@ void GameWorld::Initialize() Wheel->Radius = 0.837f; Wheel->Steering = true; Wheel->SuspensionStrength = 40.f; - Wheel->Friction = 4.0f; + Wheel->Friction = 3.0f; Wheel->ConnectedToHandbrake = true; CommitEntity(wheel); } @@ -123,8 +133,9 @@ void GameWorld::Initialize() Wheel->Mass = 10; Wheel->Radius = 0.737f; Wheel->Steering = false; - Wheel->SuspensionStrength = 50.f; - Wheel->Friction = 4.0f; + Wheel->SuspensionStrength = 20.f; + Wheel->Friction = 3.0f; + Wheel->ConnectedToHandbrake = true; CommitEntity(wheel); } @@ -141,93 +152,15 @@ void GameWorld::Initialize() Wheel->Mass = 10; Wheel->Radius = 0.737f; Wheel->Steering = false; - Wheel->SuspensionStrength = 50.f; - Wheel->Friction = 4.0f; + Wheel->SuspensionStrength = 20.f; + Wheel->Friction = 3.0f; + Wheel->ConnectedToHandbrake = true; CommitEntity(wheel); } CommitEntity(jeep); } -/* - - { - // Front Right Wheel - 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, -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); - } - { - // Front Left Wheel - 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, -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); - } - { - // Back Right Wheel - 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, -1.5f, 1.3f); - auto Wheel = AddComponent(ent, "Wheel"); - Wheel->Hardpoint = glm::vec3(1.1f, 0.f, 1.3f); - Wheel->AxleID = 1; - Wheel->Mass = 10; - 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); - } - { - // Back Left Wheel - 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, -1.5f, 1.3f); - auto Wheel = AddComponent(ent, "Wheel"); - Wheel->Hardpoint = glm::vec3(-1.1f, 0.f, 1.3f); - Wheel->AxleID = 1; - Wheel->Mass = 10; - 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); - } - - CommitEntity(car); - } -*/ - - - - for(int i = 0; i < 10; i++) + for(int i = 0; i < 0; i++) { auto cube = CreateEntity(); auto transform = AddComponent(cube, "Transform"); @@ -235,7 +168,7 @@ void GameWorld::Initialize() transform->Scale = glm::vec3(1); transform->Orientation = glm::quat(glm::vec3(0.0f, 0.0f, 0.0f)); auto model = AddComponent(cube, "Model"); - model->ModelFile = "Models/Placeholders/PhysicsTest/Cube2.obj"; + model->ModelFile = "Models/cardboardBox/BoxFixed.obj"; auto physics = AddComponent(cube, "Physics"); physics->Mass = 100; diff --git a/src/Systems/PhysicsSystem.cpp b/src/Systems/PhysicsSystem.cpp index b4f8200..059629c 100644 --- a/src/Systems/PhysicsSystem.cpp +++ b/src/Systems/PhysicsSystem.cpp @@ -67,6 +67,7 @@ void Systems::PhysicsSystem::RegisterComponents(ComponentFactory* cf) cf->Register("Sphere", []() { return new Components::Sphere(); }); cf->Register("Vehicle", []() { return new Components::Vehicle(); }); cf->Register("Wheel", []() { return new Components::Wheel(); }); + cf->Register("MeshShape", []() { return new Components::MeshShape(); }); } void Systems::PhysicsSystem::Update(double dt) @@ -183,11 +184,12 @@ void Systems::PhysicsSystem::OnEntityCommit( EntityID entity ) auto sphereComponent = m_World->GetComponent(entity, "Sphere"); auto boxComponent = m_World->GetComponent(entity, "Box"); + auto meshShapeComponent = m_World->GetComponent(entity, "MeshShape"); - - hkpConvexShape* shape; + hkpShape* shape = nullptr; hkpRigidBodyCinfo rigidBodyInfo; hkMassProperties massProperties; + if (sphereComponent) { @@ -220,16 +222,83 @@ void Systems::PhysicsSystem::OnEntityCommit( EntityID entity ) rigidBodyInfo.m_motionType = hkpMotion::MOTION_BOX_INERTIA; } hkpInertiaTensorComputer::computeBoxSurfaceMassProperties(hkVector4(boxComponent->Width - thickness, boxComponent->Height - thickness, boxComponent->Depth - thickness), physicsComponent->Mass, thickness, massProperties); + + } + else if(meshShapeComponent) + { + std::vector* vertices = new std::vector; + std::vector* vertexIndices = new std::vector; + auto meshShape = m_World->GetResourceManager()->Load("OBJ", meshShapeComponent->ResourceName); + + for (auto &vertex : meshShape->Vertices) + { + hkReal x, y, z; + std::tie(x, y, z) = vertex; + vertices->push_back(x); + vertices->push_back(y); + vertices->push_back(z); + } + + int i = 0; + for (auto &face : meshShape->Faces) + { + for (auto &faceDef : face.Definitions) + { + /*hkReal x, y, z; + std::tie(x, y, z) = meshShape->Vertices.at(faceDef.VertexIndex - 1); + vertices.push_back(x); + vertices.push_back(y); + vertices.push_back(z);*/ + vertexIndices->push_back(faceDef.VertexIndex - 1); + //vertexIndices.push_back(i++); + } + } + + hkpExtendedMeshShape* mesh = new hkpExtendedMeshShape(); + mesh->setRadius( 0.05f); + { + hkpExtendedMeshShape::TrianglesSubpart part; + part.m_numTriangleShapes = meshShape->Faces.size(); + part.m_indexBase = vertexIndices->data(); + part.m_indexStriding = sizeof(hkUint16) * 3; + + part.m_numVertices = vertices->size() / 3; + part.m_vertexBase = vertices->data(); + part.m_vertexStriding = sizeof(hkReal) * 3; + + part.m_stridingType = hkpExtendedMeshShape::INDICES_INT16; + + + + mesh->addTrianglesSubpart(part); + } + + if (physicsComponent->Static) + { + rigidBodyInfo.m_motionType = hkpMotion::MOTION_FIXED; + } + else + { + rigidBodyInfo.m_motionType = hkpMotion::MOTION_BOX_INERTIA; + } + hkpInertiaTensorComputer::computeShapeVolumeMassProperties(mesh, physicsComponent->Mass, massProperties); + rigidBodyInfo.m_shape = mesh; + m_hkpExtendedMeshShapes[entity].ExtendedMeshShape = mesh; + m_hkpExtendedMeshShapes[entity].VertexIndices = vertexIndices; + m_hkpExtendedMeshShapes[entity].Vertices = vertices; + shape = mesh; } else { return; } + rigidBodyInfo.m_position.set(transformComponent->Position.x, transformComponent->Position.y, transformComponent->Position.z); rigidBodyInfo.m_inertiaTensor = massProperties.m_inertiaTensor; - rigidBodyInfo.m_centerOfMass = massProperties.m_centerOfMass; + //rigidBodyInfo.m_centerOfMass = massProperties.m_centerOfMass; rigidBodyInfo.m_mass = massProperties.m_mass; + // Create RigidBody hkpRigidBody* rigidBody = new hkpRigidBody(rigidBodyInfo); @@ -271,108 +340,10 @@ void Systems::PhysicsSystem::OnEntityCommit( EntityID entity ) shape->removeReference(); rigidBody->removeReference(); + + } } -/* - -void Systems::PhysicsSystem::SetUpPhysicsState(EntityID entity, EntityID parent) -{ - auto transformComponent = m_World->GetComponent(entity, "Transform"); - if (!transformComponent) - return; - - auto physicsComponent = m_World->GetComponent(entity, "Physics"); - if (!physicsComponent) - return; - - auto sphereComponent = m_World->GetComponent(entity, "Sphere"); - auto boxComponent = m_World->GetComponent(entity, "Box"); - - - hkpConvexShape* shape; - hkpRigidBodyCinfo rigidBodyInfo; - hkMassProperties massProperties; - - - if (sphereComponent) - { - shape = new hkpSphereShape(sphereComponent->Radius); - rigidBodyInfo.m_shape = shape; - - if (physicsComponent->Static) - { - rigidBodyInfo.m_motionType = hkpMotion::MOTION_FIXED; - } - else - { - rigidBodyInfo.m_motionType = hkpMotion::MOTION_SPHERE_INERTIA; - } - - hkpInertiaTensorComputer::computeSphereVolumeMassProperties(sphereComponent->Radius, physicsComponent->Mass, massProperties); - - } - else if (boxComponent) - { - hkReal thickness = 0.05; - shape = new hkpBoxShape(hkVector4(boxComponent->Width - thickness, boxComponent->Height - thickness, boxComponent->Depth - thickness)); - rigidBodyInfo.m_shape = shape; - if (physicsComponent->Static) - { - rigidBodyInfo.m_motionType = hkpMotion::MOTION_FIXED; - } - else - { - rigidBodyInfo.m_motionType = hkpMotion::MOTION_BOX_INERTIA; - } - hkpInertiaTensorComputer::computeBoxSurfaceMassProperties(hkVector4(boxComponent->Width - thickness, boxComponent->Height - thickness, boxComponent->Depth - thickness), physicsComponent->Mass, thickness, massProperties); - } - else - { - return; - } - - rigidBodyInfo.m_position.set(transformComponent->Position.x, transformComponent->Position.y, transformComponent->Position.z); - rigidBodyInfo.m_inertiaTensor = massProperties.m_inertiaTensor; - rigidBodyInfo.m_centerOfMass = massProperties.m_centerOfMass; - rigidBodyInfo.m_mass = massProperties.m_mass; - - // Create RigidBody - hkpRigidBody* rigidBody = new hkpRigidBody(rigidBodyInfo); - - - auto vehicleComponent = m_World->GetComponent(entity, "Vehicle"); - if (vehicleComponent && m_Vehicles.find(entity) == m_Vehicles.end()) - { - VehicleSetup vehicleSetup; - - // Create the basic vehicle. - m_Vehicles[entity] = new hkpVehicleInstance(rigidBody); - vehicleSetup.buildVehicle(m_PhysicsWorld, *m_Vehicles[entity]); - // Add the vehicle's entities and phantoms to the world - m_Vehicles[entity]->addToWorld(m_PhysicsWorld); - - m_RigidBodies[entity] = rigidBody; - - // The vehicle is an action - m_PhysicsWorld->addAction(m_Vehicles[entity]); - - //m_Vehicles[entity]->m_rpm = 0.0f; // Not sure why this one should be here - - shape->removeReference(); - rigidBody->removeReference(); - } - else - { - m_PhysicsWorld->addEntity(rigidBody); - m_RigidBodies[entity] = rigidBody; - shape->removeReference(); - rigidBody->removeReference(); - } -} -*/ - - - void Systems::PhysicsSystem::TearDownPhysicsState(EntityID entity, EntityID parent) { diff --git a/src/Systems/PhysicsSystem.h b/src/Systems/PhysicsSystem.h index f5ca539..e69a38c 100644 --- a/src/Systems/PhysicsSystem.h +++ b/src/Systems/PhysicsSystem.h @@ -8,6 +8,8 @@ #include "Components/Box.h" #include "Components/Vehicle.h" #include "Components/Input.h" +#include "Components/MeshShape.h" +#include "OBJ.h" // Math and base include #include @@ -34,6 +36,8 @@ #include #include +#include + #include "Physics/VehicleSetup.h" #include @@ -66,10 +70,22 @@ private: void SetupPhysics(hkpWorld* physicsWorld); std::unordered_map m_RigidBodies; + + std::unordered_map m_Vehicles; std::vector m_Wheels; hkpVehicleInstance* Systems::PhysicsSystem::createVehicle(VehicleSetup& vehicleSetup, hkpRigidBody* chassis); + + + + struct ExtendedShapeData + { + hkpExtendedMeshShape* ExtendedMeshShape; + std::vector* Vertices; + std::vector* VertexIndices; + }; + std::unordered_map m_hkpExtendedMeshShapes; }; } diff --git a/src/Systems/RenderSystem.cpp b/src/Systems/RenderSystem.cpp index fe254f4..60044f8 100755 --- a/src/Systems/RenderSystem.cpp +++ b/src/Systems/RenderSystem.cpp @@ -72,7 +72,8 @@ void Systems::RenderSystem::RegisterComponents(ComponentFactory* cf) void Systems::RenderSystem::RegisterResourceTypes(ResourceManager* rm) { - rm->RegisterType("Model", [rm](std::string resourceName) { return new Model(OBJ(resourceName), rm); }); + rm->RegisterType("Model", [rm](std::string resourceName) { return new Model(rm, *rm->Load("OBJ", resourceName)); }); + rm->RegisterType("OBJ", [](std::string resourceName) { return new OBJ(resourceName); }); rm->RegisterType("Texture", [](std::string resourceName) { return new Texture(resourceName); }); } diff --git a/vs11/Returngeance/Returngeance.vcxproj b/vs11/Returngeance/Returngeance.vcxproj index 65ee872..3702a27 100755 --- a/vs11/Returngeance/Returngeance.vcxproj +++ b/vs11/Returngeance/Returngeance.vcxproj @@ -80,7 +80,7 @@ true true true - _CRT_SECURE_NO_WARNINGS;_MBCS;%(PreprocessorDefinitions) + _CRT_SECURE_NO_WARNINGS;_MBCS;HK_CONFIG_SIMD=1;%(PreprocessorDefinitions) Create PrecompiledHeader.h StreamingSIMDExtensions2 @@ -126,6 +126,7 @@ + @@ -162,6 +163,7 @@ + diff --git a/vs11/Returngeance/Returngeance.vcxproj.filters b/vs11/Returngeance/Returngeance.vcxproj.filters index dfd85bb..5117384 100755 --- a/vs11/Returngeance/Returngeance.vcxproj.filters +++ b/vs11/Returngeance/Returngeance.vcxproj.filters @@ -230,6 +230,12 @@ Physics + + Physics\Components + + + Util +