Constraints working, I think... :D

This commit is contained in:
ViktorLjung
2014-04-07 00:08:09 +02:00
parent 0896f540fe
commit 4a6f0cad32
6 changed files with 132 additions and 50 deletions
+45 -46
View File
@@ -8,8 +8,8 @@ void GameWorld::Initialize()
{
auto camera = CreateEntity();
auto transform = AddComponent<Components::Transform>(camera, "Transform");
transform->Position.z = 150.f;
transform->Position.y = 100.f;
transform->Position.z = 20.f;
transform->Position.y = 20.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;
@@ -21,67 +21,30 @@ void GameWorld::Initialize()
auto terrain = CreateEntity();
auto transform = AddComponent<Components::Transform>(terrain, "Transform");
transform->Scale = glm::vec3(1000.0f);
transform->Orientation = glm::quat(glm::vec3(0.0f, 0.0f, 0.0f));
transform->Orientation = glm::quat(glm::vec3(0.0f, 0.0f, glm::pi<float>() / 20.f));
auto 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->Width = 0.5f;
Box->Height = 0;
Box->Depth = 1;
Box->Depth = 0.5f;
physics->Friction = 0.5f;
}
{
auto entity = CreateEntity();
auto transform = AddComponent<Components::Transform>(entity, "Transform");
transform->Scale = glm::vec3(1.0f);
transform->Position = glm::vec3(0, 15, 0);
transform->Orientation = glm::quat(glm::vec3(0.0f, 0.0f, 0.0f));
auto physics = AddComponent<Components::Physics>(entity, "Physics");
physics->Mass = 10;
physics->Friction = 0.5;
auto compound = AddComponent<Components::CompoundShape>(entity, "CompoundShape");
{
auto entity0 = CreateEntity(entity);
auto transform = AddComponent<Components::Transform>(entity0, "Transform");
transform->Scale = glm::vec3(1.f);
auto model = AddComponent<Components::Model>(entity0, "Model");
model->ModelFile = "Models/PhysicsTest/Sphere.obj";
auto sphere = AddComponent<Components::SphereShape>(entity0, "SphereShape");
sphere->Radius = 0.5f;
}
{
auto entity1 = CreateEntity(entity);
auto transform = AddComponent<Components::Transform>(entity1, "Transform");
transform->Scale = glm::vec3(1.0f);
transform->Position = glm::vec3(-1, 1, 0);
transform->Orientation = glm::quat(glm::vec3(0.0f, 0.0f, 0.0f));
auto model = AddComponent<Components::Model>(entity1, "Model");
model->ModelFile = "Models/PhysicsTest/Cube.obj";
auto box = AddComponent<Components::BoxShape>(entity1, "BoxShape");
box->Width = 0.5;
box->Height = 0.5;
box->Depth = 0.5;
}
for (int i = 0; i < 1; i++)
{
auto entity = CreateEntity();
auto transform = AddComponent<Components::Transform>(entity, "Transform");
transform->Scale = glm::vec3(1.0f);
transform->Position = glm::vec3(1, 6, 0);
transform->Position = glm::vec3(0, 10+i, 0);
transform->Orientation = glm::quat(glm::vec3(0.0f, 0.0f, 0.0f));
auto model = AddComponent<Components::Model>(entity, "Model");
model->ModelFile = "Models/PhysicsTest/Cube.obj";
model->ModelFile = "Models/PhysicsTest/ArrowCube.obj";
auto physics = AddComponent<Components::Physics>(entity, "Physics");
physics->Mass = 10;
@@ -90,13 +53,46 @@ void GameWorld::Initialize()
box->Width = 0.5;
box->Height = 0.5;
box->Depth = 0.5;
{
auto entity1 = CreateEntity();
auto transform = AddComponent<Components::Transform>(entity1, "Transform");
transform->Scale = glm::vec3(1.0f);
transform->Position = glm::vec3(-5.f, 10+i, 0);
transform->Orientation = glm::quat(glm::vec3(0.0f, 0.0f, 0.0f));
auto model = AddComponent<Components::Model>(entity1, "Model");
model->ModelFile = "Models/PhysicsTest/ArrowCube.obj";
auto physics = AddComponent<Components::Physics>(entity1, "Physics");
physics->Mass = 0;
auto box = AddComponent<Components::BoxShape>(entity1, "BoxShape");
box->Width = 0.5;
box->Height = 0.5;
box->Depth = 0.5;
auto hingeConstraint = AddComponent<Components::HingeConstraint>(entity1, "HingeConstraint");
hingeConstraint->EntityA = entity;
hingeConstraint->EntityB = entity1;
hingeConstraint->AxisA = glm::vec3(0, 0, 1);
hingeConstraint->AxisB = glm::vec3(0, 0, 1);
hingeConstraint->LowLimit = -glm::pi<float>();
hingeConstraint->HighLimit = glm::pi<float>();
hingeConstraint->PivotA = glm::vec3(-5, 0, 0);
hingeConstraint->PivotB = glm::vec3(0, 0, 0);
}
}
}
}
void GameWorld::Update(double dt)
@@ -125,6 +121,9 @@ void GameWorld::RegisterComponents()
m_ComponentFactory.Register("SphereShape", []() { return new Components::SphereShape(); });
m_ComponentFactory.Register("BoxShape", []() { return new Components::BoxShape(); });
m_ComponentFactory.Register("HingeConstraint", []() { return new Components::HingeConstraint(); });
m_ComponentFactory.Register("BallSocketConstraint", []() { return new Components::BallSocketConstraint(); });
m_ComponentFactory.Register("SliderConstraint", []() { return new Components::SliderConstraint(); });
}
void GameWorld::RegisterSystems()
+4
View File
@@ -35,6 +35,10 @@
#include "Components/SphereShape.h"
#include "Components/Physics.h"
#include "Components/BallSocketConstraint.h"
#include "Components/HingeConstraint.h"
#include "Components/SliderConstraint.h"
class GameWorld : public World
{
public:
+67
View File
@@ -86,6 +86,73 @@ void Systems::PhysicsSystem::UpdateEntity(double dt, EntityID entity, EntityID p
TearDownPhysicsState(entity, parent);
}
}
auto ballSocketComponent = m_World->GetComponent<Components::BallSocketConstraint>(entity, "BallSocketConstraint");
auto sliderComponent = m_World->GetComponent<Components::SliderConstraint>(entity, "SliderConstraint");
auto hingeComponent = m_World->GetComponent<Components::HingeConstraint>(entity, "HingeConstraint");
if (ballSocketComponent)
{
EntityID entityA = ballSocketComponent->EntityA;
EntityID entityB = ballSocketComponent->EntityB;
if (m_Constraints.find(std::make_pair(entityA, entityB)) == m_Constraints.end())
{
if (m_PhysicsData.find(entityA) != m_PhysicsData.end() && m_PhysicsData.find(entityB) != m_PhysicsData.end())
{
btVector3 pivotA = btVector3(ballSocketComponent->PivotA.x, ballSocketComponent->PivotA.y, ballSocketComponent->PivotA.z);
btVector3 pivotB = btVector3(ballSocketComponent->PivotB.x, ballSocketComponent->PivotB.y, ballSocketComponent->PivotB.z);
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)]);
}
}
}
else if (sliderComponent)
{
EntityID entityA = sliderComponent->EntityA;
EntityID entityB = sliderComponent->EntityB;
if (m_Constraints.find(std::make_pair(entityA, entityB)) == m_Constraints.end())
{
if (m_PhysicsData.find(entityA) != m_PhysicsData.end() && m_PhysicsData.find(entityB) != m_PhysicsData.end())
{
btTransform transformA;
m_PhysicsData[entityA].MotionState->getWorldTransform(transformA);
btTransform transformB;
m_PhysicsData[entityB].MotionState->getWorldTransform(transformB);
m_Constraints[std::make_pair(entityA, entityB)] = new btSliderConstraint(*m_PhysicsData[entityA].RigidBody, *m_PhysicsData[entityB].RigidBody, transformA, transformB, true);
m_DynamicsWorld->addConstraint(m_Constraints[std::make_pair(entityA, entityB)]);
}
}
}
else if (hingeComponent)
{
EntityID entityA = hingeComponent->EntityA;
EntityID entityB = hingeComponent->EntityB;
if (m_Constraints.find(std::make_pair(entityA, entityB)) == m_Constraints.end())
{
if (m_PhysicsData.find(entityA) != m_PhysicsData.end() && m_PhysicsData.find(entityB) != m_PhysicsData.end())
{
btVector3 PivotA = btVector3(hingeComponent->PivotA.x, hingeComponent->PivotA.y, hingeComponent->PivotA.z);
btVector3 PivotB = btVector3(hingeComponent->PivotB.x, hingeComponent->PivotB.y, hingeComponent->PivotB.z);
btVector3 AxisA = btVector3(hingeComponent->AxisA.x, hingeComponent->AxisA.y, hingeComponent->AxisA.z);
btVector3 AxisB = btVector3(hingeComponent->AxisB.x, hingeComponent->AxisB.y, hingeComponent->AxisB.z);
btHingeConstraint *hingeConstraint = new btHingeConstraint(*m_PhysicsData[entityA].RigidBody, *m_PhysicsData[entityB].RigidBody, PivotA, PivotB, AxisA, AxisB, true);
hingeConstraint->setLimit(hingeComponent->LowLimit, hingeComponent->HighLimit, hingeComponent->Softness, hingeComponent->BiasFactor, hingeComponent->RelaxationFactor);
m_Constraints[std::make_pair(entityA, entityB)] = hingeConstraint;
m_DynamicsWorld->addConstraint(m_Constraints[std::make_pair(entityA, entityB)]);
}
}
}
}
void Systems::PhysicsSystem::OnComponentCreated(std::string type, std::shared_ptr<Component> component)
+4 -4
View File
@@ -7,6 +7,9 @@
#include "Components/CompoundShape.h"
#include "Components/BoxShape.h"
#include "Components/SphereShape.h"
#include "Components/BallSocketConstraint.h"
#include "Components/HingeConstraint.h"
#include "Components/SliderConstraint.h"
#include "btBulletDynamicsCommon.h"
#include "LinearMath/btMatrix3x3.h"
@@ -37,13 +40,10 @@ namespace Systems
btCollisionShape* CollisionShape;
};
std::map<EntityID, PhysicsData> m_PhysicsData;
std::map<std::pair<EntityID, EntityID>, btTypedConstraint*> m_Constraints;
void SetUpPhysicsState(EntityID entity, EntityID parent);
void TearDownPhysicsState(EntityID entity, EntityID parent);
};
}
+3
View File
@@ -108,6 +108,7 @@
<ClInclude Include="..\..\src\Camera.h" />
<ClInclude Include="..\..\src\Color.h" />
<ClInclude Include="..\..\src\Component.h" />
<ClInclude Include="..\..\src\Components\BallSocketConstraint.h" />
<ClInclude Include="..\..\src\Components\Bounds.h" />
<ClInclude Include="..\..\src\Components\BoxShape.h" />
<ClInclude Include="..\..\src\Components\Camera.h" />
@@ -115,12 +116,14 @@
<ClInclude Include="..\..\src\Components\CompoundShape.h" />
<ClInclude Include="..\..\src\Components\DirectionalLight.h" />
<ClInclude Include="..\..\src\Components\FreeSteering.h" />
<ClInclude Include="..\..\src\Components\HingeConstraint.h" />
<ClInclude Include="..\..\src\Components\Input.h" />
<ClInclude Include="..\..\src\Components\Model.h" />
<ClInclude Include="..\..\src\Components\ParticleEmitter.h" />
<ClInclude Include="..\..\src\Components\Physics.h" />
<ClInclude Include="..\..\src\Components\PointLight.h" />
<ClInclude Include="..\..\src\Components\PowerUp.h" />
<ClInclude Include="..\..\src\Components\SliderConstraint.h" />
<ClInclude Include="..\..\src\Components\SoundEmitter.h" />
<ClInclude Include="..\..\src\Components\SphereShape.h" />
<ClInclude Include="..\..\src\Components\Sprite.h" />
@@ -145,6 +145,15 @@
<ClInclude Include="..\..\src\Components\CompoundShape.h">
<Filter>Components</Filter>
</ClInclude>
<ClInclude Include="..\..\src\Components\SliderConstraint.h">
<Filter>Components</Filter>
</ClInclude>
<ClInclude Include="..\..\src\Components\HingeConstraint.h">
<Filter>Components</Filter>
</ClInclude>
<ClInclude Include="..\..\src\Components\BallSocketConstraint.h">
<Filter>Components</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<None Include="..\..\src\Shaders\AABB.frag.glsl">