very bad collision(work in progress)
This commit is contained in:
48
src/collision.c
Normal file
48
src/collision.c
Normal file
@@ -0,0 +1,48 @@
|
||||
#include "world.h"
|
||||
#include <math.h>
|
||||
#include "config.h"
|
||||
#include "collision.h"
|
||||
#include "player.h"
|
||||
|
||||
void checkCollision(){
|
||||
float playerx=0.0;
|
||||
float playery=0.0;
|
||||
float plrsize=0.0;
|
||||
for (int i = 0; i < MAX_ENTITIES; i++){
|
||||
Entity *e = &entities[i];
|
||||
|
||||
if (!e->active)
|
||||
continue;
|
||||
if(e->isPlayer == false){
|
||||
float objposx=e->position.x;
|
||||
//float objposy=e->position.y;
|
||||
float objsize=e->size;
|
||||
|
||||
if(playerx-(plrsize/2)<objposx+(objsize/2) && playerx-(plrsize/2)>objposx-(objsize/2)){
|
||||
ApplyForce(
|
||||
e,
|
||||
(Vector2){10000, 1000}
|
||||
);
|
||||
if(dir){
|
||||
ApplyForce(
|
||||
player,
|
||||
(Vector2){(curhorvel*3), 1000}
|
||||
);
|
||||
}
|
||||
else{
|
||||
ApplyForce(
|
||||
player,
|
||||
(Vector2){-(curhorvel*3), 1000}
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
else{
|
||||
playerx=e->position.x;
|
||||
playery=e->position.y;
|
||||
if(playery==playery)
|
||||
|
||||
plrsize=e->size;
|
||||
}
|
||||
}
|
||||
}
|
||||
8
src/collision.h
Normal file
8
src/collision.h
Normal file
@@ -0,0 +1,8 @@
|
||||
#ifndef COLLISION_H
|
||||
#define COLLISION_H
|
||||
|
||||
// FUNction declaration! yay!!!
|
||||
// run by main.c constantly to allow player input.
|
||||
void checkCollision();
|
||||
|
||||
#endif
|
||||
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
|
||||
10
src/docprogress.txt
Normal file
10
src/docprogress.txt
Normal file
@@ -0,0 +1,10 @@
|
||||
main.c - Done
|
||||
physics.c - Done, refine later
|
||||
physics.h - Done
|
||||
render.c - Done
|
||||
render.h - Done
|
||||
player.c - Done
|
||||
player.h - Done
|
||||
world.c
|
||||
world.h
|
||||
config.h
|
||||
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
|
||||
184
src/main.c
Normal file
184
src/main.c
Normal file
@@ -0,0 +1,184 @@
|
||||
#include <stdio.h>
|
||||
// add all the shit from the other shit
|
||||
#include "raylib.h"
|
||||
#include "world.h"
|
||||
#include "physics.h"
|
||||
#include "player.h"
|
||||
#include "render.h"
|
||||
#include "config.h"
|
||||
#include "collision.h"
|
||||
|
||||
int main(void)
|
||||
{
|
||||
// Let's make a window
|
||||
const int screenWidth = 1000;
|
||||
const int screenHeight = 600;
|
||||
|
||||
InitWindow(
|
||||
screenWidth,
|
||||
screenHeight,
|
||||
"WrldBox Sandbox");
|
||||
|
||||
SetTargetFPS(60);
|
||||
|
||||
// Now we actually start the code
|
||||
InitWorld();
|
||||
|
||||
printf("WrldBox engine started\n");
|
||||
printf("Gravity = %.2f\n", g);
|
||||
|
||||
// until we close the window, expect inputs
|
||||
while (!WindowShouldClose())
|
||||
{
|
||||
float dt = GetFrameTime();
|
||||
|
||||
if (IsKeyPressed(KEY_P))
|
||||
{
|
||||
// If it's simulating, stop simulating. If it isn't simulating, start simulating.
|
||||
isSimulating = !isSimulating;
|
||||
}
|
||||
|
||||
if (IsKeyPressed(KEY_R))
|
||||
{
|
||||
// VERY complicated reset logic
|
||||
InitWorld();
|
||||
}
|
||||
|
||||
if (IsKeyPressed(KEY_Q))
|
||||
{
|
||||
// We're gonna have to rewrite the entity logic at some point. Because an actual game doesn't just contain a bunch of blocks. But whatever for now...
|
||||
// Q key pressed? -> Poof, box, random position, random size, random color.
|
||||
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))
|
||||
{
|
||||
// Spawn cube where the mouse is when it clicks!
|
||||
Vector2 m = GetMousePosition();
|
||||
|
||||
SpawnEntity(
|
||||
m.x,
|
||||
m.y,
|
||||
25,
|
||||
1.0f,
|
||||
ORANGE);
|
||||
}
|
||||
|
||||
if (isSimulating)
|
||||
{
|
||||
// allow player to be controlled, fit everything within the size of the screen
|
||||
UpdatePlayerControls();
|
||||
checkCollision();
|
||||
UpdateEntities(dt, screenWidth);
|
||||
|
||||
simTime += dt;
|
||||
}
|
||||
|
||||
// UI shenanigans
|
||||
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();
|
||||
}
|
||||
|
||||
// When the window wants to close, close it. What a surprise.
|
||||
CloseWindow();
|
||||
return 0;
|
||||
}
|
||||
132
src/physics.c
Normal file
132
src/physics.c
Normal file
@@ -0,0 +1,132 @@
|
||||
#include <math.h>
|
||||
// better hope you were paying attention in physics class for this one
|
||||
#include "physics.h"
|
||||
#include "world.h"
|
||||
#include "config.h"
|
||||
|
||||
// Main physics update loop for all entities
|
||||
void UpdateEntities(float dt, int screenWidth)
|
||||
{
|
||||
// Iterate through all possible entities
|
||||
for (int i = 0; i < MAX_ENTITIES; i++)
|
||||
{
|
||||
Entity *e = &entities[i];
|
||||
|
||||
// Skip inactive entities
|
||||
if (!e->active)
|
||||
continue;
|
||||
|
||||
// Check if entity is currently touching the ground
|
||||
// YOU'RE GROUNDED!!! lol
|
||||
bool onGround = IsGrounded(e);
|
||||
|
||||
// Apply gravity force if enabled for this entity
|
||||
if (e->affectedByGravity)
|
||||
{
|
||||
ApplyForce(
|
||||
e,
|
||||
(Vector2){0, e->mass * g}
|
||||
);
|
||||
}
|
||||
|
||||
// Compute acceleration from accumulated forces (F = ma)
|
||||
e->acceleration.x =
|
||||
e->force.x / e->mass;
|
||||
|
||||
e->acceleration.y =
|
||||
e->force.y / e->mass;
|
||||
|
||||
// Integrate velocity from acceleration
|
||||
e->velocity.x +=
|
||||
e->acceleration.x * dt;
|
||||
|
||||
e->velocity.y +=
|
||||
e->acceleration.y * dt;
|
||||
|
||||
// Apply air drag to slow movement over time
|
||||
e->velocity.x *= AIR_DRAG;
|
||||
e->velocity.y *= AIR_DRAG;
|
||||
|
||||
// Apply ground friction when on the ground
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
// Clamp player horizontal speed
|
||||
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;
|
||||
}
|
||||
|
||||
// Integrate position from velocity
|
||||
e->position.x +=
|
||||
e->velocity.x * dt;
|
||||
|
||||
e->position.y +=
|
||||
e->velocity.y * dt;
|
||||
|
||||
// Reset forces after integration step
|
||||
e->force = (Vector2){0, 0};
|
||||
|
||||
// Ground collision
|
||||
if (e->position.y +
|
||||
e->size * 0.5f >=
|
||||
ground_y)
|
||||
{
|
||||
e->position.y =
|
||||
ground_y -
|
||||
e->size * 0.5f;
|
||||
|
||||
e->velocity.y *=
|
||||
-BOUNCE;
|
||||
|
||||
// Stop tiny bounces... or at least, attempt to.
|
||||
if (fabsf(e->velocity.y) < 15.0f)
|
||||
{
|
||||
e->velocity.y = 0;
|
||||
}
|
||||
}
|
||||
|
||||
// Left wall collision
|
||||
if (e->position.x -
|
||||
e->size * 0.5f < 0)
|
||||
{
|
||||
e->position.x =
|
||||
e->size * 0.5f;
|
||||
|
||||
e->velocity.x *= -0.6f;
|
||||
}
|
||||
|
||||
// Right wall collision
|
||||
if (e->position.x +
|
||||
e->size * 0.5f >
|
||||
screenWidth)
|
||||
{
|
||||
e->position.x =
|
||||
screenWidth -
|
||||
e->size * 0.5f;
|
||||
|
||||
e->velocity.x *= -0.6f;
|
||||
}
|
||||
}
|
||||
}
|
||||
9
src/physics.h
Normal file
9
src/physics.h
Normal file
@@ -0,0 +1,9 @@
|
||||
#ifndef PHYSICS_H
|
||||
#define PHYSICS_H
|
||||
|
||||
// FUNction declaration! yay!!!
|
||||
// used by main script to update entities as time goes on
|
||||
// i don't think it's used anywhere else, but double check me
|
||||
void UpdateEntities(float dt, int screenWidth);
|
||||
|
||||
#endif
|
||||
58
src/player.c
Normal file
58
src/player.c
Normal file
@@ -0,0 +1,58 @@
|
||||
#include "player.h"
|
||||
|
||||
#include "world.h"
|
||||
#include "config.h"
|
||||
|
||||
#include "raylib.h"
|
||||
|
||||
// let's a go!
|
||||
float curhorvel=0.0f; //current horizonatal velocity
|
||||
bool dir=0;
|
||||
void UpdatePlayerControls(void)
|
||||
{
|
||||
// if there isn't a player, you probably can't do much
|
||||
if (!player){
|
||||
return;
|
||||
}
|
||||
if(curhorvel==0.0f){
|
||||
curhorvel=65.5f*scale;
|
||||
}
|
||||
// if a key pressed, push player to the left
|
||||
if (IsKeyDown(KEY_A))
|
||||
{
|
||||
dir=1;
|
||||
if(curhorvel<MAX_PLAYER_SPEED){
|
||||
curhorvel=curhorvel*PLAYER_SPEED_FACTOR;
|
||||
}
|
||||
ApplyForce(
|
||||
player,
|
||||
(Vector2){-curhorvel, 0}
|
||||
);
|
||||
}
|
||||
// if d key pressed, push player to the right
|
||||
else if (IsKeyDown(KEY_D))
|
||||
{
|
||||
dir=0;
|
||||
if(curhorvel<=MAX_PLAYER_SPEED){
|
||||
curhorvel=curhorvel*PLAYER_SPEED_FACTOR;
|
||||
}
|
||||
ApplyForce(
|
||||
player,
|
||||
(Vector2){curhorvel, 0}
|
||||
);
|
||||
}
|
||||
else{
|
||||
curhorvel=65.5f*scale;
|
||||
}
|
||||
// if sspace key pressed (and player isn't already jumping), push player
|
||||
if (IsGrounded(player) &&
|
||||
IsKeyPressed(KEY_SPACE))
|
||||
{
|
||||
player->velocity.y = 0;
|
||||
|
||||
ApplyForce(
|
||||
player,
|
||||
(Vector2){0, -JUMP_FORCE}
|
||||
);
|
||||
}
|
||||
}
|
||||
10
src/player.h
Normal file
10
src/player.h
Normal file
@@ -0,0 +1,10 @@
|
||||
#ifndef PLAYER_H
|
||||
#define PLAYER_H
|
||||
|
||||
// FUNction declaration! yay!!!
|
||||
// run by main.c constantly to allow player input.
|
||||
extern bool dir;
|
||||
extern float curhorvel;
|
||||
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"
|
||||
// Take entities, put em on the screen. Basic rendering file. Our entities are just rectangles, which we should lowkey fix... but uh... that's a problem for LATER...
|
||||
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);
|
||||
}
|
||||
}
|
||||
7
src/render.h
Normal file
7
src/render.h
Normal file
@@ -0,0 +1,7 @@
|
||||
#ifndef RENDER_H
|
||||
#define RENDER_H
|
||||
// FUNction declaration! yay!!!
|
||||
// Make an entity! Once again, pretty sure this is must used in main.c
|
||||
void DrawEntities(void);
|
||||
|
||||
#endif
|
||||
139
src/world.c
Normal file
139
src/world.c
Normal file
@@ -0,0 +1,139 @@
|
||||
#include <stddef.h>
|
||||
#include "world.h"
|
||||
|
||||
// the beeg one. hooooooooooooooooh boy
|
||||
|
||||
const float scale = 43.7445319335f;
|
||||
const float g = 9.81f * scale;
|
||||
|
||||
const float PLAYER_SPEED_FACTOR =1.1f; //1.1 is good
|
||||
const float JUMP_FORCE = 450.0f*scale;
|
||||
|
||||
const float AIR_DRAG = 0.999f;
|
||||
|
||||
const float GROUND_FRICTION = 27.432f*scale;
|
||||
const float MAX_PLAYER_SPEED = 10.0f*scale;
|
||||
|
||||
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;
|
||||
}
|
||||
// say bye bye to EVERYTHINGGGG AHAHAHAHAHAAH
|
||||
// im a little mad with power
|
||||
// this is literally just a helper function to clear the entities and player
|
||||
void ClearWorld(void)
|
||||
{
|
||||
for (int i = 0; i < MAX_ENTITIES; i++)
|
||||
{
|
||||
entities[i].active = false;
|
||||
}
|
||||
|
||||
player = NULL;
|
||||
}
|
||||
|
||||
// used in main.c to start the actual engine behaviors. spawns the player, and a few other boxes for funsies.
|
||||
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;
|
||||
}
|
||||
|
||||
// helper function: count number of entities. Something somewhere uses this. I think it's probably for entity counting. Don't quote me on that.
|
||||
int CountEntities(void)
|
||||
{
|
||||
int count = 0;
|
||||
|
||||
for (int i = 0; i < MAX_ENTITIES; i++)
|
||||
{
|
||||
if (entities[i].active)
|
||||
count++;
|
||||
}
|
||||
|
||||
return count;
|
||||
}
|
||||
34
src/world.h
Normal file
34
src/world.h
Normal file
@@ -0,0 +1,34 @@
|
||||
#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;
|
||||
|
||||
extern const float MAX_PLAYER_SPEED;
|
||||
|
||||
extern const float PLAYER_SPEED_FACTOR;
|
||||
|
||||
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