114 lines
2.5 KiB
C
114 lines
2.5 KiB
C
#include <stddef.h>
|
|
#include <raylib.h>
|
|
#include <raymath.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include "objects.h"
|
|
|
|
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, 15, 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));
|
|
DrawLine3D(obj->pos, Vector3Add(obj->pos, obj->vel), PURPLE);
|
|
}
|
|
}
|
|
|
|
void updateobjs()
|
|
{
|
|
for (int i = 0; i < gl.obj_count; i++)
|
|
{
|
|
object *obj = gl.objs[i];
|
|
obj->vel.y -= 0 * GetFrameTime();
|
|
//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);
|
|
}
|
|
obj->vel = Vector3Scale(obj->vel, (1.0f-gl.friction));
|
|
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){10, 40, 0}, 2, BLUE, 1, 200000));
|
|
addobj(newobj((Vector3){50, 20, 0}, 5, RAYWHITE, 0., 5000000));
|
|
Camera3D camera = {{0, 20, 10}, {0, 20, 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;
|
|
|
|
}
|