Merge pull request #156 from SHADE-DP/SP3-141-Camera-System
Sp3 141 camera system Added C# interface for Camera and Camera Arm Added Time.DeltaTimeF and Input.GetMouseVelocity to C# interface Added ThirdPersonCamera and Camera Control scripts Added a GetForward function to Camera C# interface. This gets the forward of the last calculated view Matrix.
This commit is contained in:
commit
1f7abc7a96
|
@ -0,0 +1,127 @@
|
|||
#include "SHpch.h"
|
||||
|
||||
#include "Camera.hxx"
|
||||
#include "ECS_Base/Managers/SHSystemManager.h"
|
||||
#include "Camera/SHCameraSystem.h"
|
||||
|
||||
|
||||
namespace SHADE
|
||||
{
|
||||
Camera::Camera(Entity entity)
|
||||
:Component(entity)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
|
||||
float Camera::Pitch::get()
|
||||
{
|
||||
return (GetNativeComponent()->GetPitch());
|
||||
}
|
||||
|
||||
void Camera::Pitch::set(float val)
|
||||
{
|
||||
GetNativeComponent()->SetPitch(val);
|
||||
}
|
||||
float Camera::Yaw::get()
|
||||
{
|
||||
return (GetNativeComponent()->GetYaw());
|
||||
}
|
||||
|
||||
void Camera::Yaw::set(float val)
|
||||
{
|
||||
GetNativeComponent()->SetYaw(val);
|
||||
}
|
||||
float Camera::Roll::get()
|
||||
{
|
||||
return (GetNativeComponent()->GetRoll());
|
||||
}
|
||||
|
||||
void Camera::Roll::set(float val)
|
||||
{
|
||||
GetNativeComponent()->SetRoll(val);
|
||||
}
|
||||
float Camera::Width::get()
|
||||
{
|
||||
return (GetNativeComponent()->GetWidth());
|
||||
}
|
||||
|
||||
void Camera::Width::set(float val)
|
||||
{
|
||||
GetNativeComponent()->SetWidth(val);
|
||||
}
|
||||
float Camera::Height::get()
|
||||
{
|
||||
return (GetNativeComponent()->GetHeight());
|
||||
}
|
||||
|
||||
void Camera::Height::set(float val)
|
||||
{
|
||||
GetNativeComponent()->SetHeight(val);
|
||||
}
|
||||
float Camera::Near::get()
|
||||
{
|
||||
return (GetNativeComponent()->GetNear());
|
||||
}
|
||||
|
||||
void Camera::Near::set(float val)
|
||||
{
|
||||
GetNativeComponent()->SetNear(val);
|
||||
}
|
||||
float Camera::Far::get()
|
||||
{
|
||||
return (GetNativeComponent()->GetFar());
|
||||
}
|
||||
|
||||
void Camera::Far::set(float val)
|
||||
{
|
||||
GetNativeComponent()->SetFar(val);
|
||||
}
|
||||
float Camera::FOV::get()
|
||||
{
|
||||
return (GetNativeComponent()->GetFOV());
|
||||
}
|
||||
|
||||
void Camera::FOV::set(float val)
|
||||
{
|
||||
GetNativeComponent()->SetFOV(val);
|
||||
}
|
||||
|
||||
Vector3 Camera::Position::get()
|
||||
{
|
||||
return Convert::ToCLI(GetNativeComponent()->GetPosition());
|
||||
}
|
||||
|
||||
void Camera::Position::set(Vector3 val)
|
||||
{
|
||||
GetNativeComponent()->SetPosition(Convert::ToNative(val));
|
||||
}
|
||||
|
||||
void Camera::SetMainCamera(size_t directorIndex)
|
||||
{
|
||||
auto system = SHSystemManager::GetSystem<SHCameraSystem>();
|
||||
system->SetMainCamera(GetNativeComponent()->GetEID(), directorIndex);
|
||||
}
|
||||
|
||||
void Camera::SetMainCamera()
|
||||
{
|
||||
SetMainCamera(0);
|
||||
}
|
||||
|
||||
void Camera::LookAt(Vector3 targetPosition)
|
||||
{
|
||||
auto system = SHSystemManager::GetSystem<SHCameraSystem>();
|
||||
system->CameraLookAt(*GetNativeComponent(), Convert::ToNative(targetPosition));
|
||||
}
|
||||
|
||||
Vector3 Camera::GetForward()
|
||||
{
|
||||
auto system = SHSystemManager::GetSystem<SHCameraSystem>();
|
||||
SHVec3 forward, up, right;
|
||||
system->GetCameraAxis(*GetNativeComponent(), forward, right, up);
|
||||
return Convert::ToCLI(forward);
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
|
@ -0,0 +1,70 @@
|
|||
#pragma once
|
||||
|
||||
// Project Includes
|
||||
#include "Components/Component.hxx"
|
||||
#include "Math/Vector3.hxx"
|
||||
#include "Math/Quaternion.hxx"
|
||||
// External Dependencies
|
||||
#include "Camera/SHCameraComponent.h"
|
||||
|
||||
namespace SHADE
|
||||
{
|
||||
public ref class Camera : public Component<SHCameraComponent>
|
||||
{
|
||||
internal:
|
||||
Camera(Entity entity);
|
||||
|
||||
public:
|
||||
property float Pitch
|
||||
{
|
||||
float get();
|
||||
void set(float val);
|
||||
}
|
||||
property float Yaw
|
||||
{
|
||||
float get();
|
||||
void set(float val);
|
||||
}
|
||||
property float Roll
|
||||
{
|
||||
float get();
|
||||
void set(float val);
|
||||
}
|
||||
property float Width
|
||||
{
|
||||
float get();
|
||||
void set(float val);
|
||||
}
|
||||
property float Height
|
||||
{
|
||||
float get();
|
||||
void set(float val);
|
||||
}
|
||||
property float Near
|
||||
{
|
||||
float get();
|
||||
void set(float val);
|
||||
}
|
||||
property float Far
|
||||
{
|
||||
float get();
|
||||
void set(float val);
|
||||
}
|
||||
property float FOV
|
||||
{
|
||||
float get();
|
||||
void set(float val);
|
||||
}
|
||||
property Vector3 Position
|
||||
{
|
||||
Vector3 get();
|
||||
void set(Vector3 val);
|
||||
}
|
||||
|
||||
|
||||
void SetMainCamera(size_t directorIndex);
|
||||
void SetMainCamera();
|
||||
void LookAt(Vector3 targetPosition);
|
||||
Vector3 GetForward();
|
||||
};
|
||||
}
|
|
@ -0,0 +1,51 @@
|
|||
#include "SHpch.h"
|
||||
#include "CameraArm.hxx"
|
||||
|
||||
|
||||
namespace SHADE
|
||||
{
|
||||
CameraArm::CameraArm(Entity entity)
|
||||
:Component(entity)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
float CameraArm::Pitch::get()
|
||||
{
|
||||
return (GetNativeComponent()->GetPitch());
|
||||
}
|
||||
|
||||
void CameraArm::Pitch::set(float val)
|
||||
{
|
||||
GetNativeComponent()->SetPitch(val);
|
||||
}
|
||||
float CameraArm::Yaw::get()
|
||||
{
|
||||
return (GetNativeComponent()->GetYaw());
|
||||
}
|
||||
|
||||
void CameraArm::Yaw::set(float val)
|
||||
{
|
||||
GetNativeComponent()->SetYaw(val);
|
||||
}
|
||||
|
||||
float CameraArm::ArmLength::get()
|
||||
{
|
||||
return (GetNativeComponent()->GetArmLength());
|
||||
}
|
||||
|
||||
void CameraArm::ArmLength::set(float val)
|
||||
{
|
||||
GetNativeComponent()->SetArmLength(val);
|
||||
}
|
||||
|
||||
bool CameraArm::LookAtCameraOrigin::get()
|
||||
{
|
||||
return GetNativeComponent()->lookAtCameraOrigin;
|
||||
}
|
||||
|
||||
void CameraArm::LookAtCameraOrigin::set(bool val)
|
||||
{
|
||||
GetNativeComponent()->lookAtCameraOrigin = val;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,40 @@
|
|||
#pragma once
|
||||
|
||||
// Project Includes
|
||||
#include "Components/Component.hxx"
|
||||
#include "Math/Vector3.hxx"
|
||||
|
||||
// External Dependencies
|
||||
#include "Camera/SHCameraArmComponent.h"
|
||||
|
||||
|
||||
namespace SHADE
|
||||
{
|
||||
public ref class CameraArm : public Component<SHCameraArmComponent>
|
||||
{
|
||||
internal:
|
||||
CameraArm(Entity entity);
|
||||
public:
|
||||
property float Pitch
|
||||
{
|
||||
float get();
|
||||
void set(float val);
|
||||
}
|
||||
property float Yaw
|
||||
{
|
||||
float get();
|
||||
void set(float val);
|
||||
}
|
||||
property float ArmLength
|
||||
{
|
||||
float get();
|
||||
void set(float val);
|
||||
}
|
||||
property bool LookAtCameraOrigin
|
||||
{
|
||||
bool get();
|
||||
void set(bool val);
|
||||
}
|
||||
|
||||
};
|
||||
}
|
|
@ -33,6 +33,8 @@ of DigiPen Institute of Technology is prohibited.
|
|||
#include "Components/Transform.hxx"
|
||||
#include "Components\RigidBody.hxx"
|
||||
#include "Components\Collider.hxx"
|
||||
#include "Components/Camera.hxx"
|
||||
#include "Components/CameraArm.hxx"
|
||||
|
||||
namespace SHADE
|
||||
{
|
||||
|
@ -248,6 +250,8 @@ namespace SHADE
|
|||
componentMap.Add(createComponentSet<SHTransformComponent, Transform>());
|
||||
componentMap.Add(createComponentSet<SHColliderComponent, Collider>());
|
||||
componentMap.Add(createComponentSet<SHRigidBodyComponent, RigidBody>());
|
||||
componentMap.Add(createComponentSet<SHCameraComponent, Camera>());
|
||||
componentMap.Add(createComponentSet<SHCameraArmComponent, CameraArm>());
|
||||
}
|
||||
|
||||
/*---------------------------------------------------------------------------------*/
|
||||
|
|
|
@ -29,6 +29,12 @@ namespace SHADE
|
|||
{
|
||||
return SHFrameRateController::GetRawDeltaTime();
|
||||
}
|
||||
|
||||
|
||||
float Time::DeltaTimeF::get()
|
||||
{
|
||||
return static_cast<float>(SHFrameRateController::GetRawDeltaTime());
|
||||
}
|
||||
double Time::FixedDeltaTime::get()
|
||||
{
|
||||
return SHPhysicsSystemInterface::GetFixedDT();
|
||||
|
|
|
@ -32,6 +32,16 @@ namespace SHADE
|
|||
{
|
||||
double get();
|
||||
}
|
||||
/*-----------------------------------------------------------------------------*/
|
||||
/* Properties */
|
||||
/*-----------------------------------------------------------------------------*/
|
||||
/// <summary>
|
||||
/// Time taken to process the previous frame.
|
||||
/// </summary>
|
||||
static property float DeltaTimeF
|
||||
{
|
||||
float get();
|
||||
}
|
||||
/// <summary>
|
||||
/// Time taken for Physics simulations. You should use this for operations
|
||||
/// within Script.FixedUpdate()
|
||||
|
|
|
@ -99,4 +99,11 @@ namespace SHADE
|
|||
return SHInputManager::GetKeyReleasedTime(static_cast<SHInputManager::SH_KEYCODE>(key));
|
||||
}
|
||||
|
||||
Vector2 Input::GetMouseVelocity()
|
||||
{
|
||||
double velX, velY;
|
||||
SHInputManager::GetMouseVelocity(&velX, &velY);
|
||||
|
||||
return Convert::ToCLI(SHVec2{ (float)velX,(float)velY });
|
||||
}
|
||||
}
|
|
@ -471,5 +471,7 @@ namespace SHADE
|
|||
/// <param name="key">The key to check.</param>
|
||||
/// <returns>Time in seconds that the key was held.</returns>
|
||||
static double GetMouseReleasedTime(MouseCode mouseButton);
|
||||
|
||||
static Vector2 GetMouseVelocity();
|
||||
};
|
||||
}
|
|
@ -0,0 +1,22 @@
|
|||
using System;
|
||||
using SHADE;
|
||||
|
||||
namespace SHADE_Scripting
|
||||
{
|
||||
public class CameraControl :Script
|
||||
{
|
||||
public float turnSpeed = 0.5f;
|
||||
|
||||
public CameraControl(GameObject go) : base(go) { }
|
||||
protected override void update()
|
||||
{
|
||||
//Camera
|
||||
Camera cam = GetComponent<Camera>();
|
||||
Vector2 mouseVel = Input.GetMouseVelocity();
|
||||
|
||||
cam.Pitch -= mouseVel.y * turnSpeed * (float)Time.DeltaTime;
|
||||
cam.Yaw += mouseVel.x * turnSpeed * (float)Time.DeltaTime;
|
||||
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,55 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using SHADE;
|
||||
|
||||
|
||||
namespace SHADE_Scripting
|
||||
{
|
||||
public class ThirdPersonCamera: Script
|
||||
{
|
||||
|
||||
public float armLength = 4.0f;
|
||||
public float turnSpeedPitch = 0.3f;
|
||||
public float turnSpeedYaw = 0.5f;
|
||||
public float pitchClamp = 45.0f;
|
||||
public ThirdPersonCamera(GameObject go) : base(go) { }
|
||||
|
||||
protected override void awake()
|
||||
{
|
||||
if(!GetComponent<Camera>())
|
||||
{
|
||||
AddComponent<Camera>();
|
||||
}
|
||||
if (!GetComponent<CameraArm>())
|
||||
{
|
||||
AddComponent<CameraArm>();
|
||||
}
|
||||
GetComponent<CameraArm>().ArmLength = armLength;
|
||||
}
|
||||
|
||||
protected override void update()
|
||||
{
|
||||
CameraArm arm = GetComponent<CameraArm>();
|
||||
if(arm)
|
||||
{
|
||||
Vector2 vel = Input.GetMouseVelocity();
|
||||
arm.Pitch -= vel.y * turnSpeedPitch * Time.DeltaTimeF;
|
||||
arm.Yaw += vel.x * turnSpeedYaw * Time.DeltaTimeF;
|
||||
|
||||
if(arm.Pitch > pitchClamp)
|
||||
{
|
||||
arm.Pitch = pitchClamp;
|
||||
}
|
||||
else if(arm.Pitch < -pitchClamp)
|
||||
{
|
||||
arm.Pitch = -pitchClamp;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue