angle difference and ratio methods
This commit is contained in:
parent
7e31b5a1c0
commit
d1cb24a785
|
@ -1,9 +1,9 @@
|
|||
/* /\ +--------------------------------------------------------------+
|
||||
____/ \____ /| - zlib/MIT/Unlicenced game framework licensed to freely use, |
|
||||
\ / / | copy, modify and sell without restriction |
|
||||
+--\ ^__^ /--+ | |
|
||||
| ~/ \~ | | - originally created at [http://nugget.fun] |
|
||||
| ~~~~~~~~~~~~ | +--------------------------------------------------------------+
|
||||
/* /\ +------------------------------------------------------------+
|
||||
____/ \____ /| zlib/MIT/Unlicenced game framework licensed to freely use, |
|
||||
\ / / | copy, modify and sell without restriction |
|
||||
+--\ ^__^ /--+ | |
|
||||
| ~/ \~ | | Learn more about [SPACE BOX] at [shampoo.ooo] |
|
||||
| ~~~~~~~~~~~~ | +------------------------------------------------------------+
|
||||
| SPACE ~~~~~ | /
|
||||
| ~~~~~~~ BOX |/
|
||||
+-------------*/
|
||||
|
|
|
@ -91,12 +91,12 @@ void Delegate::cancel_propagation()
|
|||
cancelling_propagation = true;
|
||||
}
|
||||
|
||||
const std::string& Delegate::get_event_command(SDL_Event& event) const
|
||||
const std::string& Delegate::get_event_command(SDL_Event& event)
|
||||
{
|
||||
return *static_cast<std::string*>(event.user.data1);
|
||||
}
|
||||
|
||||
bool Delegate::get_event_cancel_state(SDL_Event& event) const
|
||||
bool Delegate::get_event_cancel_state(SDL_Event& event)
|
||||
{
|
||||
return *static_cast<bool*>(event.user.data2);
|
||||
}
|
||||
|
|
|
@ -41,13 +41,13 @@ public:
|
|||
Delegate(Node*);
|
||||
void add_subscriber(Subscriber, std::uint32_t);
|
||||
void dispatch();
|
||||
bool compare(SDL_Event&, const std::vector<std::string>&, bool = false, bool = false);
|
||||
bool compare(SDL_Event&, const std::string& = "", bool = false, bool = false);
|
||||
bool compare_cancel(SDL_Event&, const std::string& = "");
|
||||
bool compare_neutral(SDL_Event&, const std::string& = "");
|
||||
static bool compare(SDL_Event&, const std::vector<std::string>&, bool = false, bool = false);
|
||||
static bool compare(SDL_Event&, const std::string& = "", bool = false, bool = false);
|
||||
static bool compare_cancel(SDL_Event&, const std::string& = "");
|
||||
static bool compare_neutral(SDL_Event&, const std::string& = "");
|
||||
void cancel_propagation();
|
||||
const std::string& get_event_command(SDL_Event&) const;
|
||||
bool get_event_cancel_state(SDL_Event&) const;
|
||||
static const std::string& get_event_command(SDL_Event&);
|
||||
static bool get_event_cancel_state(SDL_Event&);
|
||||
|
||||
template<typename T>
|
||||
void subscribe(void(T::*f)(SDL_Event&), T* o, std::uint32_t type = command_event_type)
|
||||
|
|
|
@ -531,14 +531,6 @@ Audio& Game::get_audio()
|
|||
return audio;
|
||||
}
|
||||
|
||||
/* Applies delta timing to a vector: returns the passed vector as weighted by the amount of time passed since the
|
||||
* last frame update, allowing for vector values to change the same amount over time independent of the frame rate */
|
||||
glm::vec2 Game::weight(glm::vec2 motion)
|
||||
{
|
||||
return {weight(motion.x), weight(motion.y)};
|
||||
}
|
||||
|
||||
|
||||
void Game::run()
|
||||
{
|
||||
SDL_FlushEvents(SDL_FIRSTEVENT, SDL_LASTEVENT);
|
||||
|
|
|
@ -107,7 +107,6 @@ public:
|
|||
const Input& get_input() const;
|
||||
Input& get_input();
|
||||
Audio& get_audio();
|
||||
glm::vec2 weight(glm::vec2);
|
||||
void run();
|
||||
void frame(float);
|
||||
void flag_to_end();
|
||||
|
@ -120,9 +119,10 @@ public:
|
|||
~Game();
|
||||
|
||||
/* Applies delta timing to a value: returns the value as weighted by the amount of time passed since the
|
||||
* last frame update, allowing for values to change the same amount over time independent of the frame rate */
|
||||
* last frame update, allowing for values to change the same amount over time independent of the frame rate.
|
||||
* The amount is how much the value should change per second. */
|
||||
template<typename T>
|
||||
float weight(T amount)
|
||||
T weight(T amount)
|
||||
{
|
||||
return (last_frame_length / (1000.0 / 60)) * amount;
|
||||
}
|
||||
|
|
10
src/math.cpp
10
src/math.cpp
|
@ -14,3 +14,13 @@ float sb::angle_between(glm::vec2 start, glm::vec2 end)
|
|||
{
|
||||
return glm::atan(end.x - start.x, end.y - start.y);
|
||||
}
|
||||
|
||||
float sb::angle_difference(float start, float end)
|
||||
{
|
||||
return glm::atan(glm::sin(end - start), glm::cos(end - start));
|
||||
}
|
||||
|
||||
float sb::angle_ratio(float start, float end)
|
||||
{
|
||||
return sb::angle_difference(start, end) / glm::pi<float>();
|
||||
}
|
||||
|
|
70
src/math.hpp
70
src/math.hpp
|
@ -2,7 +2,7 @@
|
|||
____/ \____ /| - zlib/MIT/Unlicenced game framework licensed to freely use, |
|
||||
\ / / | copy, modify and sell without restriction |
|
||||
+--\ ^__^ /--+ | |
|
||||
| ~/ \~ | | - originally created at [http://nugget.fun] |
|
||||
| ~/ \~ | | - originally created at [https://shampoo.ooo] |
|
||||
| ~~~~~~~~~~~~ | +--------------------------------------------------------------+
|
||||
| SPACE ~~~~~ | /
|
||||
| ~~~~~~~ BOX |/
|
||||
|
@ -14,7 +14,7 @@
|
|||
Angle values are in radians. 0 is directly up on the screen in GL coordinates, and the angle increases
|
||||
clockwise.
|
||||
|
||||
This test of points on a square and one point below the square should print the expected output:
|
||||
This test of points on a square and one point below the square should print the output below it:
|
||||
|
||||
glm::vec2 a {5.0f, 5.0f}, b {1.0f, 1.0f}, c {1.0f, 5.0f}, d {5.0f, 1.0f}, e {5.0f, -1.0f};
|
||||
std::cout << glm::degrees(sb::angle_between(a, b)) << " " << glm::degrees(sb::angle_between(b, a)) << " " <<
|
||||
|
@ -25,11 +25,25 @@
|
|||
sb::velocity_to_delta(sb::angle_between(d, a), 4.0f) << " " <<
|
||||
sb::velocity_to_delta(sb::angle_between(b, e), 1.0f) << std::endl;
|
||||
|
||||
That should print:
|
||||
Should print:
|
||||
|
||||
-135 45 -90 90 116.565 -63.435
|
||||
{-4, -4} {4, 4} {0, 4} {0.894427, -0.447214}
|
||||
|
||||
|
||||
This test of angle differences should print the output below it:
|
||||
|
||||
float a = 0.0f, b = glm::pi<float>(), c = glm::half_pi<float>(), d = 0.5f, e = glm::pi<float>() * 4;
|
||||
std::cout << sb::angle_difference(a, b) << " " << sb::angle_difference(a, c) << " " << sb::angle_difference(b, c) << " " <<
|
||||
sb::angle_difference(a, d) << " " << sb::angle_difference(c, d) << " " << sb::angle_difference(a, e) << " " <<
|
||||
sb::angle_difference(c, e) << " " << sb::angle_difference(d, e) << " " << sb::angle_difference(c, c) << std::endl <<
|
||||
sb::angle_ratio(a, b) << " " << sb::angle_ratio(a, c) << " " << sb::angle_ratio(b, c) << " " << sb::angle_ratio(a, d) << " " <<
|
||||
sb::angle_ratio(c, d) << " " << sb::angle_ratio(a, e) << " " << sb::angle_ratio(c, e) << " " << sb::angle_ratio(d, e) << " " <<
|
||||
sb::angle_ratio(c, c) << std::endl;
|
||||
|
||||
Should print:
|
||||
|
||||
-3.14159 1.5708 -1.5708 0.5 -1.0708 3.49691e-07 -1.5708 -0.5 0
|
||||
-1 0.5 -0.5 0.159155 -0.340845 1.1131e-07 -0.5 -0.159155 0
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
@ -41,12 +55,48 @@
|
|||
|
||||
namespace sb
|
||||
{
|
||||
/* Convert a vector described by the given angle and magnitude to an X and Y offset vector. */
|
||||
glm::vec2 velocity_to_delta(float, float);
|
||||
/*!
|
||||
* Convert a vector described by the given angle and magnitude to an X and Y offset vector.
|
||||
*
|
||||
* @param angle a float representing the angle of velocity in radians, with 0 being up on the screen and increasing
|
||||
* values going clockwise
|
||||
* @param magnitude a float representing the speed, or the magnitude of the input vector in the X/Y plane
|
||||
* @return a glm::vec2 of the change in X and Y for the given velocity vector
|
||||
*/
|
||||
glm::vec2 velocity_to_delta(float angle, float magnitude);
|
||||
|
||||
/* Convert a vector containing angle and magnitude to an X and Y offset vector. */
|
||||
glm::vec2 velocity_to_delta(glm::vec2);
|
||||
/*!
|
||||
* @overload glm::vec2 velocity_to_delta(float angle, float magnitude)
|
||||
*/
|
||||
glm::vec2 velocity_to_delta(glm::vec2 velocity);
|
||||
|
||||
/* Get the angle between two vectors, or the angle the first would rotate to to point toward the second. */
|
||||
float angle_between(glm::vec2, glm::vec2);
|
||||
/*!
|
||||
* Get the angle between two vectors, or the angle the first would rotate to to point toward the second.
|
||||
*
|
||||
* @param start X/Y coordinates
|
||||
* @param end X/Y coordinates
|
||||
* @return an angle in radians
|
||||
*/
|
||||
float angle_between(glm::vec2 start, glm::vec2 end);
|
||||
|
||||
/*!
|
||||
* Get the signed shortest angle difference between two angles, or how much the first angle would have to rotate
|
||||
* to be equivalent to the second angle. Negative is counter-clockwise, positive clockwise.
|
||||
*
|
||||
* @param start X/Y coordinates of the angle which the difference will be relative to
|
||||
* @param end X/Y coordinates of the angle which the difference will be rotated to
|
||||
* @return angle difference relative to the start parameter in radians
|
||||
*/
|
||||
float angle_difference(float start, float end);
|
||||
|
||||
/*!
|
||||
* Get the angle difference between two angles as a signed ratio of how much of a 180 degree turn it is. Negative
|
||||
* is counter-clockwise, positive clockwise.
|
||||
*
|
||||
* @param start X/Y coordinates of the angle which the difference will be relative to
|
||||
* @param end X/Y coordinates of the angle which the difference will be rotated to
|
||||
* @return angle difference relative to the start parameter as a signed ratio of how much of a 180 degree
|
||||
* turn it is.
|
||||
*/
|
||||
float angle_ratio(float start, float end);
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue