Added support for HkpExtendedMeshShape.

This commit is contained in:
ViktorLjung
2014-04-27 08:08:00 +02:00
parent cb6b0a58d8
commit 720cf3aa87
8 changed files with 153 additions and 207 deletions
+17
View File
@@ -0,0 +1,17 @@
#ifndef Components_MeshShape_h__
#define Components_MeshShape_h__
#include <string>
#include "Component.h"
namespace Components
{
struct MeshShape : Component
{
std::string ResourceName;
};
}
#endif // !Components_MeshShape_h__
+1 -1
View File
@@ -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;
+34 -101
View File
@@ -16,15 +16,14 @@ void GameWorld::Initialize()
{
auto ground = CreateEntity();
auto transform = AddComponent<Components::Transform>(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<Components::Model>(ground, "Model");
model->ModelFile = "Models/Placeholders/PhysicsTest/Cube.obj";
auto box = AddComponent<Components::Box>(ground, "Box");
box->Width = 200;
box->Height = 5;
box->Depth = 200;
model->ModelFile = "Models/Placeholders/Terrain/Terrain.obj";
auto meshShape = AddComponent<Components::MeshShape>(ground, "MeshShape");
meshShape->ResourceName = "Models/Placeholders/Terrain/Terrain.obj";
auto physics = AddComponent<Components::Physics>(ground, "Physics");
physics->Mass = 10;
@@ -37,16 +36,28 @@ void GameWorld::Initialize()
{
auto jeep = CreateEntity();
auto transform = AddComponent<Components::Transform>(jeep, "Transform");
transform->Position = glm::vec3(0, 2, 0);
transform->Position = glm::vec3(0, 10, 0);
auto physics = AddComponent<Components::Physics>(jeep, "Physics");
physics->Mass = 1200;
auto box = AddComponent<Components::Box>(jeep, "Box");
box->Width = 1.487f;
box->Height = 0.727f;
box->Depth = 2.594f;
physics->Mass = 800;
// auto box = AddComponent<Components::Box>(jeep, "Box");
// box->Width = 1.487f;
// box->Height = 0.727f;
// box->Depth = 2.594f;
auto meshShape = AddComponent<Components::MeshShape>(jeep, "MeshShape");
meshShape->ResourceName = "Models/JeepV2/Chassi/chassi.OBJ";
auto vehicle = AddComponent<Components::Vehicle>(jeep, "Vehicle");
auto light = AddComponent<Components::PointLight>(jeep, "PointLight");
light->Diffuse = glm::vec3(0, 1, 0);
light->constantAttenuation = 0.0005f;
light->linearAttenuation = 0.001f;
light->quadraticAttenuation = 0.001f;
AddComponent<Components::Input>(jeep, "Input");
@@ -69,7 +80,6 @@ void GameWorld::Initialize()
transform->Position = glm::vec3(0, -0.6577f, 0);
auto model = AddComponent<Components::Model>(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<Components::Transform>(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<Components::Wheel>(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<Components::Model>(ent, "Model");
model->ModelFile = "Models/Placeholders/PhysicsTest/Cube2.obj";
CommitEntity(ent);
}
{
// Front Left Wheel
auto ent = CreateEntity(car);
auto transform = AddComponent<Components::Transform>(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<Components::Wheel>(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<Components::Model>(ent, "Model");
model->ModelFile = "Models/Placeholders/PhysicsTest/Cube2.obj";
CommitEntity(ent);
}
{
// Back Right Wheel
auto ent = CreateEntity(car);
auto transform = AddComponent<Components::Transform>(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<Components::Wheel>(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<Components::Model>(ent, "Model");
model->ModelFile = "Models/Placeholders/PhysicsTest/Cube2.obj";
CommitEntity(ent);
}
{
// Back Left Wheel
auto ent = CreateEntity(car);
auto transform = AddComponent<Components::Transform>(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<Components::Wheel>(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<Components::Model>(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<Components::Transform>(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<Components::Model>(cube, "Model");
model->ModelFile = "Models/Placeholders/PhysicsTest/Cube2.obj";
model->ModelFile = "Models/cardboardBox/BoxFixed.obj";
auto physics = AddComponent<Components::Physics>(cube, "Physics");
physics->Mass = 100;
+74 -103
View File
@@ -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<Components::Sphere >(entity, "Sphere");
auto boxComponent = m_World->GetComponent<Components::Box >(entity, "Box");
auto meshShapeComponent = m_World->GetComponent<Components::MeshShape >(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<hkReal>* vertices = new std::vector<hkReal>;
std::vector<hkUint16>* vertexIndices = new std::vector<hkUint16>;
auto meshShape = m_World->GetResourceManager()->Load<OBJ>("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<Components::Transform>(entity, "Transform");
if (!transformComponent)
return;
auto physicsComponent = m_World->GetComponent<Components::Physics>(entity, "Physics");
if (!physicsComponent)
return;
auto sphereComponent = m_World->GetComponent<Components::Sphere >(entity, "Sphere");
auto boxComponent = m_World->GetComponent<Components::Box >(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<Components::Vehicle >(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)
{
+16
View File
@@ -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 <Common/Base/hkBase.h>
@@ -34,6 +36,8 @@
#include <Common/Visualize/hkVisualDebugger.h>
#include <Physics2012/Utilities/VisualDebugger/hkpPhysicsContext.h>
#include <Physics2012/Collide/Shape/Compound/Collection/ExtendedMeshShape/hkpExtendedMeshShape.h>
#include "Physics/VehicleSetup.h"
#include <unordered_map>
@@ -66,10 +70,22 @@ private:
void SetupPhysics(hkpWorld* physicsWorld);
std::unordered_map<EntityID, hkpRigidBody*> m_RigidBodies;
std::unordered_map<EntityID, hkpVehicleInstance*> m_Vehicles;
std::vector<EntityID> m_Wheels;
hkpVehicleInstance* Systems::PhysicsSystem::createVehicle(VehicleSetup& vehicleSetup, hkpRigidBody* chassis);
struct ExtendedShapeData
{
hkpExtendedMeshShape* ExtendedMeshShape;
std::vector<hkReal>* Vertices;
std::vector<hkUint16>* VertexIndices;
};
std::unordered_map<EntityID, ExtendedShapeData > m_hkpExtendedMeshShapes;
};
}
+2 -1
View File
@@ -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>("OBJ", resourceName)); });
rm->RegisterType("OBJ", [](std::string resourceName) { return new OBJ(resourceName); });
rm->RegisterType("Texture", [](std::string resourceName) { return new Texture(resourceName); });
}
+3 -1
View File
@@ -80,7 +80,7 @@
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>_CRT_SECURE_NO_WARNINGS;_MBCS;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<PreprocessorDefinitions>_CRT_SECURE_NO_WARNINGS;_MBCS;HK_CONFIG_SIMD=1;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<PrecompiledHeader>Create</PrecompiledHeader>
<PrecompiledHeaderFile>PrecompiledHeader.h</PrecompiledHeaderFile>
<EnableEnhancedInstructionSet>StreamingSIMDExtensions2</EnableEnhancedInstructionSet>
@@ -126,6 +126,7 @@
<ClInclude Include="..\..\src\Components\DirectionalLight.h" />
<ClInclude Include="..\..\src\Components\FreeSteering.h" />
<ClInclude Include="..\..\src\Components\Input.h" />
<ClInclude Include="..\..\src\Components\MeshShape.h" />
<ClInclude Include="..\..\src\Components\Model.h" />
<ClInclude Include="..\..\src\Components\ParticleEmitter.h" />
<ClInclude Include="..\..\src\Components\Physics.h" />
@@ -162,6 +163,7 @@
<ClInclude Include="..\..\src\Texture.h" />
<ClInclude Include="..\..\src\Util\GLError.h" />
<ClInclude Include="..\..\src\Util\Logging.h" />
<ClInclude Include="..\..\src\Util\UnorderedMapPair.h" />
<ClInclude Include="..\..\src\World.h" />
</ItemGroup>
<ItemGroup>
@@ -230,6 +230,12 @@
<ClInclude Include="..\..\src\Physics\VehicleSetup.h">
<Filter>Physics</Filter>
</ClInclude>
<ClInclude Include="..\..\src\Components\MeshShape.h">
<Filter>Physics\Components</Filter>
</ClInclude>
<ClInclude Include="..\..\src\Util\UnorderedMapPair.h">
<Filter>Util</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<None Include="..\..\src\Shaders\AABB.frag.glsl">