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

[rcamera] GetRayCollisionBox() works weird after DisableCursor() #4726

Closed
ParthenGithub opened this issue Jan 25, 2025 · 2 comments
Closed

Comments

@ParthenGithub
Copy link

ParthenGithub commented Jan 25, 2025

Issue description

I'm making a FPS game and I try to implement shooting. So, I casting GetRayCollisionBox(), but ray it returns looking in the wrong place.

After playing with minimal reproduce example I find that bug appears only if DisableCursor() is written before UpdateCameraPro (or regular UpdateCamera).

If I print DisableCursor() after UpdateCamera in the game cycle everything works normally. IDK may be it's a way how I should it, but it feels quite bad.

Environment

  • Arch Linux (PC)
  • raylib 5.5-1
  • GPU: RX 570
  • OpenGL version string: 4.6 (Compatibility Profile) Mesa 24.3.3-arch1.2

Issue Screenshot

Here, I pointed on cube and clicked, than step back a little bit. (NOTE: you need to move to other angle, from default position ray is normal)
Image

Code Example

I modified this example:

https://www.raylib.com/examples/core/loader.html?name=core_3d_picking

Look for a comments with "BUG" note. Or just go to the beginning of game main cycle.

#include "raylib.h"

//------------------------------------------------------------------------------------
// Program main entry point
//------------------------------------------------------------------------------------
int main(void)
{
    // Initialization
    //--------------------------------------------------------------------------------------
    const int screenWidth = 800;
    const int screenHeight = 450;

    InitWindow(screenWidth, screenHeight, "raylib [core] example - 3d picking");

    // Define the camera to look into our 3d world
    Camera camera = { 0 };
    camera.position = (Vector3){ 10.0f, 10.0f, 10.0f }; // Camera position
    camera.target = (Vector3){ 0.0f, 0.0f, 0.0f };      // Camera looking at point
    camera.up = (Vector3){ 0.0f, 1.0f, 0.0f };          // Camera up vector (rotation towards target)
    camera.fovy = 45.0f;                                // Camera field-of-view Y
    camera.projection = CAMERA_PERSPECTIVE;             // Camera projection type

    Vector3 cubePosition = { 0.0f, 1.0f, 0.0f };
    Vector3 cubeSize = { 2.0f, 2.0f, 2.0f };

    Ray ray = { 0 };                    // Picking line ray
    RayCollision collision = { 0 };     // Ray collision hit info

    SetTargetFPS(60);                   // Set our game to run at 60 frames-per-second
    //--------------------------------------------------------------------------------------
    
    DisableCursor(); // BUG: IF IT'S HERE, THERE IS BUG
    // Main game loop
    while (!WindowShouldClose())        // Detect window close button or ESC key
    {
        // Update
        //----------------------------------------------------------------------------------
        UpdateCamera(&camera,  CAMERA_FIRST_PERSON);                  // Update camera
                          // Move to target (zoom)
        
        // DisableCursor(); // BUG: IF IT'S HERE, EVERYTHING IS NORMAL
        

        // Toggle camera controls
        if (IsMouseButtonPressed(MOUSE_BUTTON_RIGHT))
        {
            if (IsCursorHidden()) EnableCursor();
            else DisableCursor();
        }

        if (IsMouseButtonPressed(MOUSE_BUTTON_LEFT))
        {
            if (!collision.hit)
            {
                ray = GetScreenToWorldRay(GetMousePosition(), camera);

                // Check collision between ray and box
                collision = GetRayCollisionBox(ray,
                            (BoundingBox){(Vector3){ cubePosition.x - cubeSize.x/2, cubePosition.y - cubeSize.y/2, cubePosition.z - cubeSize.z/2 },
                                          (Vector3){ cubePosition.x + cubeSize.x/2, cubePosition.y + cubeSize.y/2, cubePosition.z + cubeSize.z/2 }});
            }
            else collision.hit = false;
        }
        //----------------------------------------------------------------------------------

        // Draw
        //----------------------------------------------------------------------------------
        BeginDrawing();

            ClearBackground(RAYWHITE);

            BeginMode3D(camera);

                if (collision.hit)
                {
                    DrawCube(cubePosition, cubeSize.x, cubeSize.y, cubeSize.z, RED);
                    DrawCubeWires(cubePosition, cubeSize.x, cubeSize.y, cubeSize.z, MAROON);

                    DrawCubeWires(cubePosition, cubeSize.x + 0.2f, cubeSize.y + 0.2f, cubeSize.z + 0.2f, GREEN);
                }
                else
                {
                    DrawCube(cubePosition, cubeSize.x, cubeSize.y, cubeSize.z, GRAY);
                    DrawCubeWires(cubePosition, cubeSize.x, cubeSize.y, cubeSize.z, DARKGRAY);
                }

                DrawRay(ray, MAROON);
                DrawGrid(10, 1.0f);

            EndMode3D();

            DrawText("Try clicking on the box with your mouse!", 240, 10, 20, DARKGRAY);

            if (collision.hit) DrawText("BOX SELECTED", (screenWidth - MeasureText("BOX SELECTED", 30)) / 2, (int)(screenHeight * 0.1f), 30, GREEN);

            DrawText("Right click mouse to toggle camera controls", 10, 430, 10, GRAY);

            DrawFPS(10, 10);

        EndDrawing();
        //----------------------------------------------------------------------------------
    }

    // De-Initialization
    //--------------------------------------------------------------------------------------
    CloseWindow();        // Close window and OpenGL context
    //--------------------------------------------------------------------------------------

    return 0;
}
@ParthenGithub
Copy link
Author

Interesting thing here is that camera works perfectly normal.

Since all of 3D examples written as in code, with DisableCursor(); before, it's seems they all affected.

@ParthenGithub
Copy link
Author

Try replacing the: // DisableCursor(); // BUG: IF IT'S HERE, EVERYTHING IS NORMAL with SetMousePosition(GetScreenWidth()/2, GetScreenHeight()/2); to see if it's that.

Okaaay, I tried this in my code and it's not worked, but in example it worked. So, it's probably my code problem.

Thanks, I will close this issue

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

1 participant