How can i change camera3d movement speed in raylib

1.4k Views Asked by At

im a 13 y/o Moroccan programmer and im in a bit of a sticky situation

so im trying to make a first person game so i used raylib since it is the easiest way to do so without using an engine,i wrote the code and stuff and it worked,one problem tho,it was extremely slow so can anyone help me?

here is the code btw:

#include "raylib.h"
int main()
{
    const int screenWidth = 800;
    const int screenHeight = 450;

    InitWindow(screenWidth, screenHeight, "raylib [core] example - basic window");

    float cx = 0.0f;
    float cy = 10.0f;
    float cz = 10.0f;

    Camera3D camera = { 0 };
    camera.position = (Vector3){ cx,cy,cz };  // 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;

    SetCameraMode(camera,CAMERA_FIRST_PERSON);            // Camera mode type

    Vector3 cubepos = {0.0f,1.0f,0.0f};

    SetTargetFPS(60);

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

        BeginDrawing();
        ClearBackground(BLACK);
            BeginMode3D(camera);
                DrawCube(cubepos,3.0f,3.0f,3.0f,RED);
            EndMode3D();
        EndDrawing();
    }

    CloseWindow();

    return 0;
}
1

There are 1 best solutions below

0
On

Looking at the Raylib's UpdateCamera(Camera*) function at https://github.com/raysan5/raylib/blob/master/src/rcamera.h, we shall change a bit.

  1. First copy and paste the rcamera.h into your project directory.
  2. At the beginning of the file add the line #define CAMERA_IMPLEMENTATION
  3. Include the file in your main.cpp after the line ``#include <raylib.h>`
  4. Laslty, in your rcamera.h, change the #define PLAYER_MOVEMENT_SENSITIVITY 2.0f to how much ever you want.

If you face C to C++ conversion issues, particularly in line 220-229, you should replace to:

static CameraData CAMERA = {        // Global CAMERA state context
    0,
    0,
    1.85f,
    { 0 },
    { 'W', 'S', 'D', 'A', 'E', 'Q' },
    341,       // raylib: KEY_LEFT_CONTROL
    342,              // raylib: KEY_LEFT_ALT
    2                 // raylib: MOUSE_BUTTON_MIDDLE
};

You can remove some of the uneeded switch cases (Line 306-405 and 495-592) to increase performane.