8000 camera character position blend by BoomingTechDev · Pull Request #143 · BoomingTech/Piccolo · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

camera character position blend #143

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ cpm_modules/
CPM_modules/
CPM_*.cmake
.DS_Store
*.code-workspace

# Visual Studio
CMakeSettings.json
Expand Down
4 changes: 3 additions & 1 deletion engine/asset/level/1-1.level.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
{
"gravity": 15,
"character_index": 0,
"objects": [
{
{
"name": "Player",
"transform": {
"position": {
Expand Down
19 changes: 19 additions & 0 deletions engine/source/runtime/engine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,8 @@ namespace Pilot
}

logicalTick(delta_time);
fps(delta_time);

if (!rendererTick())
return;
}
Expand All @@ -116,6 +118,23 @@ namespace Pilot

bool PilotEngine::rendererTick() { return m_renderer->tick(); }

const float PilotEngine::k_fps_alpha = 1.f / 100;
void PilotEngine::fps(float delta_time)
{
m_frame_count++;

if (m_frame_count == 1)
{
m_average_duration = delta_time;
}
else
{
m_average_duration = m_average_duration * (1 - k_fps_alpha) + delta_time * k_fps_alpha;
}

m_fps = static_cast<int>(1.f / m_average_duration);
}

std::shared_ptr<SurfaceIO> PilotEngine::getSurfaceIO() { return m_renderer->getPSurface()->getSurfaceIO(); }

void ThreeFrameBuffers::initialize()
Expand Down
10 changes: 10 additions & 0 deletions engine/source/runtime/engine.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ namespace Pilot
{
friend class PublicSingleton<PilotEngine>;

static const float k_fps_alpha;

protected:
PilotEngine();

Expand All @@ -60,9 +62,15 @@ namespace Pilot
ThreeFrameBuffers m_tri_frame_buffer;
std::shared_ptr<PilotRenderer> m_renderer;

float m_average_duration {0.f};
int m_frame_count {0};
int m_fps {0};

void logicalTick(float delta_time);
bool rendererTick();

void fps(float delta_time);

public:
PilotEngine(const PilotEngine&) = delete;
PilotEngine& operator=(const P EDBE ilotEngine&) = delete;
Expand All @@ -76,6 +84,8 @@ namespace Pilot
bool isQuit() const { return m_is_quit; }
void run();

int getFPS() const { return m_fps; }

std::shared_ptr<SurfaceIO> getSurfaceIO();
std::shared_ptr<PilotRenderer> getRender() const { return m_renderer; }
};
Expand Down
66 changes: 66 additions & 0 deletions engine/source/runtime/function/character/character.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
#include "runtime/function/character/character.h"

#include "runtime/engine.h"
#include "runtime/function/framework/component/motor/motor_component.h"
#include "runtime/function/framework/component/transform/transform_component.h"
#include "runtime/function/input/input_system.h"

namespace Pilot
{
Character::Character(GObject* character_object) { setObject(character_object); }

void Character::setObject(GObject* gobject)
{
m_character_object = gobject;
if (m_character_object)
{
const TransformComponent* transform_component =
m_character_object->tryGetComponentConst(TransformComponent);
const Transform& transform = transform_component->getTransformConst();
m_position = transform.m_position;
m_rotation = transform.m_rotation;
}
else
{
m_position = Vector3::ZERO;
m_rotation = Quaternion::IDENTITY;
}
}

void Character::tick()
{
TransformComponent* transform_component = m_character_object->tryGetComponent(TransformComponent);

if (m_rotation_dirty)
{
transform_component->setRotation(m_rotation_buffer);
m_rotation_dirty = false;
}

unsigned int command = InputSystem::getInstance().getGameCommand();
if (command >= (unsigned int)GameCommand::invalid)
return;

if (command > 0)
{
m_rotation_buffer = m_rotation;
transform_component->setRotation(m_rotation_buffer);
m_rotation_dirty = true;
}

const MotorComponent* motor_component = m_character_object->tryGetComponentConst(MotorComponent);
const Vector3& new_position = motor_component->getTargetPosition();

float camera_blend_time = k_camera_blend_time;

const int fps = PilotEngine::getInstance().getFPS();
if (fps == 0)
return;

float frame_length = 1.f / static_cast<float>(fps) * motor_component->getSpeedRatio();
m_position =
(m_position * (k_camera_blend_time - frame_length) + new_position * frame_length) / k_camera_blend_time;
m_position =
(m_position * (k_camera_blend_time - frame_length) + new_position * frame_length) / k_camera_blend_time;
}
} // namespace Pilot
38 changes: 38 additions & 0 deletions engine/source/runtime/function/character/character.h
F438
Original file line number Diff line numberDiff line change
@@ -0,0 +1,38 @@
#pragma once

#include "runtime/core/math/transform.h"

#include "runtime/function/framework/object/object.h"

#include <vector>

namespace Pilot
{
class Character
{
inline static const float k_camera_blend_time {0.3f};

public:
Character(GObject* character_object);

GObject* getObject() const { return m_character_object; }
void setObject(GObject* gobject);

void setPosition(const Vector3& position) { m_position = position; }
void setRotation(const Quaternion& rotation) { m_rotation = rotation; }

const Vector3& getPosition() const { return m_position; }
const Quaternion& getRotation() const { return m_rotation; }

void tick();

private:
Vector3 m_position;
Quaternion m_rotation;
GObject* m_character_object {nullptr};

// hack for setting rotation frame buffer
Quaternion m_rotation_buffer;
bool m_rotation_dirty;
};
} // namespace Pilot
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@
#include "runtime/core/math/math_headers.h"

#include "runtime/engine.h"
#include "runtime/function/character/character.h"
#include "runtime/function/framework/component/transform/transform_component.h"
#include "runtime/function/framework/level/level.h"
#include "runtime/function/framework/object/object.h"
#include "runtime/function/framework/world/world_manager.h"
#include "runtime/function/input/input_system.h"
#include "runtime/function/render/include/render/glm_wrapper.h"
#include "runtime/function/scene/scene_manager.h"
Expand Down Expand Up @@ -50,6 +53,14 @@ namespace Pilot
if (g_is_editor_mode)
return;

Level* current_level = WorldManager::getInstance().getCurrentActiveLevel();
Character* current_character = current_level->getCurrentActiveCharacter();
if (current_character == nullptr)
return;

if (current_character->getObject() != m_parent_object)
return;

switch (m_camera_mode)
{
case CameraMode::first_person:
Expand All @@ -67,14 +78,16 @@ namespace Pilot

void CameraComponent::tickFirstPersonCamera(float delta_time)
{
Level* current_level = WorldManager::getInstance().getCurrentActiveLevel();
Character* current_character = current_level->getCurrentActiveCharacter();

Quaternion q_yaw, q_pitch;

q_yaw.fromAngleAxis(InputSystem::getInstance().m_cursor_delta_yaw, Vector3::UNIT_Z);
q_pitch.fromAngleAxis(InputSystem::getInstance().m_cursor_delta_pitch, m_left);

TransformComponent* parent_transform = m_parent_object->tryGetComponent(TransformComponent);
const float offset = static_cast<FirstPersonCameraParameter*>(m_camera_param.m_parameter)->m_vertical_offset;
Vector3 eye_pos = parent_transform->getPosition() + offset * Vector3::UNIT_Z;
Vector3 eye_pos = current_character->getPosition() + offset * Vector3::UNIT_Z;

m_foward = q_yaw * q_pitch * m_foward;
m_left = q_yaw * q_pitch * m_left;
Expand All @@ -87,32 +100,34 @@ namespace Pilot
Vector3 object_left = Vector3::UNIT_Z.crossProduct(object_facing);
Quaternion object_rotation;
object_rotation.fromAxes(object_left, -object_facing, Vector3::UNIT_Z);
parent_transform->setRotation(object_rotation);
current_character->setRotation(object_rotation);
}

void CameraComponent::tickThirdPersonCamera(float delta_time)
{
Level* current_level = WorldManager::getInstance().getCurrentActiveLevel();
Character* current_character = current_level->getCurrentActiveCharacter();

ThirdPersonCameraParameter* param = static_cast<ThirdPersonCameraParameter*>(m_camera_param.m_parameter);

Quaternion q_yaw, q_pitch;

q_yaw.fromAngleAxis(InputSystem::getInstance().m_cursor_delta_yaw, Vector3::UNIT_Z);
q_pitch.fromAngleAxis(InputSystem::getInstance().m_cursor_delta_pitch, m_left);
q_pitch.fromAngleAxis(InputSystem::getInstance().m_cursor_delta_pitch, Vector3::UNIT_X);

param->m_cursor_pitch = param->m_cursor_pitch * q_pitch;
param->m_cursor_pitch = q_pitch * param->m_cursor_pitch;

TransformComponent* parent_transform = m_parent_object->tryGetComponent(TransformComponent);
const float vertical_offset = param->m_vertical_offset;
const float horizontal_offset = param->m_horizontal_offset;
Vector3 offset = Vector3(0, horizontal_offset, vertical_offset);
const float vertical_offset = param->m_vertical_offset;
const float horizontal_offset = param->m_horizontal_offset;
Vector3 offset = Vector3(0, horizontal_offset, vertical_offset);

parent_transform->setRotation(q_yaw * parent_transform->getRotation());

Vector3 center_pos = parent_transform->getPosition() + Vector3::UNIT_Z * vertical_offset;
Vector3 center_pos = current_character->getPosition() + Vector3::UNIT_Z * vertical_offset;
Vector3 camera_pos =
parent_transform->getRotation() * param->m_cursor_pitch * offset + parent_transform->getPosition();
current_character->getRotation() * param->m_cursor_pitch * offset + current_character->getPosition();
Vector3 camera_forward = center_pos - camera_pos;
Vector3 camera_up = parent_transform->getRotation() * param->m_cursor_pitch * Vector3::UNIT_Z;
Vector3 camera_up = current_character->getRotation() * param->m_cursor_pitch * Vector3::UNIT_Z;

current_character->setRotation(q_yaw * current_character->getRotation());

Matrix4x4 desired_mat = Math::makeLookAtMatrix(camera_pos, camera_pos + camera_forward, camera_up);
SceneManager::getInstance().setMainViewMatrix(desired_mat, PCurrentCameraType::Motor);
Expand Down
Loading
0