-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
68 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
#include <glm/gtx/norm.hpp> | ||
#include <spaced/game/Gravitator.hpp> | ||
|
||
namespace spaced { | ||
void Gravitator::tick(bave::Seconds dt) { | ||
dt += m_residue; | ||
if (dt > max_dt) { return; } | ||
for (; dt > 0s; dt -= time_slice) { integrate(); } | ||
m_residue = dt; | ||
} | ||
|
||
void Gravitator::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 mag = sqrt((force_x * force_x) + (force_y * force_y)); | ||
mag = std::max(tiny_value, mag); | ||
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
#pragma once | ||
#include <bave/core/time.hpp> | ||
#include <glm/vec2.hpp> | ||
|
||
namespace spaced { | ||
float const tiny_value{0.001f}; | ||
class Gravitator { | ||
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters