Files
simple_physics/main.c
2025-12-23 13:29:26 +01:00

105 lines
1.8 KiB
C

#include <stddef.h>
#include <raylib.h>
#include <stdio.h>
#include "objects.h"
struct global {
const int scwidth, scheight;
int fps;
object *objs[128];
size_t obj_count;
double terminal_yv;
};
struct global gl = {1600, 900, 60, {}, 0, -20};
void addobj(object *obj)
{
gl.objs[gl.obj_count++] = obj;
}
void drawobjs()
{
for (int i = 0; i < gl.obj_count; i++)
{
object *obj = gl.objs[i];
DrawSphere(obj->pos, obj->r, obj->color);
}
}
void updateobjs()
{
for (int i = 0; i < gl.obj_count; i++)
{
object *obj = gl.objs[i];
obj->vel.y -= 9.81 * GetFrameTime();
if (obj->vel.y < gl.terminal_yv)
{
obj->vel.y = gl.terminal_yv;
}
if (obj->pos.y < obj->r) {
obj->pos.y = obj->r;
obj->vel.y *= -obj->restitution;
}
obj->pos.x += obj->vel.x * GetFrameTime();
obj->pos.y += obj->vel.y * GetFrameTime();
obj->pos.z += obj->vel.z * GetFrameTime();
}
}
void freeobjs()
{
for (int i = 0; i < gl.obj_count; i++)
{
object *obj = gl.objs[i];
free(obj);
}
}
void applyforce()
{
for (int i = 0; i < gl.obj_count; i++)
{
object *obj = gl.objs[i];
obj->vel.x += 1;
}
}
int main(void) {
InitWindow(gl.scwidth, gl.scheight, "raylib engine");
SetTargetFPS(gl.fps);
addobj(newobj((Vector3){0, 30, 0}, 2, RED, 0.5));
addobj(newobj((Vector3){5, 2, 0}, 1, BLUE, 1));
Camera3D camera = {{0, 20, -30}, {0, 0, 0}, {0, 1, 0}, 45, CAMERA_PERSPECTIVE};
while(!WindowShouldClose())
{
UpdateCamera(&camera, CAMERA_FREE);
BeginDrawing();
{
ClearBackground(BLACK);
BeginMode3D(camera);
DrawPlane((Vector3){0, 0, 0}, (Vector2){100, 100}, GRAY);
updateobjs();
drawobjs();
if (IsKeyPressed(KEY_L)) {
applyforce();
}
EndMode3D();
}
EndDrawing();
}
freeobjs();
CloseWindow();
return 0;
}