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

RayCollision.distance from GetRayCollisionBox may be incorrect #4131

Closed
casperbear opened this issue Jul 6, 2024 · 3 comments
Closed

RayCollision.distance from GetRayCollisionBox may be incorrect #4131

casperbear opened this issue Jul 6, 2024 · 3 comments

Comments

@casperbear
Copy link

casperbear commented Jul 6, 2024

Issue description

RayCollision object returned from GetRayCollisionBox contains incorrect distance value if Ray object passed to GetRayCollisionBox has direction value that is not normalized (i.e. Vector3Normalize wasn't applied to it).

I don't know if Ray.direction is supposed to be normalized for correct results of RayLib functions and haven't found it in any documentation.
If it is then it would make sense to expand struct comment

typedef struct Ray {
Vector3 position; // Ray position (origin)
Vector3 direction; // Ray direction (normalized)
} Ray;

If it's not, then there is a bug somewhere in GetRayCollisionBox.

using Vector3Normalize on the caller side solves the issue, it's just unclear if it's supposed approach
RayCollision rayCollision = GetRayCollisionBox(Ray{ projectile.position, Vector3Normalize(projectile.speed) }, boundingBox);

Environment

Windows7, RayLib 5.0, Visual C++ 2019

Code Example

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

int main(int argc, char* args[])
{
    InitWindow(800, 600, "GetRayCollisionBox");

    Ray ray1 = Ray{ Vector3{ 0.0f, 0.0f, 0.0f }, Vector3{ 1.0f, 0.0f, 0.0f } };
    Ray ray10 = Ray{ Vector3{ 0.0f, 0.0f, 0.0f }, Vector3{ 10.0f, 0.0f, 0.0f } };

    BoundingBox boundingBox = BoundingBox{ Vector3{ 19.0f, -1.0f, -1.0f}, Vector3{ 21.0f, 1.0f, 1.0f} };
    
    RayCollision rayCollision1 = GetRayCollisionBox(ray1, boundingBox);
    RayCollision rayCollision10 = GetRayCollisionBox(ray10, boundingBox);
    RayCollision rayCollision10N = GetRayCollisionBox(Ray{ ray10.position, Vector3Normalize(ray10.direction) }, boundingBox);

    printf("rayCollision1.distance: %f\n", rayCollision1.distance); // 19.000000
    printf("rayCollision10.distance: %f\n", rayCollision10.distance); // 1.900000 (may be incorrect)
    printf("rayCollision10N.distance: %f\n", rayCollision10N.distance); // 19.000000

    while (!WindowShouldClose())
    {
        BeginDrawing();
        ClearBackground(RAYWHITE);
        EndDrawing();
    }

    CloseWindow();

    return 0;
}
@raysan5
Copy link
Owner

raysan5 commented Jul 7, 2024

@casperbear Please, could you provide an example illustrating this issue?

@casperbear
Copy link
Author

@raysan5 added example cpp

@raysan5
Copy link
Owner

raysan5 commented Jul 7, 2024

@casperbear Thanks for the example, I reviewed the issue and effectively the direction must be normalized. I added the proposed comment to the struct.

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

No branches or pull requests

2 participants