Entirely expanded upon existing proof-of-concept
This commit is contained in:
21
src/config.h
Normal file
21
src/config.h
Normal file
@@ -0,0 +1,21 @@
|
||||
#ifndef CONFIG_H
|
||||
#define CONFIG_H
|
||||
|
||||
#define MAX_ENTITIES 512
|
||||
|
||||
extern const float scale;
|
||||
extern const float g;
|
||||
|
||||
extern const float PLAYER_FORCE;
|
||||
extern const float JUMP_FORCE;
|
||||
|
||||
extern const float AIR_DRAG;
|
||||
|
||||
extern const float GROUND_FRICTION;
|
||||
extern const float MAX_PLAYER_SPEED;
|
||||
|
||||
extern const float BOUNCE;
|
||||
|
||||
extern const float ground_y;
|
||||
|
||||
#endif
|
||||
26
src/entity.h
Normal file
26
src/entity.h
Normal file
@@ -0,0 +1,26 @@
|
||||
#ifndef ENTITY_H
|
||||
#define ENTITY_H
|
||||
|
||||
#include <stdbool.h>
|
||||
#include "raylib.h"
|
||||
|
||||
typedef struct Entity
|
||||
{
|
||||
bool active;
|
||||
|
||||
bool isPlayer;
|
||||
bool affectedByGravity;
|
||||
|
||||
float mass;
|
||||
float size;
|
||||
|
||||
Vector2 position;
|
||||
Vector2 velocity;
|
||||
Vector2 acceleration;
|
||||
Vector2 force;
|
||||
|
||||
Color color;
|
||||
|
||||
} Entity;
|
||||
|
||||
#endif
|
||||
172
src/main.c
Normal file
172
src/main.c
Normal file
@@ -0,0 +1,172 @@
|
||||
#include <stdio.h>
|
||||
|
||||
#include "raylib.h"
|
||||
|
||||
#include "world.h"
|
||||
#include "physics.h"
|
||||
#include "player.h"
|
||||
#include "render.h"
|
||||
#include "config.h"
|
||||
|
||||
int main(void)
|
||||
{
|
||||
const int screenWidth = 1000;
|
||||
const int screenHeight = 600;
|
||||
|
||||
InitWindow(
|
||||
screenWidth,
|
||||
screenHeight,
|
||||
"WrldBox Sandbox");
|
||||
|
||||
SetTargetFPS(60);
|
||||
|
||||
InitWorld();
|
||||
|
||||
printf("WrldBox Sandbox Started\n");
|
||||
printf("Gravity = %.2f\n", g);
|
||||
|
||||
while (!WindowShouldClose())
|
||||
{
|
||||
float dt = GetFrameTime();
|
||||
|
||||
if (IsKeyPressed(KEY_P))
|
||||
{
|
||||
isSimulating = !isSimulating;
|
||||
}
|
||||
|
||||
if (IsKeyPressed(KEY_R))
|
||||
{
|
||||
InitWorld();
|
||||
}
|
||||
|
||||
if (IsKeyPressed(KEY_Q))
|
||||
{
|
||||
SpawnEntity(
|
||||
GetRandomValue(50, 950),
|
||||
GetRandomValue(20, 150),
|
||||
GetRandomValue(15, 45),
|
||||
1.0f,
|
||||
(Color)
|
||||
{
|
||||
GetRandomValue(50,255),
|
||||
GetRandomValue(50,255),
|
||||
GetRandomValue(50,255),
|
||||
255
|
||||
});
|
||||
}
|
||||
|
||||
if (IsMouseButtonPressed(MOUSE_BUTTON_LEFT))
|
||||
{
|
||||
Vector2 m = GetMousePosition();
|
||||
|
||||
SpawnEntity(
|
||||
m.x,
|
||||
m.y,
|
||||
25,
|
||||
1.0f,
|
||||
ORANGE);
|
||||
}
|
||||
|
||||
if (isSimulating)
|
||||
{
|
||||
UpdatePlayerControls();
|
||||
UpdateEntities(dt, screenWidth);
|
||||
|
||||
simTime += dt;
|
||||
}
|
||||
|
||||
BeginDrawing();
|
||||
|
||||
ClearBackground(RAYWHITE);
|
||||
|
||||
DrawLine(
|
||||
0,
|
||||
(int)ground_y,
|
||||
screenWidth,
|
||||
(int)ground_y,
|
||||
DARKGRAY);
|
||||
|
||||
DrawText(
|
||||
"GROUND",
|
||||
10,
|
||||
(int)ground_y + 5,
|
||||
20,
|
||||
DARKGRAY);
|
||||
|
||||
DrawEntities();
|
||||
|
||||
DrawRectangle(
|
||||
0,
|
||||
0,
|
||||
screenWidth,
|
||||
120,
|
||||
Fade(LIGHTGRAY, 0.4f));
|
||||
|
||||
DrawText(
|
||||
"WRLDBOX SANDBOX",
|
||||
10,
|
||||
10,
|
||||
28,
|
||||
BLACK);
|
||||
|
||||
DrawText(
|
||||
TextFormat("Time: %.2f", simTime),
|
||||
10,
|
||||
45,
|
||||
20,
|
||||
BLACK);
|
||||
|
||||
DrawText(
|
||||
TextFormat("Entities: %d",
|
||||
CountEntities()),
|
||||
10,
|
||||
70,
|
||||
20,
|
||||
BLACK);
|
||||
|
||||
if (player)
|
||||
{
|
||||
DrawText(
|
||||
TextFormat(
|
||||
"Player Pos: %.1f %.1f",
|
||||
player->position.x,
|
||||
player->position.y),
|
||||
250,
|
||||
45,
|
||||
20,
|
||||
BLACK);
|
||||
|
||||
DrawText(
|
||||
TextFormat(
|
||||
"Player Vel: %.1f %.1f",
|
||||
player->velocity.x,
|
||||
player->velocity.y),
|
||||
250,
|
||||
70,
|
||||
20,
|
||||
BLACK);
|
||||
}
|
||||
|
||||
DrawText(
|
||||
isSimulating ?
|
||||
"SIMULATING" :
|
||||
"PAUSED",
|
||||
850,
|
||||
15,
|
||||
25,
|
||||
isSimulating ?
|
||||
GREEN :
|
||||
RED);
|
||||
|
||||
DrawText("A/D = Move", 700, 45, 18, BLACK);
|
||||
DrawText("SPACE = Jump", 700, 65, 18, BLACK);
|
||||
DrawText("LMB = Spawn Cube", 700, 85, 18, BLACK);
|
||||
DrawText("Q = Random Cube", 700, 105, 18, BLACK);
|
||||
DrawText("P = Pause | R = Reset", 700, 125, 18, BLACK);
|
||||
|
||||
EndDrawing();
|
||||
}
|
||||
|
||||
CloseWindow();
|
||||
return 0;
|
||||
}
|
||||
115
src/physics.c
Normal file
115
src/physics.c
Normal file
@@ -0,0 +1,115 @@
|
||||
#include <math.h>
|
||||
|
||||
#include "physics.h"
|
||||
#include "world.h"
|
||||
#include "config.h"
|
||||
|
||||
void UpdateEntities(float dt, int screenWidth)
|
||||
{
|
||||
for (int i = 0; i < MAX_ENTITIES; i++)
|
||||
{
|
||||
Entity *e = &entities[i];
|
||||
|
||||
if (!e->active)
|
||||
continue;
|
||||
|
||||
bool onGround = IsGrounded(e);
|
||||
|
||||
if (e->affectedByGravity)
|
||||
{
|
||||
ApplyForce(
|
||||
e,
|
||||
(Vector2){0, e->mass * g}
|
||||
);
|
||||
}
|
||||
|
||||
e->acceleration.x =
|
||||
e->force.x / e->mass;
|
||||
|
||||
e->acceleration.y =
|
||||
e->force.y / e->mass;
|
||||
|
||||
e->velocity.x +=
|
||||
e->acceleration.x * dt;
|
||||
|
||||
e->velocity.y +=
|
||||
e->acceleration.y * dt;
|
||||
|
||||
e->velocity.x *= AIR_DRAG;
|
||||
e->velocity.y *= AIR_DRAG;
|
||||
|
||||
if (onGround)
|
||||
{
|
||||
if (e->velocity.x > 0)
|
||||
{
|
||||
e->velocity.x -=
|
||||
GROUND_FRICTION * dt;
|
||||
|
||||
if (e->velocity.x < 0)
|
||||
e->velocity.x = 0;
|
||||
}
|
||||
else if (e->velocity.x < 0)
|
||||
{
|
||||
e->velocity.x +=
|
||||
GROUND_FRICTION * dt;
|
||||
|
||||
if (e->velocity.x > 0)
|
||||
e->velocity.x = 0;
|
||||
}
|
||||
}
|
||||
|
||||
if (e->isPlayer)
|
||||
{
|
||||
if (e->velocity.x > MAX_PLAYER_SPEED)
|
||||
e->velocity.x = MAX_PLAYER_SPEED;
|
||||
|
||||
if (e->velocity.x < -MAX_PLAYER_SPEED)
|
||||
e->velocity.x = -MAX_PLAYER_SPEED;
|
||||
}
|
||||
|
||||
e->position.x +=
|
||||
e->velocity.x * dt;
|
||||
|
||||
e->position.y +=
|
||||
e->velocity.y * dt;
|
||||
|
||||
e->force = (Vector2){0, 0};
|
||||
|
||||
if (e->position.y +
|
||||
e->size * 0.5f >=
|
||||
ground_y)
|
||||
{
|
||||
e->position.y =
|
||||
ground_y -
|
||||
e->size * 0.5f;
|
||||
|
||||
e->velocity.y *=
|
||||
-BOUNCE;
|
||||
|
||||
if (fabsf(e->velocity.y) < 15.0f)
|
||||
{
|
||||
e->velocity.y = 0;
|
||||
}
|
||||
}
|
||||
|
||||
if (e->position.x -
|
||||
e->size * 0.5f < 0)
|
||||
{
|
||||
e->position.x =
|
||||
e->size * 0.5f;
|
||||
|
||||
e->velocity.x *= -0.6f;
|
||||
}
|
||||
|
||||
if (e->position.x +
|
||||
e->size * 0.5f >
|
||||
screenWidth)
|
||||
{
|
||||
e->position.x =
|
||||
screenWidth -
|
||||
e->size * 0.5f;
|
||||
|
||||
e->velocity.x *= -0.6f;
|
||||
}
|
||||
}
|
||||
}
|
||||
6
src/physics.h
Normal file
6
src/physics.h
Normal file
@@ -0,0 +1,6 @@
|
||||
#ifndef PHYSICS_H
|
||||
#define PHYSICS_H
|
||||
|
||||
void UpdateEntities(float dt, int screenWidth);
|
||||
|
||||
#endif
|
||||
39
src/player.c
Normal file
39
src/player.c
Normal file
@@ -0,0 +1,39 @@
|
||||
#include "player.h"
|
||||
|
||||
#include "world.h"
|
||||
#include "config.h"
|
||||
|
||||
#include "raylib.h"
|
||||
|
||||
void UpdatePlayerControls(void)
|
||||
{
|
||||
if (!player)
|
||||
return;
|
||||
|
||||
if (IsKeyDown(KEY_A))
|
||||
{
|
||||
ApplyForce(
|
||||
player,
|
||||
(Vector2){-PLAYER_FORCE, 0}
|
||||
);
|
||||
}
|
||||
|
||||
if (IsKeyDown(KEY_D))
|
||||
{
|
||||
ApplyForce(
|
||||
player,
|
||||
(Vector2){PLAYER_FORCE, 0}
|
||||
);
|
||||
}
|
||||
|
||||
if (IsGrounded(player) &&
|
||||
IsKeyPressed(KEY_SPACE))
|
||||
{
|
||||
player->velocity.y = 0;
|
||||
|
||||
ApplyForce(
|
||||
player,
|
||||
(Vector2){0, -JUMP_FORCE}
|
||||
);
|
||||
}
|
||||
}
|
||||
6
src/player.h
Normal file
6
src/player.h
Normal file
@@ -0,0 +1,6 @@
|
||||
#ifndef PLAYER_H
|
||||
#define PLAYER_H
|
||||
|
||||
void UpdatePlayerControls(void);
|
||||
|
||||
#endif
|
||||
38
src/render.c
Normal file
38
src/render.c
Normal file
@@ -0,0 +1,38 @@
|
||||
#include "render.h"
|
||||
#include "world.h"
|
||||
|
||||
#include "raylib.h"
|
||||
|
||||
void DrawEntities(void)
|
||||
{
|
||||
for (int i = 0; i < MAX_ENTITIES; i++)
|
||||
{
|
||||
Entity *e = &entities[i];
|
||||
|
||||
if (!e->active)
|
||||
continue;
|
||||
|
||||
Rectangle rect =
|
||||
{
|
||||
e->position.x - e->size * 0.5f,
|
||||
e->position.y - e->size * 0.5f,
|
||||
e->size,
|
||||
e->size
|
||||
};
|
||||
|
||||
DrawRectangleRec(rect, e->color);
|
||||
DrawRectangleLinesEx(rect, 2, BLACK);
|
||||
|
||||
Vector2 velEnd =
|
||||
{
|
||||
e->position.x + e->velocity.x * 0.10f,
|
||||
e->position.y + e->velocity.y * 0.10f
|
||||
};
|
||||
|
||||
DrawLineEx(
|
||||
e->position,
|
||||
velEnd,
|
||||
2,
|
||||
DARKBLUE);
|
||||
}
|
||||
}
|
||||
6
src/render.h
Normal file
6
src/render.h
Normal file
@@ -0,0 +1,6 @@
|
||||
#ifndef RENDER_H
|
||||
#define RENDER_H
|
||||
|
||||
void DrawEntities(void);
|
||||
|
||||
#endif
|
||||
133
src/world.c
Normal file
133
src/world.c
Normal file
@@ -0,0 +1,133 @@
|
||||
#include <stddef.h>
|
||||
#include "world.h"
|
||||
|
||||
const float scale = 43.7445319335f;
|
||||
const float g = 9.81f * scale;
|
||||
|
||||
const float PLAYER_FORCE = 3500.0f;
|
||||
const float JUMP_FORCE = 22000.0f;
|
||||
|
||||
const float AIR_DRAG = 0.999f;
|
||||
|
||||
const float GROUND_FRICTION = 1200.0f;
|
||||
const float MAX_PLAYER_SPEED = 300.0f;
|
||||
|
||||
const float BOUNCE = 0.45f;
|
||||
|
||||
const float ground_y = 550.0f;
|
||||
|
||||
Entity entities[MAX_ENTITIES];
|
||||
|
||||
Entity *player = NULL;
|
||||
|
||||
bool isSimulating = true;
|
||||
|
||||
float simTime = 0.0f;
|
||||
|
||||
void ApplyForce(Entity *e, Vector2 force)
|
||||
{
|
||||
if (!e || !e->active)
|
||||
return;
|
||||
|
||||
e->force.x += force.x;
|
||||
e->force.y += force.y;
|
||||
}
|
||||
|
||||
bool IsGrounded(Entity *e)
|
||||
{
|
||||
if (!e)
|
||||
return false;
|
||||
|
||||
return (
|
||||
e->position.y +
|
||||
e->size * 0.5f >=
|
||||
ground_y - 2.0f
|
||||
);
|
||||
}
|
||||
|
||||
Entity *SpawnEntity(
|
||||
float x,
|
||||
float y,
|
||||
float size,
|
||||
float mass,
|
||||
Color color)
|
||||
{
|
||||
for (int i = 0; i < MAX_ENTITIES; i++)
|
||||
{
|
||||
if (!entities[i].active)
|
||||
{
|
||||
Entity *e = &entities[i];
|
||||
|
||||
e->active = true;
|
||||
|
||||
e->isPlayer = false;
|
||||
e->affectedByGravity = true;
|
||||
|
||||
e->mass = mass;
|
||||
e->size = size;
|
||||
|
||||
e->position = (Vector2){x, y};
|
||||
e->velocity = (Vector2){0, 0};
|
||||
e->acceleration = (Vector2){0, 0};
|
||||
e->force = (Vector2){0, 0};
|
||||
|
||||
e->color = color;
|
||||
|
||||
return e;
|
||||
}
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
void ClearWorld(void)
|
||||
{
|
||||
for (int i = 0; i < MAX_ENTITIES; i++)
|
||||
{
|
||||
entities[i].active = false;
|
||||
}
|
||||
|
||||
player = NULL;
|
||||
}
|
||||
|
||||
void InitWorld(void)
|
||||
{
|
||||
ClearWorld();
|
||||
|
||||
player = SpawnEntity(
|
||||
120,
|
||||
120,
|
||||
40,
|
||||
1.0f,
|
||||
BLUE);
|
||||
|
||||
if (player)
|
||||
{
|
||||
player->isPlayer = true;
|
||||
}
|
||||
|
||||
for (int i = 0; i < 8; i++)
|
||||
{
|
||||
SpawnEntity(
|
||||
300 + i * 50,
|
||||
50,
|
||||
25,
|
||||
1.0f,
|
||||
RED);
|
||||
}
|
||||
|
||||
simTime = 0.0f;
|
||||
}
|
||||
|
||||
int CountEntities(void)
|
||||
{
|
||||
int count = 0;
|
||||
|
||||
for (int i = 0; i < MAX_ENTITIES; i++)
|
||||
{
|
||||
if (entities[i].active)
|
||||
count++;
|
||||
}
|
||||
|
||||
return count;
|
||||
}
|
||||
30
src/world.h
Normal file
30
src/world.h
Normal file
@@ -0,0 +1,30 @@
|
||||
#ifndef WORLD_H
|
||||
#define WORLD_H
|
||||
|
||||
#include "entity.h"
|
||||
#include "config.h"
|
||||
|
||||
extern Entity entities[MAX_ENTITIES];
|
||||
|
||||
extern Entity *player;
|
||||
|
||||
extern bool isSimulating;
|
||||
|
||||
extern float simTime;
|
||||
|
||||
void ApplyForce(Entity *e, Vector2 force);
|
||||
bool IsGrounded(Entity *e);
|
||||
|
||||
Entity *SpawnEntity(
|
||||
float x,
|
||||
float y,
|
||||
float size,
|
||||
float mass,
|
||||
Color color);
|
||||
|
||||
void ClearWorld(void);
|
||||
void InitWorld(void);
|
||||
|
||||
int CountEntities(void);
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user