gravitation, no collisions

This commit is contained in:
uan
2025-12-24 10:51:15 +01:00
parent 63bb2eac8c
commit 5792addc4b
3 changed files with 77 additions and 29 deletions

BIN
main

Binary file not shown.

98
main.c
View File

@@ -1,17 +1,25 @@
#include <stddef.h> #include <stddef.h>
#include <raylib.h> #include <raylib.h>
#include <raymath.h>
#include <stdio.h> #include <stdio.h>
#include "objects.h" #include "objects.h"
struct global { struct global {
const int scwidth, scheight; const int scwidth, scheight;
int fps; int fps;
object *objs[128]; object *objs[128];
size_t obj_count; size_t obj_count;
double terminal_yv; double terminal_v;
}; };
struct global gl = {1600, 900, 60, {}, 0, -20}; struct global gl = {1600, 900, 144, {}, 0, 15};
float min(float a, float b)
{return a>b?b:a;}
float max(float a, float b)
{return a>b?a:b;}
void addobj(object *obj) void addobj(object *obj)
{ {
@@ -27,23 +35,72 @@ void drawobjs()
} }
} }
Vector3 gravitation_force(object *obj1, object *obj2)
{
const double GRAVITY_FACTOR = 1;
Vector3 force;
double dist, mass, total_g;
mass = obj1->mass * obj2->mass;
force = Vector3Subtract(obj2->pos, obj1->pos);
dist = Vector3Length(force);
force = Vector3Normalize(force);
dist = max(dist, obj1->r+obj2->r);
total_g = GRAVITY_FACTOR * mass / (dist * dist);
force = Vector3Scale(force, total_g);
return force;
}
void integrate_g(object *obj1, object *obj2, Vector3 force)
{
Vector3 acc;
double f, a;
f = Vector3Length(force);
//acc = Vector3Scale(Vector3Subtract(obj1->pos, obj2->pos), f);
acc = force;
a = f / obj1->mass;
acc = Vector3Scale(Vector3Normalize(acc), a*GetFrameTime());
obj1->vel = Vector3Add(obj1->vel, acc);
a = f / obj2->mass;
acc = Vector3Scale(Vector3Normalize(acc), a*GetFrameTime());
obj2->vel = Vector3Add(obj2->vel, acc);
}
void updateobjs() void updateobjs()
{ {
for (int i = 0; i < gl.obj_count; i++) for (int i = 0; i < gl.obj_count; i++)
{ {
object *obj = gl.objs[i]; object *obj = gl.objs[i];
obj->vel.y -= 9.81 * GetFrameTime(); obj->vel.y -= 0 * GetFrameTime();
if (obj->vel.y < gl.terminal_yv) //if (obj->vel.y < gl.terminal_yv)
{
obj->vel.y = gl.terminal_yv;
}
if (obj->pos.y < obj->r) { if (obj->pos.y < obj->r) {
obj->pos.y = obj->r; obj->pos.y = obj->r;
obj->vel.y *= -obj->restitution; obj->vel.y *= -obj->restitution;
} }
obj->pos.x += obj->vel.x * GetFrameTime(); for (int j = 0; j < gl.obj_count; j++)
obj->pos.y += obj->vel.y * GetFrameTime(); {
obj->pos.z += obj->vel.z * GetFrameTime(); 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);
//}
}
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);
}
DrawLine3D(obj->pos, Vector3Add(obj->pos, obj->vel), GREEN);
obj->pos = Vector3Add(obj->pos, Vector3Scale(obj->vel, GetFrameTime()));
} }
} }
@@ -56,23 +113,17 @@ void freeobjs()
} }
} }
void applyforce()
{
for (int i = 0; i < gl.obj_count; i++)
{
object *obj = gl.objs[i];
obj->vel.x += 1;
}
}
int main(void) { int main(void) {
InitWindow(gl.scwidth, gl.scheight, "raylib engine"); InitWindow(gl.scwidth, gl.scheight, "raylib engine");
SetTargetFPS(gl.fps); SetTargetFPS(gl.fps);
addobj(newobj((Vector3){0, 30, 0}, 2, RED, 0.5)); addobj(newobj((Vector3){0, 20, 0}, 3, RED, 1, 50000));
addobj(newobj((Vector3){5, 2, 0}, 1, BLUE, 1)); addobj(newobj((Vector3){4, 20, 0}, 1, BLUE, 1, 1000));
addobj(newobj((Vector3){-6, 18, 0}, 2, GREEN, 1, 2000));
gl.objs[1]->vel = (Vector3){0, 0, 2};
Camera3D camera = {{0, 20, -30}, {0, 0, 0}, {0, 1, 0}, 45, CAMERA_PERSPECTIVE}; Camera3D camera = {{0, 20, -30}, {0, 0, 0}, {0, 1, 0}, 45, CAMERA_PERSPECTIVE};
DisableCursor();
while(!WindowShouldClose()) while(!WindowShouldClose())
{ {
@@ -85,11 +136,6 @@ int main(void) {
DrawPlane((Vector3){0, 0, 0}, (Vector2){100, 100}, GRAY); DrawPlane((Vector3){0, 0, 0}, (Vector2){100, 100}, GRAY);
updateobjs(); updateobjs();
drawobjs(); drawobjs();
if (IsKeyPressed(KEY_L)) {
applyforce();
}
EndMode3D(); EndMode3D();
} }
EndDrawing(); EndDrawing();

View File

@@ -5,12 +5,13 @@
typedef struct { typedef struct {
Vector3 pos; Vector3 pos;
Vector3 vel; Vector3 vel;
float r; double r;
Color color; Color color;
float restitution; double restitution;
double mass;
} object; } object;
object *newobj(Vector3 pos, float r, Color c, float rest) object *newobj(Vector3 pos, double r, Color c, double rest, double m)
{ {
object *obj = (object*)malloc(sizeof(object)); object *obj = (object*)malloc(sizeof(object));
obj->r = r; obj->r = r;
@@ -18,5 +19,6 @@ object *newobj(Vector3 pos, float r, Color c, float rest)
obj->color = c; obj->color = c;
obj->vel = (Vector3){0, 0, 0}; obj->vel = (Vector3){0, 0, 0};
obj->restitution = rest; obj->restitution = rest;
obj->mass = m;
return obj; return obj;
} }