Skip to content

Commit

Permalink
Working camera rotation using interpolation
Browse files Browse the repository at this point in the history
  • Loading branch information
SpinnerX committed Nov 23, 2024
2 parents d330199 + 3adf7fe commit 5b2b165
Show file tree
Hide file tree
Showing 18 changed files with 345 additions and 4 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ set(ENGINE_INCLUDE_DIR ${CMAKE_CURRENT_LIST_DIR}/engine3d)

build_library(
DIRECTORIES src Editor TestApp
)
)
6 changes: 6 additions & 0 deletions Editor/Editor/Editor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@

namespace engine3d{

struct SimplePushConstantData{
glm::mat2 Transform{1.f};
glm::vec2 Offsets;
alignas(16) glm::vec3 Color;
};

EditorApplication::EditorApplication(const std::string& p_DebugName) : ApplicationInstance(p_DebugName) {
GlobalUpdateManager::GetInstance()->SubscribeApplicationUpdate(this, &EditorApplication::OnApplicationUpdate);
m_EditorScene = new EditorScene();
Expand Down
2 changes: 1 addition & 1 deletion TestApp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,4 @@ build_demos(
)

# target_include_directories(${PROJECT_NAME} PUBLIC ${SCENE_TEST_INCLUDE_DIR})
target_include_directories(${PROJECT_NAME} PUBLIC ./ ../)
target_include_directories(${PROJECT_NAME} PUBLIC ./ ../)
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#include <engine3d/Physics/JoltHandler.hpp>
// Jolt Includes
class BodyContainer
{
public:
BodyContainer();

operator JPH::BodyID() { return m_BodyID; }
JPH::BodyCreationSettings m_BodySettings;
JPH::BodyID m_BodyID;
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#include <Scenes/Assets/Components/Physics/PhysicsBody3D.hpp>
class BoxShaper : public BodyContainer
{
public:
BoxShaper();
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#include <Scenes/Assets/Components/Physics/PhysicsBody3D.hpp>
class SphereShaper : public BodyContainer
{
public:
SphereShaper();
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@

#pragma once

#include "Core/SceneManagment/Components/GameComponent.hpp"
#include <Scenes/Assets/Components/Bodies/BodyContainer.hpp>
#include <Core/TimeManagement/UpdateManagers/SyncUpdateManager.hpp>
#include <Core/SceneManagment/Components/SPComps/Transform.hpp>
#include <Jolt/Math/Quat.h>

#include <Jolt/Physics/Body/BodyInterface.h>
// This is a test version of this class as there is to much
class PhysicsBody3D: public engine3d::GameComponent
{
public:
PhysicsBody3D(BodyContainer * p_bodyCon);
~PhysicsBody3D();
void OnIntegrate();
void Update();
void LateUpdate();
void PhysicsUpdate();
void Begin();
BodyContainer* GetBody();

void SetScale(float x, float y, float z);
void SetPosition(float x, float y, float z);
void SetRotation(Quat quaternion);

private:
engine3d::Transform* m_Transform;
PhysicsBody3D() = default;
BodyContainer * bodyType;
JPH::BodyInterface* m_interface;
bool once = false;
};
16 changes: 16 additions & 0 deletions TestApp/SceneTest/Scenes/Assets/Components/testComp.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#pragma once

#include "Scenes/Assets/Components/Physics/PhysicsBody3D.hpp"
#include <Core/SceneManagment/Components/GameComponent.hpp>

class testComp : public engine3d::GameComponent
{
public:
void OnIntegrate();
void Update();
void LateUpdate();
void PhysicsUpdate();
private:
bool t_Secret = false;
BodyContainer * m_rb;
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#include "Core/ApplicationManager/Scene.hpp"
#include "Core/SceneManagment/SceneObjects/SceneObject.hpp"
class ShowCaseSceneInstance
{
public:
ShowCaseSceneInstance();
~ShowCaseSceneInstance();
engine3d::Scene* m_Scene;
std::vector<engine3d::SceneObject*> m_SceneObjects;
engine3d::Scene* GetScene();

private:
void CreateObjects();
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#include <Scenes/Assets/Components/Bodies/BodyContainer.hpp>

BodyContainer::BodyContainer()
{
// will add more settings and configurations later
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#include <Scenes/Assets/Components/Bodies/Shapes/BoxShaper.hpp>
using namespace JPH;
using namespace JPH::literals;
using namespace engine3d;
BoxShaper::BoxShaper()
{
JoltHandler * temp = engine3d::JoltHandler::GetInstance();
m_BodySettings = BodyCreationSettings(
temp->m_BoxShapeScaled,
RVec3(0.0_r, -1.0_r, 0.0_r),
Quat::sIdentity(),
EMotionType::Static,
Engine3DLayers::Static
);

m_BodyID = temp->getInterface()->CreateAndAddBody(
m_BodySettings,
EActivation::DontActivate);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#include <Scenes/Assets/Components/Bodies/Shapes/SphereShaper.hpp>
using namespace JPH;
using namespace JPH::literals;
using namespace engine3d;
SphereShaper::SphereShaper()
{
JoltHandler * temp = engine3d::JoltHandler::GetInstance();


m_BodySettings = BodyCreationSettings(
temp->m_SphereShapeScaled,
RVec3(0.0_r, 4.0_r, 0.0_r),
Quat::sIdentity(),
EMotionType::Dynamic,
Engine3DLayers::Dynamic
);

m_BodyID = temp->getInterface()->CreateAndAddBody(
m_BodySettings,
EActivation::Activate);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
#include <Scenes/Assets/Components/Physics/PhysicsBody3D.hpp>

#include <Jolt/Physics/Body/BodyCreationSettings.h>
#include <Jolt/Math/Vec3.h>
#include <Jolt/Physics/Body/BodyID.h>
#include <Jolt/Math/Quat.h>
#include <Jolt/Physics/EActivation.h>
#include <glm/fwd.hpp>
using namespace engine3d;
PhysicsBody3D::PhysicsBody3D(BodyContainer * p_bodyCon)
{
bodyType = p_bodyCon;
}

void PhysicsBody3D::OnIntegrate()
{
SyncUpdateManager::GetInstance()->Subscribe
(this, &PhysicsBody3D::Update);
SyncUpdateManager::GetInstance()->Subscribe
(this, &PhysicsBody3D::LateUpdate);
SyncUpdateManager::GetInstance()->Subscribe
(this, &PhysicsBody3D::PhysicsUpdate);

// Probably should be an event or called when activated
//! @note For now just calling Begin on integrate

Begin();
}

void PhysicsBody3D::Begin()
{
m_interface = engine3d::JoltHandler::GetInstance()->getInterface();
m_Transform = &m_GameObjectRef->SceneGetComponent<Transform>();
}

void PhysicsBody3D::Update()
{

//Convert Posiitons
m_Transform->m_Position.x = m_interface->
GetCenterOfMassPosition(bodyType->m_BodyID).GetX();
m_Transform->m_Position.y = m_interface->
GetCenterOfMassPosition(bodyType->m_BodyID).GetY();
m_Transform->m_Position.z = m_interface->
GetCenterOfMassPosition(bodyType->m_BodyID).GetZ();

//Convert Rotations
m_Transform->m_QuaterionRot.x = m_interface->
GetRotation(bodyType->m_BodyID).GetX();
m_Transform->m_QuaterionRot.y = m_interface->
GetRotation(bodyType->m_BodyID).GetY();
m_Transform->m_QuaterionRot.z = m_interface->
GetRotation(bodyType->m_BodyID).GetZ();
m_Transform->m_QuaterionRot.w = m_interface->
GetRotation(bodyType->m_BodyID).GetW();

//Convert Rotations
m_Transform->m_AxisRotation.x = m_interface->
GetRotation(bodyType->m_BodyID).GetEulerAngles().GetX();
m_Transform->m_AxisRotation.y = m_interface->
GetRotation(bodyType->m_BodyID).GetEulerAngles().GetY();
m_Transform->m_AxisRotation.z = m_interface->
GetRotation(bodyType->m_BodyID).GetEulerAngles().GetZ();

// std::print("Rotation: (X: {0}, Y: {1}, Z: {2})\n",
// m_Transform->m_AxisRotation.x,
// m_Transform->m_AxisRotation.y,
// m_Transform->m_AxisRotation.z);

}

void PhysicsBody3D::SetScale(float x, float y, float z)
{
m_interface->GetShape(bodyType->m_BodyID)->ScaleShape(RVec3(x,y,z));
}

void PhysicsBody3D::SetPosition(float x, float y, float z)
{
m_interface->SetPosition(
bodyType->m_BodyID,
RVec3(x,y,z),
JPH::EActivation::Activate);
}

void PhysicsBody3D::SetRotation(Quat quaternion)
{
m_interface->SetRotation(
bodyType->m_BodyID,
quaternion,
JPH::EActivation::Activate);
}

void PhysicsBody3D::LateUpdate()
{

}

void PhysicsBody3D::PhysicsUpdate()
{

}

BodyContainer* PhysicsBody3D::GetBody()
{
return bodyType;
}

PhysicsBody3D::~PhysicsBody3D()
{
delete bodyType;
}
49 changes: 49 additions & 0 deletions TestApp/SceneTest/src/Scenes/Assets/Components/testComp.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// #include <engine3d/Core/EngineLogger.hpp>
#include "Core/SceneManagment/Components/GameComponent.hpp"
#include "Physics/JoltHandler.hpp"
#include "Scenes/Assets/Components/Physics/PhysicsBody3D.hpp"
#include <Jolt/Physics/EActivation.h>
#include <engine3d/Core/EngineLogger.hpp>
#include <engine3d/Core/Event/InputPoll.hpp>
#include <engine3d/Core/Event/KeyCodes.hpp>
#include <engine3d/Core/TimeManagement/UpdateManagers/SyncUpdateManager.hpp>
#include <Scenes/Assets/Components/testComp.hpp>
// #include "Scene"

using namespace engine3d;
void testComp::OnIntegrate()
{;
SyncUpdateManager::GetInstance()->Subscribe
(this, &testComp::Update);
SyncUpdateManager::GetInstance()->Subscribe
(this, &testComp::LateUpdate);
SyncUpdateManager::GetInstance()->Subscribe
(this, &testComp::PhysicsUpdate);

// Need an activation and start funciton
m_rb = m_GameObjectRef->SceneGetComponent<PhysicsBody3D>().GetBody();

}

void testComp::Update()
{
if(InputPoll::IsKeyPressed(KeyCode::F5))
{
JoltHandler::GetInstance()->
getInterface()->AddForce(
m_rb->m_BodyID,
RVec3(0.0f,100000.0f,0.0f),
EActivation::Activate
);
}
}

void testComp::LateUpdate()
{

}

void testComp::PhysicsUpdate()
{

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#include "Core/ApplicationManager/Scene.hpp"
#include "Core/SceneManagment/SceneObjects/SceneObject.hpp"
#include "Scenes/Assets/Components/Physics/PhysicsBody3D.hpp"
#include "Scenes/Assets/Components/testComp.hpp"
#include <Scenes/Assets/Components/Bodies/Shapes/BoxShaper.hpp>
#include <Scenes/Assets/Components/Bodies/Shapes/SphereShaper.hpp>
#include <TestApp/SceneTest/Scenes/Assets/SceneInstances/ShowCaseSceneInstance.hpp>

ShowCaseSceneInstance::ShowCaseSceneInstance()
{
m_Scene = new engine3d::Scene();
printf("getting here\n");
CreateObjects();

}

void ShowCaseSceneInstance::CreateObjects()
{
//Platform
m_SceneObjects.push_back(new engine3d::SceneObject(m_Scene));
BodyContainer * l_Body = new BoxShaper();
m_SceneObjects[0]->AddComponent<PhysicsBody3D>(l_Body);
m_SceneObjects[0]->name = "Platform1";

//Sphere
m_SceneObjects.push_back(new engine3d::SceneObject(m_Scene));
l_Body = new SphereShaper();
m_SceneObjects[1]->AddComponent<PhysicsBody3D>(l_Body);
m_SceneObjects[1]->AddComponent<testComp>();
m_SceneObjects[1]->name = "Ball";

}

ShowCaseSceneInstance::~ShowCaseSceneInstance()
{
delete m_Scene;
for(auto obj : m_SceneObjects)
{
delete obj;
}
}
2 changes: 1 addition & 1 deletion sim_shader_transforms/simple_shader.vert
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,4 @@ void main(){
// // gl_Position = vec4(Position, 0.0, 1.0);
// gl_Position = vec4(push.transform * Position + push.offset, 0.0, 1.0);
// // fragColor = Color;
// }
// }
2 changes: 1 addition & 1 deletion src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -113,4 +113,4 @@ add_library(
${ENGINE_SRC_DIR}/Math/Interpolation.cpp

${ENGINE_SRC_DIR}/Physics/JoltHandler.cpp
)
)
Loading

0 comments on commit 5b2b165

Please sign in to comment.