raylib: [raymath] Vector2LineAngle() may be giving wrong results

As far as I am aware I have done the following:

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

Issue description

Vector2Angle and Vector2LineAngle give results of different signedness. This might be intended behavior but I wasn’t sure.

After looking at some other functions that use angles (Vector2Rotate() and DrawCircleSector()) it seems raylib has a clockwise bias for angles, so I am inclined to think that Vector2Angle() has the correct result of the two.

Issue Screenshot

screenrec001

Solution

In raymath.h, Simply changing “atan2f” in Vector2LineAngle() to “-atan2f” should fix the issue.

Code Example

You can test the behavior with the following program:

#include "raylib.h"

#include "raymath.h"

int main(void){

    int screenWidth = 800;
    int screenHeight = 450;

    InitWindow(screenWidth, screenHeight, "Vector Angle Test");
    SetTargetFPS(60);

    Vector2 vOrigin = { (float)screenWidth/2.0f, (float)screenHeight/2.0f };
    Vector2 v1 = Vector2Add(vOrigin, (Vector2){100.0f, 0.0f});
    Vector2 v2 = Vector2Add(vOrigin, (Vector2){-100.0f, 80.0f}); 
    int v2Flipped = 0;

    float angleVector;
    float angleLine; 

    while(!WindowShouldClose()){

        if (v2Flipped && IsKeyPressed(KEY_SPACE))
        {
            v2 = Vector2Add(vOrigin, (Vector2){-100.0f, 80.0f});
            v2Flipped = 0;
        } 
        else if (IsKeyPressed(KEY_SPACE))
        {
            v2 = Vector2Add(vOrigin, (Vector2){-100.0f, -80.0f});
            v2Flipped = 1;
        }

        Vector2 v1Normal = Vector2Normalize(Vector2Subtract(v1, vOrigin));
        Vector2 v2Normal = Vector2Normalize(Vector2Subtract(v2, vOrigin));

        angleVector = Vector2Angle(v1Normal, v2Normal)*RAD2DEG;
        angleLine = Vector2LineAngle(vOrigin, v2)*RAD2DEG;

        BeginDrawing();

            ClearBackground(RAYWHITE);

            DrawText(TextFormat("ANGLE VECTOR: %3.2f", angleVector), 20, 20, 20.0f, BLACK);
            DrawText(TextFormat("ANGLE LINE: %3.2f", angleLine), 20, 40, 20.0f, BLACK);

            DrawLineEx(vOrigin, v1, 3.0f, BLACK);
            DrawLineEx(vOrigin, v2, 3.0f, BLACK);

            DrawCircleV(vOrigin, 5.0f, LIME);
            DrawCircleV(v1, 5.0f, BLUE);
            DrawCircleV(v2, 5.0f, RED);

            DrawText("vOrigin", vOrigin.x, vOrigin.y+10, 10.0f, DARKGRAY);
            DrawText("v1", v1.x+10, v1.y, 10.0f, DARKGRAY);
            DrawText("v2", v2.x-20, v2.y, 10.0f, DARKGRAY);

        EndDrawing();

    }

    CloseWindow();

}

Apologies if this is painfully bad, it’s my first time submitting an issue.

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Comments: 18 (18 by maintainers)

Most upvoted comments

@raysan5 Thank you! And sorry again I’m not completely used to using git so I think I messed up with the commits on my end.

Just put up a pull request for this #3394 that fixes raymath.h and the corresponding example. I went with clockwise angles just so raylib is consistent for now. If I can throw my hat into the ring, I would vote for counter-clockwise angles, simply because I think that is what most people learn in school (and come to expect.)

Here is a gif of the updated example working, I also added the ability to move v1 around because that’s fun and stuff.

screenrec001

Again, apologies if I mess anything up, this is my first pull request.

Yep, Vector2LineAngle really seem to output the wrong values…

Ray, I think it will be better to make sure there is an explanation on how raylib(raymath) defines its axis grid, so we can better argue about what is wrong and what is right.

I had seen that a lot of has been opened about the angles function, so i think adding a conversion on the outputted angle from raymath’s functions would be a great way to remove any confusion in the future. (If there is already an conversion/explanation on the raymath’s axis, I think it should be much more straightforward on where we can find it, and what it really is)

p.s. sorry for the broken English.