Delta Time

I am unsure of what I am doing wrong…ive done your code exactly the same but for some unknown reason scarfy jumps off the screen instantly when I hit space. This has started since I added delta time.

#include “raylib.h”

int main()

{

//Window dimensions

const int windowWidth{512};

const int windowHeight{380};

//Initialize the window

InitWindow(windowWidth, windowHeight, "Robert's Dapper Dasher Game");

// acceleration due to gravity (pixels/fram)/frame

const int gravity{1'000};

Texture2D scarfy = LoadTexture("textures/scarfy.png");

Rectangle scarfyRec;

scarfyRec.width = scarfy.width/6;

scarfyRec.height = scarfy.height;

scarfyRec.x = 0;

scarfyRec.y = 0;

Vector2 scarfyPos;

scarfyPos.x = windowWidth/2 - scarfyRec.width/2;

scarfyPos.y = windowHeight - scarfyRec.height;

bool isInAir{false};

const int jumpVel{-600};

int velocity{0};

SetTargetFPS(60);

while (!WindowShouldClose())

{

    //delta time

    const float dT{GetFrameTime()};

   //Start drawing

    BeginDrawing();

    ClearBackground(WHITE);

    scarfyPos.y += velocity;

    // ground check

    if(scarfyPos.y >= windowHeight - scarfyRec.height)

    {

        // rectangle is on the ground

        velocity = 0;

        isInAir = false;

    }

    else

    {

        // rectangle is in the air

        //apply gravity

        velocity += gravity * dT;

        isInAir = true;

    }

    //check for jumping

    if (IsKeyPressed(KEY_SPACE) && !isInAir)

    {

        velocity += jumpVel;

    }

    //Update position

    scarfyPos.y += velocity * dT;

    DrawTextureRec(scarfy, scarfyRec, scarfyPos, WHITE);

    //Stop drawing

    EndDrawing();

}

UnloadTexture(scarfy);

CloseWindow();

}

This topic was automatically closed 20 days after the last reply. New replies are no longer allowed.

Privacy & Terms