#include #include #include #include #include #include "objects.h" #define VEC3FORMAT "%lf, %lf, %lf\n" #define PRINTVEC3(vec) printf("%f, %lf, %lf\n", vec.x, vec.y, vec.z) struct global { const int scwidth, scheight; int fps; object *objs[128]; size_t obj_count; double terminal_v; double friction; }; struct global gl = {1600, 900, 144, {}, 0, 30, 0.001}; 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); DrawSphereWires(obj->pos, obj->r, 16, 16, ColorBrightness(obj->color, -0.5f)); } } void updateobjs() { for (int i = 0; i < gl.obj_count; i++) { object *obj = gl.objs[i]; obj->vel.y -= 0 * GetFrameTime(); obj->vel = Vector3Scale(obj->vel, (1.0f-gl.friction)); //if (obj->vel.y < gl.terminal_yv) if (obj->pos.y < obj->r) { obj->pos.y = obj->r; obj->vel.y *= -obj->restitution; } for (int j = 0; j < gl.obj_count; j++) { if (i==j) continue; object *obj2 = gl.objs[j]; integrate_g(obj, obj2, gravitation_force(obj, obj2)); //float min_dist_delta = Vector3Distance(obj->pos, obj2->pos) - (obj->r+obj2->r); //if (min_dist_delta < 0) //{ // Vector3SubtractValue(obj->pos, min_dist_delta); //} if (Vector3Distance(obj->pos, obj2->pos) < obj->r+obj2->r+0.01f) { resolve_collision(obj, obj2); } } float ratio_speed_to_terminal = Vector3Length(obj->vel)/gl.terminal_v; if (Vector3Length(obj->vel) > gl.terminal_v) { obj->vel = Vector3Scale(obj->vel, 1.0f/ratio_speed_to_terminal); } //PRINTVEC3(obj->vel); DrawLine3D(obj->pos, Vector3Add(obj->pos, obj->vel), PURPLE); obj->pos = Vector3Add(obj->pos, Vector3Scale(obj->vel, GetFrameTime())); } } void freeobjs() { for (int i = 0; i < gl.obj_count; i++) { object *obj = gl.objs[i]; free(obj); } } int main(void) { InitWindow(gl.scwidth, gl.scheight, "raylib engine"); SetTargetFPS(gl.fps); addobj(newobj((Vector3){-100, 60, 0}, 2, BLUE, 0.8, 2000)); addobj(newobj((Vector3){100, 50, 0}, 50, RAYWHITE, 0, 50000000)); Camera3D camera = {{0, 50, 20}, {0, 50, 0}, {0, 1, 0}, 50, CAMERA_PERSPECTIVE}; DisableCursor(); SetWindowMonitor(0); while(!WindowShouldClose()) { BeginDrawing(); { UpdateCamera(&camera, CAMERA_FREE); ClearBackground(BLACK); BeginMode3D(camera); DrawPlane((Vector3){0, 0, 0}, (Vector2){500, 500}, RAYWHITE); updateobjs(); drawobjs(); EndMode3D(); } EndDrawing(); } freeobjs(); CloseWindow(); return 0; }