From d930dbd70ea0454da4867d96fc6f4890e85344b3 Mon Sep 17 00:00:00 2001 From: Alex Frasca Date: Sat, 9 Mar 2024 12:01:32 -0500 Subject: [PATCH 1/3] add attractor --- ext/bave | 2 +- src/spaced/spaced/game/attractor.cpp | 38 ++++++++++++++++++++++++++++ src/spaced/spaced/game/attractor.hpp | 25 ++++++++++++++++++ 3 files changed, 64 insertions(+), 1 deletion(-) create mode 100644 src/spaced/spaced/game/attractor.cpp create mode 100644 src/spaced/spaced/game/attractor.hpp diff --git a/ext/bave b/ext/bave index 80bcd8b..5002fc2 160000 --- a/ext/bave +++ b/ext/bave @@ -1 +1 @@ -Subproject commit 80bcd8b5319d3c8b1750afc1e99e5bff8981bce0 +Subproject commit 5002fc271533a3268592c47c5bc8653ae094dc6b diff --git a/src/spaced/spaced/game/attractor.cpp b/src/spaced/spaced/game/attractor.cpp new file mode 100644 index 0000000..ea321fd --- /dev/null +++ b/src/spaced/spaced/game/attractor.cpp @@ -0,0 +1,38 @@ +#include +#include + +namespace spaced { +void Attractor::tick(bave::Seconds dt) { + dt += m_residue; + if (dt > max_dt) { return; } + for (; dt > 0s; dt -= time_slice) { integrate(); } + m_residue = dt; +} + +void Attractor::integrate() { + auto const to_target = target - position; + + if (glm::length2(to_target) < min_distance * min_distance) { + position = target; + return; + } + + auto const gx = position.x; + auto const gy = position.y; + auto const mx = target.x; + auto const my = target.y; + + auto force_x = mx - gx; + auto force_y = my - gy; + auto const mag = sqrt((force_x * force_x) + (force_y * force_y)); + auto strength = force / mag * mag; + force_x *= strength; + force_y *= strength; + auto const acceleration = glm::vec2{force_x, force_y}; + + auto const dv = acceleration * time_slice.count(); + m_velocity += dv; + m_velocity *= (1.0f - dampen); + position += m_velocity * time_slice.count(); +} +} // namespace spaced diff --git a/src/spaced/spaced/game/attractor.hpp b/src/spaced/spaced/game/attractor.hpp new file mode 100644 index 0000000..bab5518 --- /dev/null +++ b/src/spaced/spaced/game/attractor.hpp @@ -0,0 +1,25 @@ +#pragma once +#include +#include + +namespace spaced { +class Attractor { + public: + glm::vec2 target{}; + glm::vec2 position{}; + + bave::Seconds time_slice{1.0f / 250.0f}; + bave::Seconds max_dt{0.8s}; + float dampen{0.9f}; + float force{0.01f}; + float min_distance{0.1f}; + + void tick(bave::Seconds dt); + + private: + void integrate(); + + glm::vec2 m_velocity{}; + bave::Seconds m_residue{}; +}; +} // namespace spaced From 653c108d45bdbddd72bf9605bb0b1ab33efa34d0 Mon Sep 17 00:00:00 2001 From: Alex Frasca Date: Sat, 9 Mar 2024 12:43:27 -0500 Subject: [PATCH 2/3] give temporary attractor (m_highlight) to player for testing --- src/spaced/spaced/game/attractor.cpp | 16 ++++++++++------ src/spaced/spaced/game/attractor.hpp | 10 ++++++++-- src/spaced/spaced/game/player.cpp | 6 +++++- src/spaced/spaced/game/player.hpp | 2 ++ 4 files changed, 25 insertions(+), 9 deletions(-) diff --git a/src/spaced/spaced/game/attractor.cpp b/src/spaced/spaced/game/attractor.cpp index ea321fd..57ebab3 100644 --- a/src/spaced/spaced/game/attractor.cpp +++ b/src/spaced/spaced/game/attractor.cpp @@ -2,6 +2,15 @@ #include namespace spaced { + +using bave::RoundedQuad; + +Attractor::Attractor(Services const& services) { + auto rounded_quad = RoundedQuad{}; + rounded_quad.size = glm::vec2{20.0f}; + rounded_quad.corner_radius = 5.0f; + shape.set_shape(rounded_quad); +} void Attractor::tick(bave::Seconds dt) { dt += m_residue; if (dt > max_dt) { return; } @@ -10,12 +19,6 @@ void Attractor::tick(bave::Seconds dt) { } void Attractor::integrate() { - auto const to_target = target - position; - - if (glm::length2(to_target) < min_distance * min_distance) { - position = target; - return; - } auto const gx = position.x; auto const gy = position.y; @@ -34,5 +37,6 @@ void Attractor::integrate() { m_velocity += dv; m_velocity *= (1.0f - dampen); position += m_velocity * time_slice.count(); + shape.transform.position = position; } } // namespace spaced diff --git a/src/spaced/spaced/game/attractor.hpp b/src/spaced/spaced/game/attractor.hpp index bab5518..56fe011 100644 --- a/src/spaced/spaced/game/attractor.hpp +++ b/src/spaced/spaced/game/attractor.hpp @@ -1,20 +1,26 @@ #pragma once #include #include +#include +#include namespace spaced { class Attractor { public: + Attractor() = default; + Attractor(Services const& services); + glm::vec2 target{}; glm::vec2 position{}; bave::Seconds time_slice{1.0f / 250.0f}; bave::Seconds max_dt{0.8s}; - float dampen{0.9f}; - float force{0.01f}; + float dampen{0.01f}; + float force{4.1f}; float min_distance{0.1f}; void tick(bave::Seconds dt); + bave::RoundedQuadShape shape{}; private: void integrate(); diff --git a/src/spaced/spaced/game/player.cpp b/src/spaced/spaced/game/player.cpp index a13d94d..908e4b8 100644 --- a/src/spaced/spaced/game/player.cpp +++ b/src/spaced/spaced/game/player.cpp @@ -23,6 +23,7 @@ Player::Player(Services const& services, std::unique_ptr controller rounded_quad.size = glm::vec2{100.0f}; rounded_quad.corner_radius = 20.0f; ship.set_shape(rounded_quad); + m_highlight = Attractor(services); debug_switch_weapon(); } @@ -37,6 +38,9 @@ void Player::tick(std::span const> targets, Seconds const auto const y_position = m_controller->tick(dt); set_y(y_position); + m_highlight.target = ship.transform.position; + m_highlight.tick(dt); + auto const muzzle_position = ship.transform.position + 0.5f * glm::vec2{ship.get_shape().size.x, 0.0f}; if (m_controller->is_firing() && m_debug.shots_remaining > 0) { if (auto round = m_weapon->fire(muzzle_position)) { @@ -56,7 +60,7 @@ void Player::tick(std::span const> targets, Seconds const void Player::draw(Shader& shader) const { ship.draw(shader); - + m_highlight.shape.draw(shader); for (auto const& round : m_weapon_rounds) { round->draw(shader); } } diff --git a/src/spaced/spaced/game/player.hpp b/src/spaced/spaced/game/player.hpp index d16a8bf..8f448ef 100644 --- a/src/spaced/spaced/game/player.hpp +++ b/src/spaced/spaced/game/player.hpp @@ -4,6 +4,7 @@ #include #include #include +#include namespace spaced { class Player : public bave::IDrawable { @@ -39,6 +40,7 @@ class Player : public bave::IDrawable { std::unique_ptr m_controller; std::unique_ptr m_weapon{}; std::vector> m_weapon_rounds{}; + Attractor m_highlight{}; struct { int shots_remaining{}; From f8d18ebc3592f5494362df34b5db4018f6023b30 Mon Sep 17 00:00:00 2001 From: Alex Frasca Date: Sat, 9 Mar 2024 12:47:27 -0500 Subject: [PATCH 3/3] fix formatting --- src/spaced/spaced/game/attractor.hpp | 2 +- src/spaced/spaced/game/player.hpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/spaced/spaced/game/attractor.hpp b/src/spaced/spaced/game/attractor.hpp index 56fe011..08e6462 100644 --- a/src/spaced/spaced/game/attractor.hpp +++ b/src/spaced/spaced/game/attractor.hpp @@ -1,7 +1,7 @@ #pragma once #include -#include #include +#include #include namespace spaced { diff --git a/src/spaced/spaced/game/player.hpp b/src/spaced/spaced/game/player.hpp index 8f448ef..42415ef 100644 --- a/src/spaced/spaced/game/player.hpp +++ b/src/spaced/spaced/game/player.hpp @@ -1,10 +1,10 @@ #pragma once #include #include +#include #include #include #include -#include namespace spaced { class Player : public bave::IDrawable {