Skip to content

Commit

Permalink
Camera(sorta) and physics working on screen! (#73)
Browse files Browse the repository at this point in the history
* Connected the physics to the renderer.

* Got the physics running with the visuals, correctly this time!

* Failed to get camera working but am on the right track!

---------

Co-authored-by: ZacharyH777 <[email protected]>
  • Loading branch information
ZacharyH777 and ZHowe1 authored Nov 27, 2024
1 parent 8e91777 commit be58eed
Show file tree
Hide file tree
Showing 24 changed files with 583 additions and 230 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ set(ENGINE_INCLUDE_DIR ${CMAKE_CURRENT_LIST_DIR}/engine3d)
find_package(tinyobjloader REQUIRED)

build_library(
DIRECTORIES src Editor TestApp
DIRECTORIES src TestApp #Editor
LINK_LIBRARIES tinyobjloader::tinyobjloader
)
19 changes: 12 additions & 7 deletions Editor/Editor/EditorScene.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ namespace engine3d{
SceneObject* m_CameraObject = new SceneObject(m_Scene);
m_CameraObject->AddComponent<Camera>();
auto& camera_transform = m_CameraObject->GetComponent<Transform>();
camera_transform.m_Position = {-1.f, 5.f, -20.f};
camera_transform.SetPos<glm::vec3>( {-1.f, 5.f, -20.f});
// camera_transform.m_AxisRotation = {glm::radians(180.0f), 0.f, 0.f};
auto camera = m_CameraObject->GetComponent<Camera>();
m_CameraObjects.push_back(m_CameraObject);
Expand All @@ -44,7 +44,7 @@ namespace engine3d{
// -----------------------------
SceneObject* cube1 = new SceneObject(m_Scene);
auto& cube1_transform = cube1->GetComponent<Transform>();
cube1_transform.m_Position = {.0f, .0f, 2.5};
cube1_transform.SetPos<glm::vec3>( {.0f, .0f, 2.5});
cube1_transform.m_Scale = {10.5f, 10.5f, 10.5f};
// cube1_transform.m_AxisRotation = ToQuat(glm::vec3(glm::radians(180.0f), 0.f, 0.f));
cube1_transform.m_AxisRotation = {
Expand All @@ -59,15 +59,15 @@ namespace engine3d{
SceneObject* cube2 = new SceneObject(m_Scene);
auto& cube2_transform = cube2->GetComponent<Transform>();
// auto aspect_ratio = ApplicationInstance::GetWindow().GetAspectRatio();
cube2_transform.m_Position = {5.f, .0f, -7.f};
cube2_transform.SetPos<glm::vec3>( {5.f, .0f, -7.f});
cube2_transform.m_Scale = {5.5f, 5.5f, 5.5};

cube2->SetMesh(cube_mesh);

SceneObject* sphere_point_light = new SceneObject(m_Scene);
Mesh mesh = Mesh::LoadModel("3d_models/tutorial/sphere.obj");
auto& sphere_transform = sphere_point_light->GetComponent<Transform>();
sphere_transform.m_Position = {-10.0, 3.0, -1.0};
sphere_transform.SetPos<glm::vec3>( {-10.0, 3.0, -1.0});
sphere_transform.m_Scale = {1.f, 1.f, 1.f};
sphere_point_light->SetMesh(mesh);

Expand Down Expand Up @@ -134,11 +134,16 @@ namespace engine3d{
pos_sensitivity += (m_MousePosition.y - InputPoll::GetMouseY()) * invert_pos.x;
}

if(glm::dot(move_dir, move_dir) > std::numeric_limits<float>::epsilon()){
transform.m_Position += m_MoveSpeed * (SyncUpdateManager::GetInstance()->m_SyncLocalDeltaTime) * glm::normalize(move_dir) * pos_sensitivity;
if(glm::dot(move_dir, move_dir) > std::numeric_limits<float>::epsilon())
{
transform.SetPos<glm::vec3>(
transform.GetPos<glm::vec3>() +
m_MoveSpeed *
(SyncUpdateManager::GetInstance()->m_SyncLocalDeltaTime) *
glm::normalize(move_dir) * pos_sensitivity);
}

camera.SetViewXYZ(transform.m_Position, transform.m_AxisRotation);
camera.SetViewXYZ(transform.GetPos<glm::vec3>(), transform.m_AxisRotation);


camera.SetPerspectiveProjection(glm::radians(50.f), ApplicationInstance::GetWindow().GetAspectRatio(), 1.f, 1000.f);
Expand Down
4 changes: 4 additions & 0 deletions TestApp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ build_demos(
Scenes/Assets/WorldInstances/ShowCaseWorldInstance.hpp
src/Scenes/Assets/WorldInstances/ShowCaseWorldInstance.cpp

#Camera
Scenes/Assets/Components/Camera/CameraFollow.hpp
src/Scenes/Assets/Components/Camera/CameraFollow.cpp

#Graphics
Scenes/Assets/Components/Graphics/SpriteRender3D.hpp
src/Scenes/Assets/Components/Graphics/SpriteRender3D.cpp
Expand Down
29 changes: 27 additions & 2 deletions TestApp/Scenes/Assets/Components/Bodies/BodyContainer.hpp
Original file line number Diff line number Diff line change
@@ -1,12 +1,37 @@
#pragma once
// #include <engine3d/Physics/JoltHandler.hpp>
#include "Physics/Interfaces/BPLayerInterfaceHandler.hpp"
#include <Jolt/Core/Core.h>
#include <Jolt/Physics/Collision/Shape/Shape.h>
#include <engine3d/Physics/JoltHandler.hpp>
// Jolt Includes
#include <Jolt/Physics/Body/MotionType.h>
#include <Jolt/Physics/Collision/ObjectLayer.h>
#include <Jolt/Physics/Collision/Shape/RotatedTranslatedShape.h>
#include <Jolt/Physics/Collision/Shape/ScaledShape.h>
#include <Jolt/Physics/Collision/Shape/CompoundShape.h>
//Other includes
#include <glm/fwd.hpp>
class BodyContainer
{
public:
BodyContainer();
BodyContainer() = default;
~BodyContainer();

operator JPH::BodyID() { return m_BodyID; }
JPH::BodyCreationSettings m_BodySettings;
JPH::BodyCreationSettings* m_BodySettings = nullptr;
JPH::BodyID m_BodyID;

void CreateRotatedType(JPH::Vec3 p_Rot);
void CreateScaledType(JPH::Vec3 p_Scale);
void CreateCompoundType(JPH::Vec3 p_Rot, JPH::Vec3 p_Scale);
JPH::BodyID CreateBody(EActivation p_ActivationType ,JPH::RVec3 p_Position);

protected:
Shape* BaseShape = nullptr;
Shape* ParentShape = nullptr;
JPH::EMotionType m_MotionType = EMotionType::Static;
JPH::ObjectLayer m_LayerType = Engine3DLayers::Static;


};
4 changes: 3 additions & 1 deletion TestApp/Scenes/Assets/Components/Bodies/Shapes/BoxShaper.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#pragma once
#include <Scenes/Assets/Components/Physics/PhysicsBody3D.hpp>
class BoxShaper : public BodyContainer
{
public:
BoxShaper();
BoxShaper() = delete;
BoxShaper(JPH::EMotionType p_MotionType, const JPH::ObjectLayer p_LayerType);
};
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#pragma once
#include <Scenes/Assets/Components/Physics/PhysicsBody3D.hpp>
class SphereShaper : public BodyContainer
{
public:
SphereShaper();
SphereShaper() = delete;
SphereShaper(JPH::EMotionType p_MotionType, const JPH::ObjectLayer p_LayerType);
};
16 changes: 16 additions & 0 deletions TestApp/Scenes/Assets/Components/Camera/CameraFollow.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#pragma once
#include <engine3d/Core/SceneManagment/Components/GameComponent.hpp>
#include <engine3d/Core/SceneManagment/Components/SPComps/Transform.hpp>

class CameraFollow : public engine3d::GameComponent
{
public:
CameraFollow()=delete;
CameraFollow(engine3d::Transform &p_transform);

void OnIntegrate();
void LateUpdate();
private:
engine3d::Transform* m_PlayerTransform;
engine3d::Transform* m_Transform;
};
6 changes: 3 additions & 3 deletions TestApp/Scenes/Assets/Components/Physics/PhysicsBody3D.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ class PhysicsBody3D: public engine3d::GameComponent
void Begin();
BodyContainer* GetBody();

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

private:
engine3d::Transform* m_Transform;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,6 @@ class ShowCaseWorldInstance
glm::vec2 m_MousePosition;
float m_MoveSpeed = {5.f};
float m_LookSpeed = {1.5f};

bool callOnce = false;
};
72 changes: 70 additions & 2 deletions TestApp/src/Scenes/Assets/Components/Bodies/BodyContainer.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,74 @@
#include <Scenes/Assets/Components/Bodies/BodyContainer.hpp>
using namespace JPH::literals;

BodyContainer::BodyContainer()
JPH::BodyID BodyContainer::CreateBody(EActivation p_ActivationType,JPH::RVec3 p_Position)
{
engine3d::JoltHandler* l_Handler = engine3d::JoltHandler::GetInstance();
auto* l_Shape = &ParentShape;

if(m_BodySettings == nullptr)
{
// Create the type of body framework
m_BodySettings = new BodyCreationSettings(
*l_Shape,
p_Position,
Quat::sIdentity(),
m_MotionType,
m_LayerType
);
}
else
{
m_BodySettings->SetShape(*l_Shape);
l_Handler->getInterface()->RemoveBody(m_BodyID);
l_Handler->getInterface()->DestroyBody(m_BodyID);
m_BodyID = l_Handler->getInterface()->CreateAndAddBody(*m_BodySettings, p_ActivationType);
return m_BodyID;
}


printf("Getting here5.3\n");

return m_BodyID = l_Handler->getInterface()->CreateAndAddBody(
*m_BodySettings,p_ActivationType);
}

void BodyContainer::CreateRotatedType(JPH::Vec3 p_Rot)
{
// will add more settings and configurations later
ParentShape = new RotatedTranslatedShape(
BaseShape->GetCenterOfMass(),
Quat::sEulerAngles(p_Rot),
BaseShape
);
}
void BodyContainer::CreateScaledType(JPH::Vec3 p_Scale)
{
ParentShape = new ScaledShape(
BaseShape,
p_Scale
);
}
void BodyContainer::CreateCompoundType(JPH::Vec3 p_Rot, JPH::Vec3 p_Scale)
{
//In this order to prevent sheering
// Scale
ParentShape = new ScaledShape(
BaseShape,
p_Scale
);

// Rotate
ParentShape = new RotatedTranslatedShape(
ParentShape->GetCenterOfMass(),
Quat::sEulerAngles(p_Rot),
ParentShape
);


}

BodyContainer::~BodyContainer()
{
delete ParentShape;
delete BaseShape;
}
24 changes: 8 additions & 16 deletions TestApp/src/Scenes/Assets/Components/Bodies/Shapes/BoxShaper.cpp
Original file line number Diff line number Diff line change
@@ -1,19 +1,11 @@
#include <Scenes/Assets/Components/Bodies/Shapes/BoxShaper.hpp>
using namespace JPH;
using namespace JPH::literals;
using namespace engine3d;
BoxShaper::BoxShaper()
#include <Jolt/Physics/Collision/Shape/BoxShape.h>
BoxShaper::BoxShaper(JPH::EMotionType p_MotionType, const JPH::ObjectLayer p_LayerType)
{
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);
printf("Getting here2\n");
BaseShape = new JPH::BoxShape(Vec3(1.0f, 1.0f, 1.0f));
ParentShape = BaseShape;
printf("Getting here3\n");
m_MotionType = p_MotionType;
m_LayerType = p_LayerType;
}
Original file line number Diff line number Diff line change
@@ -1,21 +1,9 @@
#include <Scenes/Assets/Components/Bodies/Shapes/SphereShaper.hpp>
using namespace JPH;
using namespace JPH::literals;
using namespace engine3d;
SphereShaper::SphereShaper()
#include <Jolt/Physics/Collision/Shape/SphereShape.h>
SphereShaper::SphereShaper(JPH::EMotionType p_MotionType, const JPH::ObjectLayer p_LayerType)
{
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);
BaseShape = new SphereShape(2.5f);
ParentShape = BaseShape;
m_MotionType = p_MotionType;
m_LayerType = p_LayerType;
}
81 changes: 81 additions & 0 deletions TestApp/src/Scenes/Assets/Components/Camera/CameraFollow.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
#include "Core/EngineLogger.hpp"
#include "Core/SceneManagment/Components/SPComps/Camera.hpp"
#include <cstdio>
#include <engine3d/Physics/JoltHandler.hpp>
#include <Core/SceneManagment/SceneObjects/SceneObject.hpp>
#include <Scenes/Assets/Components/Physics/PhysicsBody3D.hpp>
#include <Scenes/Assets/Components/Camera/CameraFollow.hpp>
#include <engine3d/Core/TimeManagement/UpdateManagers/SyncUpdateManager.hpp>
#include <Jolt/Math/Quat.h>
#include <glm/fwd.hpp>
#include <glm/geometric.hpp>
#include <glm/gtc/matrix_transform.hpp>

#define GLM_ENABLE_EXPERIMENTAL
#include <glm/gtx/quaternion.hpp>

const float RADIUS = 20.0f;
const float HEIGHT = 10.0f;


// namespace ToPhysics
// {
// glm::vec3 FollowObject(const glm::vec3& targetPosition, const glm::vec3& velocity, float radius) {
// // Avoid division by zero by handling near-zero velocity components
// glm::vec3 adjustedVelocity = velocity;
// if (glm::length(velocity) < 0.001f) {
// adjustedVelocity = glm::vec3(0.0f, 0.0f, -1.0f); // Default fallback direction
// }

// // Calculate the direction opposite to the velocity
// glm::vec3 normVel = -glm::normalize(glm::vec3(adjustedVelocity.x, 0.0f, adjustedVelocity.z));

// // Scale by the desired follow radius
// glm::vec3 followOffset = normVel * radius;

// // Calculate the camera's new position behind the target
// return targetPosition + followOffset;
// }
// };

CameraFollow::CameraFollow(engine3d::Transform &p_transform)
{
m_PlayerTransform = &p_transform;
}

void CameraFollow::OnIntegrate()
{
engine3d::SyncUpdateManager::GetInstance()->Subscribe
(this, &CameraFollow::LateUpdate);

// Probably should be an event or called when activated
//! @note For now just calling Begin on integrate
m_Transform = &m_GameObjectRef->GetComponent<engine3d::Transform>();;
}

void CameraFollow::LateUpdate()
{

engine3d::SceneObject* player = m_PlayerTransform->GetParent();
PhysicsBody3D* rb = &player->GetComponent<PhysicsBody3D>();
auto l_interface = engine3d::JoltHandler::GetInstance()->getInterface();
auto player_velocity = l_interface->GetLinearVelocity(rb->GetBody()->m_BodyID);
// glm::vec3 newPos = ToPhysics::FollowObject(
// m_PlayerTransform->GetPos<glm::vec3>(),
// glm::vec3{player_velocity.GetZ(),player_velocity.GetY(),player_velocity.GetZ()},
// RADIUS);

m_Transform->SetPos<glm::vec3>({
m_PlayerTransform->m_Position.x,
m_PlayerTransform->m_Position.y + HEIGHT,
m_PlayerTransform->m_Position.z - RADIUS
});

m_GameObjectRef->GetComponent<engine3d::Camera>().SetViewTarget(
{
m_Transform->m_Position.x,
m_Transform->m_Position.y,
m_Transform->m_Position.z},
m_PlayerTransform->m_Position);

}
Loading

0 comments on commit be58eed

Please sign in to comment.