Skip to content

Commit

Permalink
Ball working amd camera follows the ball. (#74)
Browse files Browse the repository at this point in the history
* Created a function moving camera that follows the player!

* Could not complete the bumping issue, possible from out of sync frames of flight when camera and player move at the same time.

---------

Co-authored-by: ZacharyH777 <[email protected]>
  • Loading branch information
ZacharyH777 and ZHowe1 authored Dec 8, 2024
1 parent be58eed commit 1dd1104
Show file tree
Hide file tree
Showing 13 changed files with 344 additions and 189 deletions.
5 changes: 3 additions & 2 deletions TestApp/Scenes/Assets/Components/Bodies/BodyContainer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,9 @@ class BodyContainer
JPH::BodyID CreateBody(EActivation p_ActivationType ,JPH::RVec3 p_Position);

protected:
Shape* BaseShape = nullptr;
Shape* ParentShape = nullptr;
Ref<Shape> BaseShape = nullptr;
Ref<Shape> ParentShape = nullptr;

JPH::EMotionType m_MotionType = EMotionType::Static;
JPH::ObjectLayer m_LayerType = Engine3DLayers::Static;

Expand Down
4 changes: 4 additions & 0 deletions TestApp/Scenes/Assets/Components/Camera/CameraFollow.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,12 @@ class CameraFollow : public engine3d::GameComponent
CameraFollow(engine3d::Transform &p_transform);

void OnIntegrate();
void PhysicsUpdate();
void LateUpdate();
private:

float m_CameraViewDistance = 0.0f;
engine3d::Transform* m_PlayerTransform;
engine3d::Transform* m_Transform;
glm::vec3 playerVelocity;
};
11 changes: 10 additions & 1 deletion TestApp/Scenes/Assets/Components/testComp.hpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
#pragma once

#include "Scenes/Assets/Components/Physics/PhysicsBody3D.hpp"
#include <Scenes/Assets/Components/Physics/PhysicsBody3D.hpp>
#include <Core/SceneManagment/Components/GameComponent.hpp>
#include <glm/fwd.hpp>
#include <engine3d/Core/SceneManagment/Components/SPComps/Transform.hpp>

class testComp : public engine3d::GameComponent
{
Expand All @@ -10,7 +12,14 @@
void Update();
void LateUpdate();
void PhysicsUpdate();

void SetCameraTransform(engine3d::Transform* p_Transform)
{
m_CameraTransform = p_Transform;
}
private:
bool t_Secret = false;
BodyContainer * m_rb;
engine3d::Transform* m_CameraTransform;
engine3d::Transform* m_PlayerTransform;
};
104 changes: 72 additions & 32 deletions TestApp/src/Scenes/Assets/Components/Camera/CameraFollow.cpp
Original file line number Diff line number Diff line change
@@ -1,42 +1,54 @@
#include "Core/EngineLogger.hpp"
#include "Core/SceneManagment/Components/SPComps/Camera.hpp"
#include <Core/SceneManagment/Components/SPComps/Camera.hpp>
#include <Core/SceneManagment/Components/SPComps/Transform.hpp>
#include <Physics/Interfaces/BPLayerInterfaceHandler.hpp>
#include <Jolt/Math/Vec3.h>
#include <Scenes/Assets/Components/testComp.hpp>
#include <cstddef>
#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/ext/matrix_transform.hpp>
#include <glm/fwd.hpp>
#include <glm/geometric.hpp>
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <Math/Math.hpp>
#include <Math/Interpolation.hpp>


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

const float RADIUS = 20.0f;
const float HEIGHT = 10.0f;
float distanceSpeed = 0.0f;
bool hasLooked = false;

namespace ToPhysics
{
glm::quat LookAt(const glm::vec3& position, const glm::vec3& target)
{
glm::vec3 up = {0.0f, 1.0f, 0.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
// }
glm::vec3 forward = glm::normalize(target - position);

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

// // Scale by the desired follow radius
// glm::vec3 followOffset = normVel * radius;
float w = sqrt(1.0f + right.x + adjustedUp.y + forward.z) * 0.5f;
float invW4 = 1.0f / (4.0f * w);
float x = (adjustedUp.z - forward.y) * invW4;
float y = (forward.x - right.z) * invW4;
float z = (right.y - adjustedUp.x) * invW4;

// // Calculate the camera's new position behind the target
// return targetPosition + followOffset;
// }
// };
return glm::quat(w, x, y, z);
}
}

CameraFollow::CameraFollow(engine3d::Transform &p_transform)
{
Expand All @@ -45,12 +57,16 @@ CameraFollow::CameraFollow(engine3d::Transform &p_transform)

void CameraFollow::OnIntegrate()
{
engine3d::SyncUpdateManager::GetInstance()->Subscribe
(this, &CameraFollow::PhysicsUpdate);
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>();;
m_Transform = &m_GameObjectRef->GetComponent<engine3d::Transform>();
auto* player = m_PlayerTransform->GetParent();
player->GetComponent<testComp>().SetCameraTransform(m_Transform);
}

void CameraFollow::LateUpdate()
Expand All @@ -59,23 +75,47 @@ 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);
const glm::vec3 player_velocity = VectorConversion::To_Type<glm::vec3>(
l_interface->GetLinearVelocity(rb->GetBody()->m_BodyID));

if(glm::length(player_velocity) > .00001f)
{
playerVelocity = glm::normalize(player_velocity) * -RADIUS;
}
distanceSpeed = glm::length(abs(m_PlayerTransform->m_Position-m_Transform->m_Position))/10.0f;

ConsoleLogWarn("Bounce check: {},{},{}",
m_Transform->m_Position.x,
m_Transform->m_Position.y,
m_Transform->m_Position.z);
glm::vec3 finalRotPosition = glm::vec3{
playerVelocity.x + m_PlayerTransform->m_Position.x,
m_PlayerTransform->m_Position.y + HEIGHT,
playerVelocity.z + m_PlayerTransform->m_Position.z};

m_Transform->SetPos<glm::vec3>({
m_PlayerTransform->m_Position.x,
m_PlayerTransform->m_Position.y + HEIGHT,
m_PlayerTransform->m_Position.z - RADIUS
engine3d::Interpolation::LinearInterpolate(
m_Transform->m_Position,
finalRotPosition,
nullptr,
engine3d::SyncUpdateManager::GetInstance()->m_SyncLocalDeltaTime * distanceSpeed)
});

m_GameObjectRef->GetComponent<engine3d::Camera>().SetViewTarget(
{
m_Transform->m_Position.x,
m_Transform->m_Position.y,
m_Transform->m_Position.z},
glm::quat lookAt = ToPhysics::LookAt(
m_Transform->m_Position,
m_PlayerTransform->m_Position);


m_Transform->SetQuat(
lookAt
);
m_Transform->SetAxisRot(
(m_Transform->GetQuat<JPH::Quat>().GetEulerAngles())
);



}

void CameraFollow::PhysicsUpdate()
{
}
89 changes: 55 additions & 34 deletions TestApp/src/Scenes/Assets/Components/testComp.cpp
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
// #include <engine3d/Core/EngineLogger.hpp>
#include "Core/SceneManagment/Components/GameComponent.hpp"
#include "Core/SceneManagment/Components/SPComps/Transform.hpp"
#include "Math/Math.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 <Core/SceneManagment/SceneObjects/SceneObject.hpp>
#include <glm/fwd.hpp>
#include <glm/glm.hpp>

#define PUSHFORCE 6000000.0f

using namespace engine3d;
void testComp::OnIntegrate()
{;
{
SyncUpdateManager::GetInstance()->Subscribe
(this, &testComp::Update);
SyncUpdateManager::GetInstance()->Subscribe
Expand All @@ -22,55 +25,73 @@ using namespace engine3d;

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

JoltHandler::GetInstance()->getInterface()->SetGravityFactor(m_rb->m_BodyID,2.0f);
}

void testComp::Update()
{
glm::vec3 forward = glm::normalize(
m_GameObjectRef->GetComponent<Transform>().GetPos<glm::vec3>() -
m_CameraTransform->GetPos<glm::vec3>()
);
glm::vec3 globalUp = glm::vec3(0.0f, 1.0f, 0.0f);
glm::vec3 right = glm::normalize(glm::cross(globalUp, forward));

forward.y = 0.0f;
right.y = 0.0f;

auto player_velocity = VectorConversion::To_Type<glm::vec3>(JoltHandler::GetInstance()->
getInterface()->GetLinearVelocity(m_rb->m_BodyID));
float pushForce = 0.0f;
if(abs(player_velocity.x) + abs(player_velocity.z) > 1)
{
pushForce = PUSHFORCE / (abs(player_velocity.x) + abs(player_velocity.z));
}
else {
pushForce = PUSHFORCE;
}
//ConsoleLogWarn("Forward: {},{},{}", player_velocity.x,forward.y,player_velocity.z);

if(InputPoll::IsKeyPressed(KeyCode::F5))
{
JoltHandler::GetInstance()->
getInterface()->AddForce(
m_rb->m_BodyID,
RVec3(0.0f,100000.0f,0.0f),
RVec3(0.0f,pushForce,0.0f),
EActivation::Activate
);
}
if(InputPoll::IsKeyPressed(KeyCode::Up))
if (InputPoll::IsKeyPressed(KeyCode::Up))
{
JoltHandler::GetInstance()->
getInterface()->AddForce(
m_rb->m_BodyID,
RVec3(0.0f,0.0f,100000.0f),
EActivation::Activate
);
JoltHandler::GetInstance()->getInterface()->AddForce(
m_rb->m_BodyID,
VectorConversion::To_Type<RVec3>(forward * pushForce/2.0f),
EActivation::Activate
);
}
if(InputPoll::IsKeyPressed(KeyCode::Down))
if (InputPoll::IsKeyPressed(KeyCode::Down))
{
JoltHandler::GetInstance()->
getInterface()->AddForce(
m_rb->m_BodyID,
RVec3(0.0f,0.0f,-100000.0f),
EActivation::Activate
);
JoltHandler::GetInstance()->getInterface()->AddForce(
m_rb->m_BodyID,
VectorConversion::To_Type<RVec3>(-forward * PUSHFORCE/20.0f),
EActivation::Activate
);
}
if(InputPoll::IsKeyPressed(KeyCode::Left))
if (InputPoll::IsKeyPressed(KeyCode::Left))
{
JoltHandler::GetInstance()->
getInterface()->AddForce(
m_rb->m_BodyID,
RVec3(-100000.0f,0.0f,0.0f),
EActivation::Activate
);
JoltHandler::GetInstance()->getInterface()->AddForce(
m_rb->m_BodyID,
VectorConversion::To_Type<RVec3>(-right * PUSHFORCE/20.0f),
EActivation::Activate
);
}
if(InputPoll::IsKeyPressed(KeyCode::Right))
if (InputPoll::IsKeyPressed(KeyCode::Right))
{
JoltHandler::GetInstance()->
getInterface()->AddForce(
m_rb->m_BodyID,
RVec3(100000.0f,0.0f,0.0f),
EActivation::Activate
);
JoltHandler::GetInstance()->getInterface()->AddForce(
m_rb->m_BodyID,
VectorConversion::To_Type<RVec3>(right * PUSHFORCE/20.0f),
EActivation::Activate
);
}
}

Expand Down
Loading

0 comments on commit 1dd1104

Please sign in to comment.