raylib: [raymath] `Vector2Angle()` gives unexpected results

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() gives unexpected results.

Environment

Windows 10 x86_64 Last RayMath

Issue Screenshot

Here I represent angles in degrees for Vector2Angle() and a “fixed” version of it. Red is the last Vector2Angle() Blue is the fixed one vector2angle

#include <raylib.h>
#include <raymath.h>

float Vector2Angle_fixed(Vector2 v1, Vector2 v2)
{
  float result = atan2f(v2.y - v1.y, v2.x - v1.x);
  return result;
}

int main(void)
{
  InitWindow(256, 256, "Vector2Angle Issue");
  SetTargetFPS(60);
  
  while (!WindowShouldClose())
  {
    BeginDrawing();
    ClearBackground(BLACK);
    
    Vector2 Start={(int)(GetRenderWidth()/2),(int)(GetRenderHeight()/2)};
    Vector2 End={GetMouseX(),GetMouseY()};
    
    DrawLine(
      Start.x,
      Start.y,
      Start.x+(cosf(Vector2Angle(Start, End))*Vector2Distance(Start, End)),
      Start.y+(sinf(Vector2Angle(Start, End))*Vector2Distance(Start, End)),
      RED);
      
    DrawLine(
      Start.x,
      Start.y,
      Start.x+(cosf(Vector2Angle_fixed(Start, End))*Vector2Distance(Start, End)),
      Start.y+(sinf(Vector2Angle_fixed(Start, End))*Vector2Distance(Start, End)),
      BLUE);
      
    DrawText(TextFormat("Normal:%.01f°", Vector2Angle(Start, End)*RAD2DEG),       0,  0, 20, WHITE);
    DrawText(TextFormat("Fixed:%.01f°",   Vector2Angle_fixed(Start, End)*RAD2DEG), 0, 20, 20, WHITE);
    
    EndDrawing();
  }
  
  CloseWindow();
  return 0;
}

About this issue

  • Original URL
  • State: closed
  • Created a year ago
  • Comments: 15 (12 by maintainers)

Commits related to this issue

Most upvoted comments

Vector2Angle gives you the angle between two vectors. It does not compute a vector between two points and compute the absolute angle of that new vector.

Vector2Angle is not broken, it just doesn’t do what you expect. It is not Vector TO angle, it is the angle between two vector2s.

The ‘fixed’ function is more like AngleOfLine and it takes a start point and and end point, not two vectors. Note that the arguments Vector2Angle are vectors, not points, yet in your code you call them start and end, clearly points, not vectors. While the data structure may be the same, the sense of the data stored in them is VERY different.

This is not a bug, simply a missunderstanding of what the function is meant to do. Changing it would break many things and remove the ability to find the angle between to vectors, used to know how far to turn to face a target.

You can use it to get the absolute angle of a vector, by simply computing a vector from your points and using a fixed ‘zero’ axis.

Vector2 delta = Vector2Subtract(End, Start);
float angle = Vector2Angle(delta, (Vector2){1,0});

The function Vector2Angle_fixed() in the code is pretty fast and generic And it results -180° to +180°