Physics!!!!!1111oneoneonetwo

This commit is contained in:
ViktorLjung
2014-04-06 03:39:50 +02:00
parent d6fc82c91e
commit 1d0747076f
12 changed files with 373 additions and 18 deletions
+3 -1
View File
@@ -8,7 +8,9 @@ namespace Components
struct BoxShape : Component
{
float height, width;
float Height;
float Width;
float Depth;
};
}
+3 -4
View File
@@ -1,7 +1,6 @@
#ifndef Components_Physics_h__
#define Components_Physics_h__
//#include "btBulletDynamicsCommon.h"
#include "Component.h"
namespace Components
@@ -9,9 +8,9 @@ namespace Components
struct Physics : Component
{
float Mass;
float Inertia[3];
//btRigidBody RigidBody;
float Mass = 0;
float Friction = 0;
glm::vec3 Gravity = glm::vec3(0, -9.82f, 0);
};
}
+2 -1
View File
@@ -8,7 +8,8 @@ namespace Components
struct SphereShape : Component
{
float radius;
float Radius;
float RollingFriction;
};
}
+113 -6
View File
@@ -10,24 +10,115 @@ void GameWorld::Initialize()
auto camera = CreateEntity();
transform = AddComponent<Components::Transform>(camera, "Transform");
transform->Position.y = 10.f;
transform->Position.z = 150.f;
transform->Position.y = 100.f;
transform->Orientation = glm::quat(glm::vec3(glm::pi<float>() / 8.f, 0.f, 0.f));
auto cameraComp = AddComponent<Components::Camera>(camera, "Camera");
cameraComp->FarClip = 2000.f;
AddComponent(camera, "Input");
auto freeSteering = AddComponent<Components::FreeSteering>(camera, "FreeSteering");
auto terrain = CreateEntity();
transform = AddComponent<Components::Transform>(terrain, "Transform");
transform->Scale = glm::vec3(.05f);
model = AddComponent<Components::Model>(terrain, "Model");
model->ModelFile = "Models/terrain/terrain.obj";
transform->Scale = glm::vec3(1000.0f);
transform->Orientation = glm::quat(glm::vec3(0.0f, 0.0f, glm::pi<float>()/20.f));
model = AddComponent<Components::Model>(terrain, "Model");
model->ModelFile = "Models/PhysicsTest/Plane.obj";
auto physics = AddComponent<Components::Physics>(terrain, "Physics");
auto Box = AddComponent<Components::BoxShape>(terrain, "BoxShape");
Box->Width = 1;
Box->Height = 0;
Box->Depth = 1;
physics->Friction = 0.5f;
auto tank = CreateEntity();
terrain = CreateEntity();
transform = AddComponent<Components::Transform>(terrain, "Transform");
transform->Scale = glm::vec3(1000.0f);
transform->Position = glm::vec3(-500, 0, 0);
transform->Orientation = glm::quat(glm::vec3(0.0f, 0.0f, -glm::pi<float>() / 20.f));
model = AddComponent<Components::Model>(terrain, "Model");
model->ModelFile = "Models/PhysicsTest/Plane.obj";
physics = AddComponent<Components::Physics>(terrain, "Physics");
Box = AddComponent<Components::BoxShape>(terrain, "BoxShape");
Box->Width = 1;
Box->Height = 0;
Box->Depth = 1;
physics->Friction = 0.5f;
auto Boxed = CreateEntity();
transform = AddComponent<Components::Transform>(Boxed, "Transform");
transform->Position.y = 100.f;
transform->Position.x = 4.f;
transform->Scale = glm::vec3(5.0f);
model = AddComponent<Components::Model>(Boxed, "Model");
model->ModelFile = "Models/PhysicsTest/Cube.obj";
physics = AddComponent<Components::Physics>(Boxed, "Physics");
physics->Mass = 10;
physics->Friction = 0.5f;
Box = AddComponent<Components::BoxShape>(Boxed, "BoxShape");
Box->Width = 1;
Box->Height = 1;
Box->Depth = 1;
for (int i = 0; i < 20; i++)
{
auto Sphered = CreateEntity();
transform = AddComponent<Components::Transform>(Sphered, "Transform");
transform->Position.y = 15.f;
transform->Position.x = 4.f;
transform->Scale = glm::vec3(1.0f);
model = AddComponent<Components::Model>(Sphered, "Model");
model->ModelFile = "Models/PhysicsTest/Sphere.obj";
physics = AddComponent<Components::Physics>(Sphered, "Physics");
physics->Mass = 100;
physics->Friction = 0.5f;
auto sphere = AddComponent<Components::SphereShape>(Sphered, "SphereShape");
sphere->Radius = 1;
sphere->RollingFriction = 0.5f;
{
auto tank_top = CreateEntity(Sphered);
transform = AddComponent<Components::Transform>(tank_top, "Transform");
transform->Scale = glm::vec3(1.0f/100.f);
model = AddComponent<Components::Model>(tank_top, "Model");
model->ModelFile = "Models/tank/tank_top.obj";
{
auto tank_barrel = CreateEntity(tank_top);
transform = AddComponent<Components::Transform>(tank_barrel, "Transform");
model = AddComponent<Components::Model>(tank_barrel, "Model");
model->ModelFile = "Models/tank/tank_barrel.obj";
}
}
}
/*auto tank = CreateEntity();
transform = AddComponent<Components::Transform>(tank, "Transform");
transform->Position.y = 10.f;
transform->Position.x = 4.f;
transform->Scale = glm::vec3(.01f);
model = AddComponent<Components::Model>(tank, "Model");
model->ModelFile = "Models/tank/tank_base.obj";
physics = AddComponent<Components::Physics>(tank, "Physics");
physics->Mass = 100;
physics->Friction = 0.5f;*/
/*auto sphere = AddComponent<Components::SphereShape>(tank, "SphereShape");
sphere->Radius = 10.*(1.f / .01f);
sphere->RollingFriction = 0.5f;*/
/*
Box = AddComponent<Components::BoxShape>(tank, "BoxShape");
Box->Width = 100;
Box->Height = 50;
Box->Depth = 100;*/
/*
{
auto tank_top = CreateEntity(tank);
transform = AddComponent<Components::Transform>(tank_top, "Transform");
@@ -41,7 +132,8 @@ void GameWorld::Initialize()
model = AddComponent<Components::Model>(tank_barrel, "Model");
model->ModelFile = "Models/tank/tank_barrel.obj";
}
}
}*/
//auto light = CreateEntity();
//transform = AddComponent<Components::Transform>(light, "Transform");
@@ -72,6 +164,11 @@ void GameWorld::RegisterComponents()
m_ComponentFactory.Register("Template", []() { return new Components::Template(); });
m_ComponentFactory.Register("Transform", []() { return new Components::Transform(); });
m_ComponentFactory.Register("FreeSteering", []() { return new Components::FreeSteering(); });
m_ComponentFactory.Register("SphereShape", []() { return new Components::SphereShape(); });
m_ComponentFactory.Register("BoxShape", []() { return new Components::BoxShape(); });
m_ComponentFactory.Register("Physics", []() { return new Components::Physics(); });
}
void GameWorld::RegisterSystems()
@@ -84,7 +181,12 @@ void GameWorld::RegisterSystems()
//m_SystemFactory.Register("PlayerSystem", [this]() { return new Systems::PlayerSystem(this); });
m_SystemFactory.Register("FreeSteeringSystem", [this]() { return new Systems::FreeSteeringSystem(this); });
//m_SystemFactory.Register("SoundSystem", [this]() { return new Systems::SoundSystem(this); });
m_SystemFactory.Register("PhysicsSystem", [this]() { return new Systems::PhysicsSystem(this); });
m_SystemFactory.Register("RenderSystem", [this]() { return new Systems::RenderSystem(this, m_Renderer); });
}
void GameWorld::AddSystems()
@@ -97,5 +199,10 @@ void GameWorld::AddSystems()
//AddSystem("PlayerSystem");
AddSystem("FreeSteeringSystem");
//AddSystem("SoundSystem");
AddSystem("PhysicsSystem");
AddSystem("RenderSystem");
}
+5
View File
@@ -13,6 +13,7 @@
#include "Systems/FreeSteeringSystem.h"
#include "Systems/RenderSystem.h"
//#include "Systems/SoundSystem.h"
#include "Systems/PhysicsSystem.h"
#include "Components/Bounds.h"
#include "Components/Camera.h"
@@ -29,6 +30,10 @@
#include "Components/Template.h"
#include "Components/Transform.h"
#include "Components\BoxShape.h"
#include "Components\SphereShape.h"
#include "Components\Physics.h"
class GameWorld : public World
{
public:
+4
View File
@@ -0,0 +1,4 @@
#include "PrecompiledHeader.h"
#include "DebugSystem.h"
#include "World.h"
+21
View File
@@ -0,0 +1,21 @@
#ifndef DebugSystem_h__
#define DebugSystem_h__
#include "System.h"
#include "Components/Transform.h"
namespace Systems
{
class DebugSystem : public System
{
public:
DebugSystem(World* world)
: System(world) { }
void Update(double dt) override;
//void UpdateEntity(double dt, EntityID entity, EntityID parent) override;
};
}
#endif // DebugSystem_h__
+155
View File
@@ -0,0 +1,155 @@
#include "PrecompiledHeader.h"
#include "PhysicsSystem.h"
#include "World.h"
Systems::PhysicsSystem::PhysicsSystem(World* world) : System(world)
{
m_Broadphase = new btDbvtBroadphase();
m_CollisionConfiguration = new btDefaultCollisionConfiguration();
m_Dispatcher = new btCollisionDispatcher(m_CollisionConfiguration);
m_Solver = new btSequentialImpulseConstraintSolver();
m_DynamicsWorld = new btDiscreteDynamicsWorld(m_Dispatcher, m_Broadphase, m_Solver, m_CollisionConfiguration);
m_DynamicsWorld->setGravity(btVector3(0, -9.82f, 0));
}
void Systems::PhysicsSystem::Update(double dt)
{
// Update entity transform in physics world
for (auto pair : *m_World->GetEntities()) {
EntityID entity = pair.first;
EntityID parent = pair.second;
if (parent != 0)
continue;
if (m_PhysicsData.find(entity) != m_PhysicsData.end()) {
PhysicsData* physicsData = &m_PhysicsData[entity];
auto transformComponent = m_World->GetComponent<Components::Transform>(entity, "Transform");
btTransform transform;
transform.setFromOpenGLMatrix(glm::value_ptr(glm::translate(glm::mat4(), transformComponent->Position) * glm::toMat4(transformComponent->Orientation)));
physicsData->MotionState->setWorldTransform(transform);
physicsData->CollisionShape->setLocalScaling(btVector3(transformComponent->Scale.x, transformComponent->Scale.y, transformComponent->Scale.z));
auto physicsComponent = m_World->GetComponent<Components::Physics>(entity, "Physics");
physicsData->RigidBody->setGravity(btVector3(physicsComponent->Gravity.x, physicsComponent->Gravity.y, physicsComponent->Gravity.z));
}
}
m_DynamicsWorld->stepSimulation(dt, 10, 1.0f/60.0f);
}
void Systems::PhysicsSystem::UpdateEntity(double dt, 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");
auto sphereShapeComponent = m_World->GetComponent<Components::SphereShape>(entity, "SphereShape");
auto boxShapeComponent = m_World->GetComponent<Components::BoxShape>(entity, "BoxShape");
if (physicsComponent && (sphereShapeComponent || boxShapeComponent))
{
if (m_PhysicsData.find(entity) == m_PhysicsData.end()) {
SetUpPhysicsState(entity);
}
if (parent != 0)
return;
PhysicsData* physicsData = &m_PhysicsData[entity];
btTransform transform;
physicsData->MotionState->getWorldTransform(transform);
transformComponent->Position.x = transform.getOrigin().x();
transformComponent->Position.y = transform.getOrigin().y();
transformComponent->Position.z = transform.getOrigin().z();
btVector3 angle = transform.getRotation().getAxis();
transformComponent->Orientation.x = transform.getRotation().x();
transformComponent->Orientation.y = transform.getRotation().y();
transformComponent->Orientation.z = transform.getRotation().z();
transformComponent->Orientation.w = transform.getRotation().w();
}
else
{
if (m_PhysicsData.find(entity) != m_PhysicsData.end()) {
TearDownPhysicsState(entity);
}
}
}
void Systems::PhysicsSystem::OnComponentCreated(std::string type, std::shared_ptr<Component> component)
{
}
void Systems::PhysicsSystem::OnComponentRemoved(std::string type, Component* component)
{
}
void Systems::PhysicsSystem::SetUpPhysicsState(EntityID entity)
{
auto transformComponent = m_World->GetComponent<Components::Transform>(entity, "Transform");
auto physicsComponent = m_World->GetComponent<Components::Physics>(entity, "Physics");
auto sphereShapeComponent = m_World->GetComponent<Components::SphereShape>(entity, "SphereShape");
auto boxShapeComponent = m_World->GetComponent<Components::BoxShape>(entity, "BoxShape");
PhysicsData* physicsData = &m_PhysicsData[entity];
if (boxShapeComponent)
{
physicsData->CollisionShape = new btBoxShape(btVector3(boxShapeComponent->Width, boxShapeComponent->Height, boxShapeComponent->Depth));
}
else if(sphereShapeComponent)
{
physicsData->CollisionShape = new btSphereShape(sphereShapeComponent->Radius);
}
if (transformComponent)
{
btTransform transform;
transform.setFromOpenGLMatrix(glm::value_ptr(glm::translate(glm::mat4(), transformComponent->Position) * glm::toMat4(transformComponent->Orientation)));
physicsData->MotionState = new btDefaultMotionState(transform);
}
btVector3 inertia;
if (physicsComponent->Mass != 0)
physicsData->CollisionShape->calculateLocalInertia(physicsComponent->Mass, inertia);
btRigidBody::btRigidBodyConstructionInfo rigidBodyCI(physicsComponent->Mass, physicsData->MotionState, physicsData->CollisionShape, inertia);
rigidBodyCI.m_friction = physicsComponent->Friction;
if (sphereShapeComponent)
{
rigidBodyCI.m_rollingFriction = sphereShapeComponent->RollingFriction;
}
physicsData->RigidBody = new btRigidBody(rigidBodyCI);
m_DynamicsWorld->addRigidBody(physicsData->RigidBody);
}
void Systems::PhysicsSystem::TearDownPhysicsState(EntityID entity)
{
PhysicsData* physicsData = &m_PhysicsData[entity];
delete physicsData->RigidBody;
delete physicsData->MotionState;
delete physicsData->CollisionShape;
m_PhysicsData.erase(entity);
}
+49
View File
@@ -0,0 +1,49 @@
#ifndef PhysicsSystem_h__
#define PhysicsSystem_h__
#include "System.h"
#include "Components/Transform.h"
#include "Components/Physics.h"
#include "Components/BoxShape.h"
#include "Components/SphereShape.h"
#include "btBulletDynamicsCommon.h"
#include "LinearMath/btMatrix3x3.h"
namespace Systems
{
class PhysicsSystem : public System
{
public:
PhysicsSystem(World* world);
void Update(double dt) override;
void UpdateEntity(double dt, EntityID entity, EntityID parent) override;
void OnComponentCreated(std::string type, std::shared_ptr<Component> component) override;
void OnComponentRemoved(std::string type, Component* component) override;
private:
btBroadphaseInterface* m_Broadphase;
btDefaultCollisionConfiguration* m_CollisionConfiguration;
btCollisionDispatcher* m_Dispatcher;
btSequentialImpulseConstraintSolver* m_Solver;
btDiscreteDynamicsWorld* m_DynamicsWorld;
struct PhysicsData
{
btRigidBody* RigidBody;
btMotionState* MotionState;
btCollisionShape* CollisionShape;
};
std::map<EntityID, PhysicsData> m_PhysicsData;
void SetUpPhysicsState(EntityID entity);
void TearDownPhysicsState(EntityID entity);
};
}
#endif // PhysicsSystem_h__
+1 -1
View File
@@ -8,4 +8,4 @@ int main(int argc, char* argv[])
engine.Tick();
return 0;
}
}
+6 -3
View File
@@ -61,7 +61,7 @@
</ClCompile>
<Link>
<GenerateDebugInformation>true</GenerateDebugInformation>
<AdditionalDependencies>OpenAL32.lib;opengl32.lib;glu32.lib;SOIL.lib;glew32d.lib;glfw3dll.lib;BulletCollision_Debug.lib;%(AdditionalDependencies)</AdditionalDependencies>
<AdditionalDependencies>BulletCollision_Debug.lib;BulletDynamics_Debug.lib;LinearMath_Debug.lib;OpenAL32.lib;opengl32.lib;glu32.lib;SOIL.lib;glew32d.lib;glfw3dll.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Link>
<CustomBuildStep />
</ItemDefinitionGroup>
@@ -80,7 +80,7 @@
<GenerateDebugInformation>true</GenerateDebugInformation>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
<AdditionalDependencies>OpenAL32.lib;opengl32.lib;glu32.lib;SOIL.lib;glew32.lib;glfw3dll.lib;BulletCollision.lib;%(AdditionalDependencies)</AdditionalDependencies>
<AdditionalDependencies>BulletCollision.lib;BulletDynamics.lib;LinearMath.lib;OpenAL32.lib;opengl32.lib;glu32.lib;SOIL.lib;glew32.lib;glfw3dll.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Link>
<CustomBuildStep />
</ItemDefinitionGroup>
@@ -95,8 +95,10 @@
<ClCompile Include="..\..\src\Renderer.cpp" />
<ClCompile Include="..\..\src\ShaderProgram.cpp" />
<ClCompile Include="..\..\src\Skybox.cpp" />
<ClCompile Include="..\..\src\Systems\DebugSystem.cpp" />
<ClCompile Include="..\..\src\Systems\FreeSteeringSystem.cpp" />
<ClCompile Include="..\..\src\Systems\InputSystem.cpp" />
<ClCompile Include="..\..\src\Systems\PhysicsSystem.cpp" />
<ClCompile Include="..\..\src\Systems\RenderSystem.cpp" />
<ClCompile Include="..\..\src\Systems\TransformSystem.cpp" />
<ClCompile Include="..\..\src\Texture.cpp" />
@@ -110,7 +112,6 @@
<ClInclude Include="..\..\src\Components\BoxShape.h" />
<ClInclude Include="..\..\src\Components\Camera.h" />
<ClInclude Include="..\..\src\Components\Collision.h" />
<ClInclude Include="..\..\src\Components\CustomShape.h" />
<ClInclude Include="..\..\src\Components\DirectionalLight.h" />
<ClInclude Include="..\..\src\Components\FreeSteering.h" />
<ClInclude Include="..\..\src\Components\Input.h" />
@@ -137,8 +138,10 @@
<ClInclude Include="..\..\src\ShaderProgram.h" />
<ClInclude Include="..\..\src\Skybox.h" />
<ClInclude Include="..\..\src\System.h" />
<ClInclude Include="..\..\src\Systems\DebugSystem.h" />
<ClInclude Include="..\..\src\Systems\FreeSteeringSystem.h" />
<ClInclude Include="..\..\src\Systems\InputSystem.h" />
<ClInclude Include="..\..\src\Systems\PhysicsSystem.h" />
<ClInclude Include="..\..\src\Systems\RenderSystem.h" />
<ClInclude Include="..\..\src\Systems\TransformSystem.h" />
<ClInclude Include="..\..\src\Texture.h" />
+11 -2
View File
@@ -25,6 +25,12 @@
<ClCompile Include="..\..\src\Systems\FreeSteeringSystem.cpp">
<Filter>Systems</Filter>
</ClCompile>
<ClCompile Include="..\..\src\Systems\PhysicsSystem.cpp">
<Filter>Systems</Filter>
</ClCompile>
<ClCompile Include="..\..\src\Systems\DebugSystem.cpp">
<Filter>Systems</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<Filter Include="Util">
@@ -130,8 +136,11 @@
<ClInclude Include="..\..\src\Components\BoxShape.h">
<Filter>Components</Filter>
</ClInclude>
<ClInclude Include="..\..\src\Components\CustomShape.h">
<Filter>Components</Filter>
<ClInclude Include="..\..\src\Systems\PhysicsSystem.h">
<Filter>Systems</Filter>
</ClInclude>
<ClInclude Include="..\..\src\Systems\DebugSystem.h">
<Filter>Systems</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>