commit 63bb2eac8c7a83af956d9947829de39f834e4b7a Author: uan Date: Tue Dec 23 13:29:26 2025 +0100 first commit diff --git a/main b/main new file mode 100755 index 0000000..2a53b05 Binary files /dev/null and b/main differ diff --git a/main.c b/main.c new file mode 100644 index 0000000..61ec00e --- /dev/null +++ b/main.c @@ -0,0 +1,104 @@ +#include +#include +#include +#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; + +} diff --git a/objects.h b/objects.h new file mode 100644 index 0000000..cc714f2 --- /dev/null +++ b/objects.h @@ -0,0 +1,22 @@ +#pragma once +#include +#include + +typedef struct { + Vector3 pos; + Vector3 vel; + float r; + Color color; + float restitution; +} object; + +object *newobj(Vector3 pos, float r, Color c, float rest) +{ + object *obj = (object*)malloc(sizeof(object)); + obj->r = r; + obj->pos = pos; + obj->color = c; + obj->vel = (Vector3){0, 0, 0}; + obj->restitution = rest; + return obj; +}