1

I am making a game with raylib. Collision seems to work except I can't move when I am touching the ground.

//including raylib

#include<raylib.h>

//defining the G - gravity
#define G 3.0f

//main function
int main()
{
        // player struct
        //SetExitKey();
        struct player
        {
                // old position used for colision
                Vector2 oldPos;

                //new position used for colision
                Vector2 newPos;

                // simply the player color
                Color color;
        };

        //constants
        const int screenWidth=800;
        const int screenHeight=450;

        float plSpeed=5.0f;

        //objects
        struct player pl;
        pl.newPos.x=screenWidth/2;
        pl.newPos.y=screenHeight/2;

        pl.color=MAROON;

        //player rectangle for colision
        Rectangle plr;

        //the obsacle
        Rectangle floor={
        0,400,screenWidth,55,
        };

        //creating the window

        InitWindow(screenWidth,screenHeight,"Raylib Tutorial P1");

        //target fps
        SetTargetFPS(60);

        //Main Game Loop 
        //heart of the game

        while(!WindowShouldClose())
        {
                // update position
                if (IsKeyDown(KEY_RIGHT)) pl.newPos.x += plSpeed;
                if (IsKeyDown(KEY_LEFT)) pl.newPos.x -= plSpeed;
                if (IsKeyDown(KEY_UP)) pl.newPos.y -= plSpeed;

                pl.newPos.y += G;

                //colision detection
                int coliding=0;

                if (CheckCollisionRecs(plr,floor))
                {
                coliding=1;
                }

                if(!coliding) pl.oldPos=pl.newPos;
                else pl.newPos=pl.oldPos;

                //set player rect

                plr.x=pl.newPos.x;
                plr.y=pl.newPos.y;
                plr.width=30;
                plr.height=30;


                //drawing

                BeginDrawing();

                //set background color
                ClearBackground(RAYWHITE);

                DrawRectangleRec(floor,GREEN);

                //draw player
                DrawRectangleRec(plr,pl.color);

                EndDrawing();


        }

        // De-initialization
        CloseWindow();

        return 0;
} 

Here I move the player and if they collide I teleport them back to the last point. If not I update the oldPos variable.

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
arydev
  • 11
  • 4

1 Answers1

3

Because you're constantly colliding with the floor if you're on it. Then the position doesn't get updated.

I thought this:

pl.newPos.y += G;

was a problem, because I thought G should accelerate you, not move you, but apparently the player doesn't really have a speed, just a position. Currently your player can only make steps of 5 sideways, 5 up and 3 down. It seems like you can't hit the floor once you jump, because you'll stay at height 2.

Maybe you should handle your x and y directions separately when checking collisions (to fix your original problem). Your minimum step sizes also seem big, but that depends on the rest of the game.

Gnoom
  • 183
  • 6