your text
#include “raylib.h”
your text
#include <stdio.h>
your text
your text
struct square
your text
{
your text
// Variables
your text
float x, y;
your text
float w, h;
your text
float gravity;
your text
float velocity;
your text
your text
Rectangle GetRectangle() { // x w h
your text
return Rectangle{ x – w / 2, y – h / x, 80, 80 };
your text
}
your text
int drawsquare() {
your text
DrawRectangleRec(GetRectangle(), RED);
your text
return 0;
your text
}
your text
};
your text
struct obby
your text
{
your text
// Variables
your text
float x, y;
your text
float w, h;
your text
your text
Rectangle GetRectangle() {
your text
return Rectangle{ x, y, w, h };
your text
}
your text
int drawobby() {
your text
DrawRectangleRec(GetRectangle(), BROWN);
your text
return 0;
your text
}
your text
};
your text
your text
int currentLevel = 1;
your text
your text
int main(void)
your text
{
your text
InitWindow(1100, 700, “Basic Platformer”);
your text
your text
SetWindowState(FLAG_VSYNC_HINT);
your text
your text
// Player Attributes
your text
square player;
your text
player.x = 100;
your text
player.y = 400;
your text
player.w = 80;
your text
player.h = 80;
your text
player.gravity = 0.0f;
your text
player.velocity = 0.15f;
your text
your text
// Floor Attributes
your text
obby floor1;
your text
floor1.x = 50;
your text
floor1.y = 575;
your text
floor1.w = 160;
your text
floor1.h = 30;
your text
your text
// DeltaTime
your text
float deltaTime = GetFrameTime();
your text
your text
// Sets the FPS
your text
SetTargetFPS(60);
your text
your text
// While Window is Open
your text
while (!WindowShouldClose())
your text
{
your text
// Player Gravity / Physics
your text
player.gravity += player.velocity;
your text
player.y += player.gravity;
your text
your text
// Collisions
your text
if (CheckCollisionRecs(player.GetRectangle(), floor1.GetRectangle())) {
your text
player.y = floor1.y – 80;
your text
player.gravity = 0;
your text
}
your text
your text
// Input
your text
if (IsKeyPressed(KEY_W)) {
your text
player.gravity = -5;
your text
}
your text
else if (IsKeyDown(KEY_A)) {
your text
player.x -= 4;
your text
}
your text
else if (IsKeyDown(KEY_D)) {
your text
player.x += 4;
your text
currentLevel = 2;
your text
}
your text
your text
BeginDrawing();
your text
your text
// Level 1
your text
if (currentLevel == 1) {
your text
void Level1(void);
your text
{
your text
floor1.drawobby();
your text
}
your text
}
your text
your text
// Level 2
your text
if (currentLevel == 2) {
your text
void Level1(void);
your text
{
your text
your text
}
your text
}
your text
your text
// Level 3
your text
if (currentLevel == 3) {
your text
void Level1(void);
your text
{
your text
your text
}
your text
}
your text
your text
ClearBackground(GRAY);
your text
player.drawsquare();
your text
your text
EndDrawing();
your text
your text
// DeltaTime
your text
deltaTime = GetFrameTime();
your text
}
your text
your text
your text
CloseWindow();
your text
your text
return 0;
your text
}
So Im making this simple platform game on Raylib C++, and I can’t nail down the collisions. I’ve searched many tutorials for collisions but I haven’t found one that fits my situation. I have tried to implement my own collisions 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.
ayden is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.