ayden
ayden

Reputation: 1

Collisions in Raylib C++

Im making this simple platform game on Raylib C++, and I can't nail down the collisions. I have tried to implement my own collisions:

#include "raylib.h"
#include <stdio.h>

struct square
{
    // Variables 
    float x, y;
    float w, h;
   float gravity;
    float velocity;

    Rectangle GetRectangle() { //            x   w   h
        return Rectangle{ x - w / 2, y - h / x, 80, 80 };
    }
    int drawsquare() {
        DrawRectangleRec(GetRectangle(), RED);
        return 0;
    }
};
struct obby
{
    // Variables 
    float x, y;
    float w, h;

    Rectangle GetRectangle() {
        return Rectangle{ x, y, w, h };
    }
    int drawobby() {
        DrawRectangleRec(GetRectangle(), BROWN);
        return 0;
    }
};

int currentLevel = 1;

int main(void) 
{
    InitWindow(1100, 700, "Basic Platformer");

    SetWindowState(FLAG_VSYNC_HINT);

    // Player Attributes
    square player;
    player.x = 100;
    player.y = 400;
    player.w = 80;
    player.h = 80;
    player.gravity = 0.0f;
    player.velocity = 0.15f;

    // Floor Attributes
    obby floor1;
    floor1.x = 50;
    floor1.y = 575;
    floor1.w = 160;
    floor1.h = 30;

    // DeltaTime
    float deltaTime = GetFrameTime();

    // Sets the FPS
    SetTargetFPS(60);

    // While Window is Open
    while (!WindowShouldClose()) 
    {
        // Player Gravity / Physics
        player.gravity += player.velocity;
        player.y += player.gravity;

        // Collisions
        if (CheckCollisionRecs(player.GetRectangle(), floor1.GetRectangle())) {
            player.y = floor1.y - 80;
            player.gravity = 0;
        }

        // Input
        if (IsKeyPressed(KEY_W)) {
            player.gravity = -5;
        }
        else if (IsKeyDown(KEY_A)) {
            player.x -= 4;
        }
        else if (IsKeyDown(KEY_D)) {
            player.x += 4;
            currentLevel = 2;
        }

        BeginDrawing();

        // Level 1
        if (currentLevel == 1) {
            void Level1(void);
            {
                floor1.drawobby();
            }
        }

        // Level 2
        if (currentLevel == 2) {
            void Level1(void);
            {

           }
        }

        // Level 3
        if (currentLevel == 3) {
            void Level1(void);
            {

            }
        }

        ClearBackground(GRAY);
        player.drawsquare();

        EndDrawing();

        // DeltaTime
        deltaTime = GetFrameTime();
    }


    CloseWindow();

    return 0;
}

...but it only works for the top of the floor object my player is trying to stand on.

Also, don't worry about the Level voids those are just there for the future.

Upvotes: 0

Views: 248

Answers (0)

Related Questions