WIP docs: commenting in physics.c/physics.h and README updates

This commit is contained in:
2026-06-16 12:24:11 -04:00
parent 8507c2b754
commit 11d7c736ae
3 changed files with 31 additions and 1 deletions

10
src/docprogress.txt Normal file
View File

@@ -0,0 +1,10 @@
main.c - Done
physics.c - Done, refine later
physics.h
renderer.c
renderer.h
player.c
player.h
world.c
world.h
config.h

View File

@@ -1,20 +1,26 @@
#include <math.h> #include <math.h>
// better hope you were paying attention in physics class for this one
#include "physics.h" #include "physics.h"
#include "world.h" #include "world.h"
#include "config.h" #include "config.h"
// Main physics update loop for all entities
void UpdateEntities(float dt, int screenWidth) void UpdateEntities(float dt, int screenWidth)
{ {
// Iterate through all possible entities
for (int i = 0; i < MAX_ENTITIES; i++) for (int i = 0; i < MAX_ENTITIES; i++)
{ {
Entity *e = &entities[i]; Entity *e = &entities[i];
// Skip inactive entities
if (!e->active) if (!e->active)
continue; continue;
// Check if entity is currently touching the ground
// YOU'RE GROUNDED!!! lol
bool onGround = IsGrounded(e); bool onGround = IsGrounded(e);
// Apply gravity force if enabled for this entity
if (e->affectedByGravity) if (e->affectedByGravity)
{ {
ApplyForce( ApplyForce(
@@ -23,21 +29,25 @@ void UpdateEntities(float dt, int screenWidth)
); );
} }
// Compute acceleration from accumulated forces (F = ma)
e->acceleration.x = e->acceleration.x =
e->force.x / e->mass; e->force.x / e->mass;
e->acceleration.y = e->acceleration.y =
e->force.y / e->mass; e->force.y / e->mass;
// Integrate velocity from acceleration
e->velocity.x += e->velocity.x +=
e->acceleration.x * dt; e->acceleration.x * dt;
e->velocity.y += e->velocity.y +=
e->acceleration.y * dt; e->acceleration.y * dt;
// Apply air drag to slow movement over time
e->velocity.x *= AIR_DRAG; e->velocity.x *= AIR_DRAG;
e->velocity.y *= AIR_DRAG; e->velocity.y *= AIR_DRAG;
// Apply ground friction when on the ground
if (onGround) if (onGround)
{ {
if (e->velocity.x > 0) if (e->velocity.x > 0)
@@ -58,6 +68,7 @@ void UpdateEntities(float dt, int screenWidth)
} }
} }
// Clamp player horizontal speed
if (e->isPlayer) if (e->isPlayer)
{ {
if (e->velocity.x > MAX_PLAYER_SPEED) if (e->velocity.x > MAX_PLAYER_SPEED)
@@ -67,14 +78,17 @@ void UpdateEntities(float dt, int screenWidth)
e->velocity.x = -MAX_PLAYER_SPEED; e->velocity.x = -MAX_PLAYER_SPEED;
} }
// Integrate position from velocity
e->position.x += e->position.x +=
e->velocity.x * dt; e->velocity.x * dt;
e->position.y += e->position.y +=
e->velocity.y * dt; e->velocity.y * dt;
// Reset forces after integration step
e->force = (Vector2){0, 0}; e->force = (Vector2){0, 0};
// Ground collision
if (e->position.y + if (e->position.y +
e->size * 0.5f >= e->size * 0.5f >=
ground_y) ground_y)
@@ -86,12 +100,14 @@ void UpdateEntities(float dt, int screenWidth)
e->velocity.y *= e->velocity.y *=
-BOUNCE; -BOUNCE;
// Stop tiny bounces... or at least, attempt to.
if (fabsf(e->velocity.y) < 15.0f) if (fabsf(e->velocity.y) < 15.0f)
{ {
e->velocity.y = 0; e->velocity.y = 0;
} }
} }
// Left wall collision
if (e->position.x - if (e->position.x -
e->size * 0.5f < 0) e->size * 0.5f < 0)
{ {
@@ -101,6 +117,7 @@ void UpdateEntities(float dt, int screenWidth)
e->velocity.x *= -0.6f; e->velocity.x *= -0.6f;
} }
// Right wall collision
if (e->position.x + if (e->position.x +
e->size * 0.5f > e->size * 0.5f >
screenWidth) screenWidth)

View File

@@ -1,6 +1,9 @@
#ifndef PHYSICS_H #ifndef PHYSICS_H
#define 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); void UpdateEntities(float dt, int screenWidth);
#endif #endif