Added Camera Collision WIP #278

Merged
maverickdgg merged 2 commits from SP3-141-Camera-System into main 2022-11-25 11:08:11 +08:00
8 changed files with 106 additions and 16 deletions
Showing only changes of commit 428f8f29c6 - Show all commits

View File

@ -1,4 +1,4 @@
Start in Fullscreen: false
Starting Scene ID: 97158628
Starting Scene ID: 86098106
Window Size: {x: 1920, y: 1080}
Window Title: SHADE Engine

View File

@ -8599,6 +8599,7 @@
Arm Length: 1
Look At Camera Origin: true
Target Offset: {x: 0, y: 0, z: 0}
Camera Collision: true
IsActive: true
Scripts:
- Type: SHADE_Scripting.ThirdPersonCamera

View File

@ -7,8 +7,8 @@ namespace SHADE
{
SHCameraArmComponent::SHCameraArmComponent()
:pitch(0.0f), yaw(0.0f), armLength(1.0f),offset(), dirty(true), lookAtCameraOrigin(true)
, targetOffset(0.0f)
:pitch(0.0f), yaw(0.0f), armLength(1.0f), offset(), enableCameraCollision(false), lookAtCameraOrigin(true)
, targetOffset(0.0f),ray()
{
}
@ -39,22 +39,23 @@ namespace SHADE
return targetOffset;
}
void SHCameraArmComponent::SetPitch(float pitch) noexcept
{
this->pitch = pitch;
dirty = true;
}
void SHCameraArmComponent::SetYaw(float yaw) noexcept
{
this->yaw = yaw;
dirty = true;
//dirty = true;
}
void SHCameraArmComponent::SetArmLength(float length) noexcept
{
this->armLength = length;
dirty = true;
//dirty = true;
}
void SHCameraArmComponent::SetTargetOffset(SHVec3 offset) noexcept
@ -62,6 +63,8 @@ namespace SHADE
this->targetOffset = offset;
}
}//namespace SHADE
@ -76,6 +79,7 @@ RTTR_REGISTRATION
.property("Arm Length", &SHCameraArmComponent::GetArmLength, &SHCameraArmComponent::SetArmLength)
.property("Look At Camera Origin", &SHCameraArmComponent::lookAtCameraOrigin)
.property("Target Offset", &SHCameraArmComponent::GetTargetOffset, &SHCameraArmComponent::SetTargetOffset)
.property("Camera Collision", &SHCameraArmComponent::enableCameraCollision)
;
}

View File

@ -5,9 +5,14 @@
#include "ECS_Base/Components/SHComponent.h"
#include "Math/SHMatrix.h"
#include "SH_API.h"
#include "Math/SHRay.h"
namespace SHADE
{
class SHBox;
class SHRay;
class SH_API SHCameraArmComponent final: public SHComponent
{
private:
@ -15,15 +20,18 @@ namespace SHADE
float yaw;
float armLength;
bool dirty;
SHVec3 offset;
SHVec3 targetOffset;
SHRay ray;
public:
friend class SHCameraSystem;
SHCameraArmComponent();
virtual ~SHCameraArmComponent() = default;
bool lookAtCameraOrigin;
bool enableCameraCollision;
//Getters
//SHMatrix const& GetMatrix() const noexcept;
SHVec3 const& GetOffset() const noexcept;
@ -31,12 +39,14 @@ namespace SHADE
float GetYaw() const noexcept;
float GetArmLength() const noexcept;
SHVec3 GetTargetOffset() const noexcept;
//Setters
void SetPitch(float pitch) noexcept;
void SetYaw(float yaw) noexcept;
void SetArmLength(float length) noexcept;
void SetTargetOffset(SHVec3 offset)noexcept;
void SetTargetOffset(SHVec3 offset) noexcept;
protected:

View File

@ -10,6 +10,10 @@
#include "Scene/SHSceneManager.h"
#include "ECS_Base/Managers/SHSystemManager.h"
#include "Editor/SHEditor.h"
#include "Math/Geometry/SHBox.h"
#include "Math/SHRay.h"
#include "Physics/System/SHPhysicsSystem.h"
namespace SHADE
{
@ -96,7 +100,7 @@ namespace SHADE
if (editorCameraArm.armLength < 1.0f)
editorCameraArm.armLength = 1.0f;
UpdatePivotArmComponent(editorCameraArm);
UpdateCameraArmComponent(editorCameraArm);
editorCamera.offset = editorCameraArm.GetOffset();
@ -132,10 +136,12 @@ namespace SHADE
return &editorCamera;
}
void SHCameraSystem::UpdatePivotArmComponent(SHCameraArmComponent& pivot) noexcept
void SHCameraSystem::UpdateCameraArmComponent(SHCameraArmComponent& pivot) noexcept
{
if (pivot.dirty)
{
SHVec3 offset{ 0.0f,0.0f, pivot.GetArmLength() };
offset = SHVec3::RotateX(offset, -(SHMath::DegreesToRadians(pivot.GetPitch())));
@ -145,10 +151,61 @@ namespace SHADE
//pivot.rtMatrix = SHMatrix::RotateX(SHMath::DegreesToRadians(pivot.GetPitch()))
// * SHMatrix::RotateY(SHMath::DegreesToRadians(pivot.GetYaw()))
// * SHMatrix::Translate(SHVec3(0.0f , 0.0f, pivot.GetArmLength()));
pivot.offset = offset;
if (!pivot.enableCameraCollision)
{
return;
}
SHCameraComponent* camera = SHComponentManager::GetComponent_s<SHCameraComponent>(pivot.GetEID());
SHTransformComponent* transform = SHComponentManager::GetComponent_s<SHTransformComponent>(pivot.GetEID());
auto physicsSystem = SHSystemManager::GetSystem<SHPhysicsSystem>();
if (camera == nullptr || transform == nullptr)
return;
/*if (SHComponentManager::HasComponent<SHTransformComponent>(camera->GetEID()) == true && camera != &editorCamera)
{
auto transform = SHComponentManager::GetComponent<SHTransformComponent>(camera->GetEID());
SHVec3 rotation = transform->GetWorldRotation();
camera->pitch = SHMath::RadiansToDegrees(rotation.x);
camera->yaw = SHMath::RadiansToDegrees(rotation.y);
camera->roll = SHMath::RadiansToDegrees(rotation.z);
camera->position = transform->GetWorldPosition();
camera->dirtyView = true;
}*/
pivot.ray.position = camera->GetPosition() + pivot.targetOffset;
pivot.ray.direction = SHVec3::Normalise((camera->position + offset)- pivot.ray.position);
//SHLOG_INFO("Ray position: {},{},{} direction:{},{},{}",pivot.ray.position.x, pivot.ray.position.y, pivot.ray.position.z,pivot.ray.direction.x, pivot.ray.direction.y, pivot.ray.direction.z)
auto result = physicsSystem->Raycast(pivot.ray );
if (result && result.distance < pivot.GetArmLength())
{
SHVec3 newOffset = SHVec3{ 0.0f,0.0f, result.distance * 0.8f };
newOffset = SHVec3::RotateX(newOffset, -(SHMath::DegreesToRadians(pivot.GetPitch())));
newOffset = SHVec3::RotateY(newOffset, (SHMath::DegreesToRadians(pivot.GetYaw())));
pivot.offset = newOffset;
//SHLOG_INFO("CAMERA COLLISION HIT, {}", result.distance);
}
else
{
//SHLOG_INFO("CAMERA COLLISION CANT HIT CAMERA");
}
// pivot.rtMatrix = SHMatrix::Inverse(pivot.rtMatrix);
}
}
@ -291,7 +348,7 @@ namespace SHADE
for (auto& pivot : pivotDense)
{
if(SHSceneManager::CheckNodeAndComponentsActive<SHCameraArmComponent>(pivot.GetEID()))
system->UpdatePivotArmComponent(pivot);
system->UpdateCameraArmComponent(pivot);
}
for (auto& cam : dense)
@ -390,7 +447,9 @@ namespace SHADE
}
void SHCameraSystem::SetCameraViewMatrix(SHCameraComponent& camera, SHMatrix const& viewMatrix) noexcept
{
SHVec3 pos;
DecomposeViewMatrix(viewMatrix, camera.pitch, camera.yaw, camera.roll, camera.position);
camera.dirtyView = true;
}

View File

@ -26,7 +26,7 @@ namespace SHADE
void UpdateCameraComponent(SHCameraComponent& camera) noexcept;
void UpdatePivotArmComponent(SHCameraArmComponent& pivot) noexcept;
void UpdateCameraArmComponent(SHCameraArmComponent& pivot) noexcept;

View File

@ -49,6 +49,16 @@ namespace SHADE
GetNativeComponent()->lookAtCameraOrigin = val;
}
bool CameraArm::EnableCameraCollision::get()
{
return GetNativeComponent()->enableCameraCollision;
}
void CameraArm::EnableCameraCollision::set(bool val)
{
GetNativeComponent()->enableCameraCollision = val;
}
Vector3 CameraArm::TargetOffset::get()
{
return Convert::ToCLI(GetNativeComponent()->GetTargetOffset());

View File

@ -36,6 +36,12 @@ namespace SHADE
void set(bool val);
}
property bool EnableCameraCollision
{
bool get();
void set(bool val);
}
property Vector3 TargetOffset
{
Vector3 get();