Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[raymath] Vector2Angle() produces invalid results for some vectors #2828

Closed
3 tasks done
AlxHnr opened this issue Dec 12, 2022 · 0 comments · Fixed by #2829
Closed
3 tasks done

[raymath] Vector2Angle() produces invalid results for some vectors #2828

AlxHnr opened this issue Dec 12, 2022 · 0 comments · Fixed by #2829

Comments

@AlxHnr
Copy link
Contributor

AlxHnr commented Dec 12, 2022

Please, before submitting a new issue verify and check:

  • I tested it on latest raylib version from master branch
  • I checked there is no similar issue already reported
  • My code has no errors or misuse of raylib

Issue description

Vector2Angle() works fine for most values, but fails to compute the angle for certain others. I'm testing with valid normalized vectors which work fine with libraries like glm, online math sites and computing it by hand.

Environment

Fedora 36, Mesa 22.1.7, llvmpipe (LLVM 14.0.0).

Issue Screenshot

Output of the code sample below:

raylib angle: 5.73751
glm angle: 0.545679
angle computed with raylib primitives: 0.545679
----
raylib angle: -1.86645
glm angle: 1.86645
angle computed with raylib primitives: 1.86645

Code Example

#include <cmath>
#include <iostream>

#include <raymath.h>
#include <glm/vec2.hpp>
#include <glm/geometric.hpp>
#include <glm/gtx/vector_angle.hpp>

void wrongNumbers() {
  /* raylib example. */
  {
    Vector2 a = Vector2Normalize({-0.96f, -0.28f});
    Vector2 b = Vector2Normalize({-0.97f, 0.26f});
    float angle = Vector2Angle(a, b);
    std::cout << "raylib angle: " << angle << std::endl;
  }

  /* glm example. */
  {
    glm::vec2 a = glm::normalize(glm::vec2{-0.96f, -0.28f});
    glm::vec2 b = glm::normalize(glm::vec2{-0.97f, 0.26f});
    float angle = glm::angle(a, b);
    std::cout << "glm angle: " << angle << std::endl;
  }

  /* Angle computed with raylib primitives. */
  {
    Vector2 a = Vector2Normalize({-0.96f, -0.28f});
    Vector2 b = Vector2Normalize({-0.97f, 0.26f});
    float angle = acos(Vector2DotProduct(a, b));
    std::cout << "angle computed with raylib primitives: " << angle << std::endl;
  }
}

void goodButNegative() {
  /* raylib example. */
  {
    Vector2 a = Vector2Normalize({0.85f, 0.53f});
    Vector2 b = Vector2Normalize({0.26f, -0.97f});
    float angle = Vector2Angle(a, b);
    std::cout << "raylib angle: " << angle << std::endl;
  }

  /* glm example. */
  {
    glm::vec2 a = glm::normalize(glm::vec2{0.85f, 0.53f});
    glm::vec2 b = glm::normalize(glm::vec2{0.26f, -0.97f});
    float angle = glm::angle(a, b);
    std::cout << "glm angle: " << angle << std::endl;
  }

  /* Angle computed with raylib primitives. */
  {
    Vector2 a = Vector2Normalize({0.85f, 0.53f});
    Vector2 b = Vector2Normalize({0.26f, -0.97f});
    float angle = acos(Vector2DotProduct(a, b));
    std::cout << "angle computed with raylib primitives: " << angle << std::endl;
  }
}

int main() {
  wrongNumbers();
  std::cout << "----" << std::endl;
  goodButNegative();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant