fix trigonometry functions so angles correspond with GL coordinates, add function to calculate angle between two vectors
This commit is contained in:
parent
3444afc2c1
commit
7e31b5a1c0
|
@ -2,10 +2,15 @@
|
|||
|
||||
glm::vec2 sb::velocity_to_delta(float angle, float magnitude)
|
||||
{
|
||||
return {glm::sin(angle) * magnitude, -glm::cos(angle) * magnitude};
|
||||
return {glm::sin(angle) * magnitude, glm::cos(angle) * magnitude};
|
||||
}
|
||||
|
||||
glm::vec2 sb::velocity_to_delta(glm::vec2 velocity)
|
||||
{
|
||||
return velocity_to_delta(velocity.x, velocity.y);
|
||||
}
|
||||
|
||||
float sb::angle_between(glm::vec2 start, glm::vec2 end)
|
||||
{
|
||||
return glm::atan(end.x - start.x, end.y - start.y);
|
||||
}
|
||||
|
|
25
src/math.hpp
25
src/math.hpp
|
@ -10,8 +10,26 @@
|
|||
|
||||
[math.hpp]
|
||||
|
||||
For math helper functions that require only GLM primitives.
|
||||
|
||||
For math helper functions that require only GLM primitives, especially trigonometric functions.
|
||||
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:
|
||||
|
||||
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)) << " " <<
|
||||
glm::degrees(sb::angle_between(a, c)) << " " << glm::degrees(sb::angle_between(c, a)) << " " <<
|
||||
glm::degrees(sb::angle_between(b, e)) << " " << glm::degrees(sb::angle_between(e, b)) << std::endl <<
|
||||
sb::velocity_to_delta(sb::angle_between(a, b), 4.0f * glm::sqrt(2.0f)) << " " <<
|
||||
sb::velocity_to_delta(sb::angle_between(b, a), 4.0f * glm::sqrt(2.0f)) << " " <<
|
||||
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:
|
||||
|
||||
-135 45 -90 90 116.565 -63.435
|
||||
{-4, -4} {4, 4} {0, 4} {0.894427, -0.447214}
|
||||
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
@ -28,4 +46,7 @@ namespace sb
|
|||
|
||||
/* Convert a vector containing angle and magnitude to an X and Y offset vector. */
|
||||
glm::vec2 velocity_to_delta(glm::vec2);
|
||||
|
||||
/* 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);
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue