diff --git a/src/GameWorld.cpp b/src/GameWorld.cpp index 4565bd7..683f6a9 100755 --- a/src/GameWorld.cpp +++ b/src/GameWorld.cpp @@ -8,8 +8,8 @@ void GameWorld::Initialize() { auto camera = CreateEntity(); auto transform = AddComponent(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() / 8.f, 0.f, 0.f)); auto cameraComp = AddComponent(camera, "Camera"); cameraComp->FarClip = 2000.f; @@ -21,67 +21,30 @@ void GameWorld::Initialize() auto terrain = CreateEntity(); auto transform = AddComponent(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() / 20.f)); auto model = AddComponent(terrain, "Model"); model->ModelFile = "Models/PhysicsTest/Plane.obj"; auto physics = AddComponent(terrain, "Physics"); auto Box = AddComponent(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(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(entity, "Physics"); - physics->Mass = 10; - physics->Friction = 0.5; - - auto compound = AddComponent(entity, "CompoundShape"); - - { - auto entity0 = CreateEntity(entity); - auto transform = AddComponent(entity0, "Transform"); - transform->Scale = glm::vec3(1.f); - auto model = AddComponent(entity0, "Model"); - model->ModelFile = "Models/PhysicsTest/Sphere.obj"; - - auto sphere = AddComponent(entity0, "SphereShape"); - sphere->Radius = 0.5f; - } - - { - auto entity1 = CreateEntity(entity); - auto transform = AddComponent(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(entity1, "Model"); - model->ModelFile = "Models/PhysicsTest/Cube.obj"; - - auto box = AddComponent(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(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(entity, "Model"); - model->ModelFile = "Models/PhysicsTest/Cube.obj"; + model->ModelFile = "Models/PhysicsTest/ArrowCube.obj"; auto physics = AddComponent(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(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(entity1, "Model"); + model->ModelFile = "Models/PhysicsTest/ArrowCube.obj"; + + auto physics = AddComponent(entity1, "Physics"); + physics->Mass = 0; + + auto box = AddComponent(entity1, "BoxShape"); + box->Width = 0.5; + box->Height = 0.5; + box->Depth = 0.5; + + auto hingeConstraint = AddComponent(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(); + hingeConstraint->HighLimit = glm::pi(); + 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() diff --git a/src/GameWorld.h b/src/GameWorld.h index 1f728d7..0061f0c 100755 --- a/src/GameWorld.h +++ b/src/GameWorld.h @@ -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: diff --git a/src/Systems/PhysicsSystem.cpp b/src/Systems/PhysicsSystem.cpp index 34d0eba..30c1b88 100644 --- a/src/Systems/PhysicsSystem.cpp +++ b/src/Systems/PhysicsSystem.cpp @@ -86,6 +86,73 @@ void Systems::PhysicsSystem::UpdateEntity(double dt, EntityID entity, EntityID p TearDownPhysicsState(entity, parent); } } + + auto ballSocketComponent = m_World->GetComponent(entity, "BallSocketConstraint"); + auto sliderComponent = m_World->GetComponent(entity, "SliderConstraint"); + auto hingeComponent = m_World->GetComponent(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) diff --git a/src/Systems/PhysicsSystem.h b/src/Systems/PhysicsSystem.h index 9928018..3e2d611 100644 --- a/src/Systems/PhysicsSystem.h +++ b/src/Systems/PhysicsSystem.h @@ -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 m_PhysicsData; - + std::map, btTypedConstraint*> m_Constraints; void SetUpPhysicsState(EntityID entity, EntityID parent); void TearDownPhysicsState(EntityID entity, EntityID parent); - - - }; } diff --git a/vs12/Returngeance/Returngeance.vcxproj b/vs12/Returngeance/Returngeance.vcxproj index 4933bfe..ffd3d9e 100755 --- a/vs12/Returngeance/Returngeance.vcxproj +++ b/vs12/Returngeance/Returngeance.vcxproj @@ -108,6 +108,7 @@ + @@ -115,12 +116,14 @@ + + diff --git a/vs12/Returngeance/Returngeance.vcxproj.filters b/vs12/Returngeance/Returngeance.vcxproj.filters index 695be4f..1df308b 100755 --- a/vs12/Returngeance/Returngeance.vcxproj.filters +++ b/vs12/Returngeance/Returngeance.vcxproj.filters @@ -145,6 +145,15 @@ Components + + Components + + + Components + + + Components +