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

Make camera movement independant of framerate #4247

Merged
merged 1 commit into from
Aug 12, 2024

Conversation

hanaxars
Copy link
Contributor

Instead of moving camera with constant speed per frame, speed is multiplied with delta time before movement.

Instead of moving camera with constant speed per frame, speed is multiplied with delta time before movement.
@hanaxars
Copy link
Contributor Author

Camera can be tested with this small example. Try to lock/unlock fps while moving camera around, the movement stays constant and independent of ramerate.

#include "raylib.h"

int main(void)
{
    InitWindow(800, 450, "Camera Test");

    Camera camera = { 0 };
    camera.position = (Vector3){ 50.0f, 10.0f, 0.0f };
    camera.target = (Vector3){ 0.0f, 10.0f, 0.0f };
    camera.up = (Vector3){ 0.0f, 1.0f, 0.0f };
    camera.fovy = 45.0f;
    camera.projection = CAMERA_PERSPECTIVE;

    DisableCursor();
    SetTargetFPS(60);
    bool lockFPS = true;

    while (!WindowShouldClose())
    {
        UpdateCamera(&camera, CAMERA_FREE);

        if (IsKeyPressed(KEY_F))
        {
            lockFPS = !lockFPS;
            if (lockFPS)
                SetTargetFPS(60);
            else
                SetTargetFPS(0);
        }

        BeginDrawing();
            ClearBackground(RAYWHITE);

            BeginMode3D(camera);
                DrawGrid(100, 1.0f);
            EndMode3D();

            DrawText("Press F to Lock/Unlock FPS", 0, 20, 20, MAROON);
            DrawFPS(0, 0);
        EndDrawing();
    }

    CloseWindow();
    return 0;
}

@raysan5
Copy link
Owner

raysan5 commented Aug 11, 2024

@hanaxars I'm afraid this change couples camera.h module to raylib, the intention of the module was avoiding that if possible

@hanaxars
Copy link
Contributor Author

rcamera.h was already coupled to raylib with GetMouseDelta() GetMouseWheelMove() IsKeyDown() IsKeyPressed() GetFrameTime() and I thought it was ok to use another GetFrameTime() call.
In this case I can't think a proper way to get frame time without communicating with raylib unfortunately.

@raysan5 raysan5 merged commit 65c4003 into raysan5:master Aug 12, 2024
@raysan5
Copy link
Owner

raysan5 commented Aug 12, 2024

@hanaxars You are right, the coupling was done on last library update and I still need to review it somehow...

Ok, I'm merging it for now...

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 this pull request may close these issues.

2 participants