-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Camera(sorta) and physics working on screen! (#73)
* 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
1 parent
8e91777
commit be58eed
Showing
24 changed files
with
583 additions
and
230 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
|
||
|
||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}; |
4 changes: 3 additions & 1 deletion
4
TestApp/Scenes/Assets/Components/Bodies/Shapes/SphereShaper.hpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
72 changes: 70 additions & 2 deletions
72
TestApp/src/Scenes/Assets/Components/Bodies/BodyContainer.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
24
TestApp/src/Scenes/Assets/Components/Bodies/Shapes/BoxShaper.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
24 changes: 6 additions & 18 deletions
24
TestApp/src/Scenes/Assets/Components/Bodies/Shapes/SphereShaper.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
81
TestApp/src/Scenes/Assets/Components/Camera/CameraFollow.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
|
||
} |
Oops, something went wrong.