Commit 10133b11 authored by Tim Scheiber's avatar Tim Scheiber
Browse files

Added camera behaviour with spring behaviour

parent d0bd4460
Pipeline #3230 failed with stage
in 6 minutes and 39 seconds
......@@ -5,5 +5,16 @@
"fov": 50.0,
"near": 0.05,
"far": 80.0
},
"Viewtarget": {
"offset": {
"x": 0.0,
"y": 10.0,
"z": 1.0
}
},
"Spring": {
"spring_constant": 20.0,
"mass": 1.0
}
}
#include "camera_system.hpp"
#include <mirrage/ecs/components/transform_comp.hpp>
namespace phase_shifter::gameplay {
Camera_system::Camera_system(mirrage::ecs::Entity_manager& entity_manager)
: _entity_manager(entity_manager)
{
_entity_manager.register_component_type<Viewtarget_comp>();
_entity_manager.register_component_type<Spring_comp>();
}
void Camera_system::update(mirrage::util::Time dt)
{
for (auto&& [camera, viewtarget, spring] : _entity_manager.list<mirrage::ecs::Entity_facet, Viewtarget_comp, Spring_comp>()) {
auto& target_facet = _entity_manager.get(viewtarget.target);
if(target_facet.is_some()) {
auto& target_transform = target_facet.get_or_throw().get<mirrage::ecs::components::Transform_comp>();
if(target_transform.is_some()) {
auto& target_position = target_transform.get_or_throw().position;
glm::vec3 offset = viewtarget.offset;
glm::vec3 dest_pos = {target_position.x + offset.x,
target_position.y + offset.y,
target_position.z + offset.z};
float damping_constant = 2 * sqrtf(spring.spring_constant);
glm::vec3 damping_force = -damping_constant * spring.velocity;
camera.process([&](mirrage::ecs::components::Transform_comp& transform) {
glm::vec3 spring_force = spring.spring_constant * (dest_pos - transform.position);
glm::vec3 acceleration = (spring_force + damping_force) / spring.mass;
spring.velocity += dt.value() * acceleration;
transform.position += dt.value() * spring.velocity;
transform.direction(-offset);
});
}
}
}
}
}
\ No newline at end of file
#pragma once
#include <mirrage/utils/units.hpp>
#include <mirrage/ecs/entity_manager.hpp>
#include "viewtarget_comp.hpp"
#include "spring_comp.hpp"
namespace phase_shifter::gameplay {
class Camera_system {
public:
Camera_system(mirrage::ecs::Entity_manager& entity_manager);
void update(mirrage::util::Time dt);
private:
mirrage::ecs::Entity_manager& _entity_manager;
};
}
\ No newline at end of file
#pragma once
#include <mirrage/ecs/ecs.hpp>
#include <mirrage/utils/sf2_glm.hpp>
namespace phase_shifter::gameplay {
struct Spring_comp : public mirrage::ecs::Component<Spring_comp> {
static constexpr const char* name() { return "Spring"; }
using Component::Component;
glm::vec3 velocity = {0.f, 0.f, 0.f};
float spring_constant = 1.f;
float mass = 1.f;
};
sf2_structDef(Spring_comp, spring_constant);
}
\ No newline at end of file
#pragma once
#include <mirrage/ecs/ecs.hpp>
#include <mirrage/utils/sf2_glm.hpp>
namespace phase_shifter::gameplay {
struct Viewtarget_comp : public mirrage::ecs::Component<Viewtarget_comp> {
static constexpr const char* name() { return "Viewtarget"; }
using Component::Component;
mirrage::ecs::Entity_handle target;
glm::vec3 offset = {0.f, 10.f, 1.f};
};
sf2_structDef(Viewtarget_comp, offset);
}
\ No newline at end of file
......@@ -4,6 +4,7 @@
#include "game_engine.hpp"
#include "gameplay/beat_system.hpp"
#include "gameplay/movement_system.hpp"
#include "gameplay/camera_system.hpp"
#include "helper/attachment_system.hpp"
#include "input/input_system.hpp"
#include "ui/hud_system.hpp"
......@@ -28,6 +29,7 @@ namespace phase_shifter {
, _model_loading(std::make_unique<renderer::Loading_system>(_entities, engine.assets()))
, _beat_system(std::make_unique<gameplay::Beat_system>())
, _movement_system(std::make_unique<gameplay::Movement_system>(_entities, *_beat_system))
, _camera_system(std::make_unique<gameplay::Camera_system>(_entities))
, _input_system(std::make_unique<input::Input_system>(engine.bus(), _entities))
, _attachment_system(std::make_unique<helper::Attachment_system>(_entities))
, _hud_system(std::make_unique<ui::Hud_system>(engine.gui(), _entities))
......@@ -79,17 +81,6 @@ namespace phase_shifter {
auto cam_azimuth = 0.0f;
auto cam_dir = glm::quat(glm::vec3(
(cam_elevation - 2.f) * glm::pi<float>() / 2.f, glm::pi<float>() * cam_azimuth, 0.f));
entities()
.entity_builder("camera")
.rotation(cam_dir)
.position(glm::rotate(cam_dir, glm::vec3(0, 0, -1)) * 8.f)
.post_create([=](ecs::Entity_facet entity) {
entity.process<Transform_comp>([&](auto& transform) {
transform.position = transform.direction() * -10.f;
transform.look_at({0, 0, 0});
});
})
.create();
auto sun_elevation = 0.05f * glm::pi<float>();
auto sun_azimuth = 0.7f;
......@@ -101,7 +92,20 @@ namespace phase_shifter {
.position(glm::rotate(sun_dir, glm::vec3(0, 0, 1)) * 40.f)
.create();
entities().entity_builder("player").position({0, 1, 0}).create();
mirrage::ecs::Entity_facet player = entities().entity_builder("player").position({0, 1, 0}).create();
auto playerhandle = player.handle();
mirrage::ecs::Entity_facet camera = entities()
.entity_builder("camera")
.rotation(cam_dir)
.position(glm::rotate(cam_dir, glm::vec3(0, 0, -1)) * 8.f)
.post_create([=](ecs::Entity_facet entity) {
entity.process<Transform_comp>([&](auto& transform) {
transform.position = transform.direction() * -10.f;
transform.look_at({0, 0, 0});
});
entity.process([=](gameplay::Viewtarget_comp& viewtarget) { viewtarget.target = playerhandle; });
})
.create();
entities()
.entity_builder("cube")
......@@ -127,6 +131,7 @@ namespace phase_shifter {
_beat_system->update(dt);
_input_system->update(dt);
_movement_system->update(dt);
_camera_system->update(dt);
_attachment_system->update(dt);
......
......@@ -22,6 +22,7 @@ namespace phase_shifter {
namespace gameplay {
class Beat_system;
class Movement_system;
class Camera_system;
} // namespace gameplay
namespace helper {
class Attachment_system;
......@@ -60,6 +61,7 @@ namespace phase_shifter {
std::unique_ptr<input::Input_system> _input_system;
std::unique_ptr<helper::Attachment_system> _attachment_system;
std::unique_ptr<ui::Hud_system> _hud_system;
std::unique_ptr<gameplay::Camera_system> _camera_system;
// TODO: add systems here
mirrage::util::Console_command_container _commands;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment