Sparkle 0.0.1
Loading...
Searching...
No Matches
spk_force_2d.hpp
1#pragma once
2
3#include "structure/math/spk_safe_comparand.hpp"
4#include "structure/math/spk_vector2.hpp"
5
6namespace spk
7{
12 struct Force2D
13 {
15 spk::Vector2 direction = {0.0f, 0.0f};
17 float magnitude = 0.0f;
18
20 Force2D() = default;
26 Force2D(const spk::Vector2 &p_direction, const float &p_magnitude) :
27 direction(p_direction),
28 magnitude(p_magnitude)
29 {
30 }
31
36 spk::Vector2 vector() const
37 {
38 return direction * magnitude;
39 }
40
46 static Force2D fromVector(const spk::Vector2 &p_vector)
47 {
48 const float length = p_vector.length();
49 if (spk::SafeComparand(length) == 0.0f)
50 {
51 return {};
52 }
53 return Force2D(p_vector / length, length);
54 }
55 };
56}
spk::Vector2 direction
Normalized direction of the force.
Definition spk_force_2d.hpp:15
Force2D(const spk::Vector2 &p_direction, const float &p_magnitude)
Creates a force from direction and magnitude.
Definition spk_force_2d.hpp:26
Force2D()=default
Creates a zero force.
float magnitude
Force magnitude.
Definition spk_force_2d.hpp:17
static Force2D fromVector(const spk::Vector2 &p_vector)
Builds a force from a vector.
Definition spk_force_2d.hpp:46
spk::Vector2 vector() const
Returns the force vector.
Definition spk_force_2d.hpp:36
float length() const
Computes Euclidean length.
Definition spk_vector2.hpp:490
Wraps arithmetic values to compare with tolerance for floating-point inputs.
Definition spk_safe_comparand.hpp:17