Update to non pointers and not working gravit
This commit is contained in:
12 files changed
+94
-100
No files matched your search
+2
-2
@@ -21,9 +21,9 @@ find_package(raylib REQUIRED)
|
||||
|
||||
add_executable(${PROJECT_NAME}
|
||||
main.c
|
||||
cphysics.c
|
||||
mechanics.c
|
||||
vec_util.c
|
||||
vec_utils.c
|
||||
gravity.c
|
||||
)
|
||||
|
||||
target_link_libraries(${PROJECT_NAME}
|
||||
|
||||
@@ -2,6 +2,6 @@
|
||||
#define CONSTS_H_
|
||||
#define TIMESTEP 1000000
|
||||
#define simx 2
|
||||
|
||||
#define NEWTON_G 6.674e-11
|
||||
|
||||
#endif // CONSTS_H_
|
||||
@@ -1,9 +0,0 @@
|
||||
#include "cphysics.h"
|
||||
#include "objects.h"
|
||||
|
||||
void gravity(universe_t* univ){
|
||||
|
||||
}
|
||||
void magnetism(universe_t* univ){
|
||||
|
||||
}
|
||||
@@ -1,6 +0,0 @@
|
||||
#ifndef CPHYSICS_H_
|
||||
#define CPHYSICS_H_
|
||||
#include "objects.h"
|
||||
void gravity(universe_t* univ);
|
||||
void magnetism(universe_t* univ);
|
||||
#endif // CPHYSICS_H_
|
||||
@@ -0,0 +1,26 @@
|
||||
#include "gravity.h"
|
||||
#include "consts.h"
|
||||
#include "vec_utils.h"
|
||||
#include <raylib.h>
|
||||
|
||||
void gravity(universe_t *uni) {
|
||||
mass_body_t body_1;
|
||||
mass_body_t body_2;
|
||||
for (unsigned int i = 0; i < uni->num_bodies - 1; i++) {
|
||||
body_1 = uni->bodies[i];
|
||||
for (unsigned int y = i + 1; y < uni->num_bodies; y++) {
|
||||
body_2 = uni->bodies[y];
|
||||
float force =
|
||||
simx * NEWTON_G * body_1.mass * body_2.mass /
|
||||
(vec2_len(vec2_diff(body_2.position, body_1.position)) * TIMESTEP);
|
||||
Vector2 acceleration =
|
||||
vec2_normalize(vec2_diff(body_2.position, body_1.position));
|
||||
Vector2 acceleration_1 = vec2_scale(acceleration, force / body_1.mass);
|
||||
Vector2 acceleration_2 = vec2_scale(acceleration, -force / body_2.mass);
|
||||
body_1.speed = vec2_sum(body_1.speed, acceleration_1);
|
||||
body_2.speed = vec2_sum(body_1.speed, acceleration_2);
|
||||
uni->bodies[y] = body_2;
|
||||
}
|
||||
uni->bodies[i] = body_1;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
#pragma once
|
||||
#ifndef GRAVITY_H_
|
||||
#define GRAVITY_H_
|
||||
#include "consts.h"
|
||||
#include "objects.h"
|
||||
void gravity(universe_t *uni);
|
||||
|
||||
#endif // MECHANICS_H_
|
||||
@@ -1,7 +1,9 @@
|
||||
#define _POSIX_C_SOURCE 200809L
|
||||
|
||||
#include "consts.h"
|
||||
#include "gravity.h"
|
||||
#include "mechanics.h"
|
||||
#include "objects.h"
|
||||
#include <bits/time.h>
|
||||
#include <math.h>
|
||||
#include <pthread.h>
|
||||
@@ -29,7 +31,9 @@ void *physics(void *args) {
|
||||
universe_t *uni = (universe_t *)args;
|
||||
struct timespec ot, ct;
|
||||
while (true) {
|
||||
printf("a");
|
||||
clock_gettime(CLOCK_MONOTONIC, &ct);
|
||||
// gravity(uni);
|
||||
mechanics(uni);
|
||||
ot = ct;
|
||||
long long elapsed = timespec_diff_ns(ct, ot);
|
||||
@@ -55,39 +59,10 @@ int main(void) {
|
||||
const char *welcome_text = "Welcome to Fucked Physics!";
|
||||
universe_t universe = {0, NULL};
|
||||
universe.bodies = malloc(1024 * sizeof(mass_body_t));
|
||||
universe.num_bodies = 0;
|
||||
for (int slot = -1; slot < 32 * 20 && universe.num_bodies < 1024; slot++) {
|
||||
float radius = 12.0f + 8.0f / (universe.num_bodies + 1);
|
||||
float x = slot < 0
|
||||
? 0.0f
|
||||
: -387.5f + 25.0f * (slot % 32) + (slot / 32 % 2) * 12.5f;
|
||||
float y = slot < 0 ? 0.0f : -209.0f + 22.0f * (slot / 32);
|
||||
if (fabsf(x) + radius > 400.0f || fabsf(y) + radius > 225.0f) {
|
||||
continue;
|
||||
}
|
||||
universe.num_bodies = 2;
|
||||
|
||||
bool overlaps = false;
|
||||
for (unsigned int j = 0; j < universe.num_bodies; j++) {
|
||||
float dx = x - universe.bodies[j].position.x;
|
||||
float dy = y - universe.bodies[j].position.y;
|
||||
float min_distance = radius + universe.bodies[j].radius;
|
||||
if (dx * dx + dy * dy <= min_distance * min_distance) {
|
||||
overlaps = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (overlaps) {
|
||||
continue;
|
||||
}
|
||||
|
||||
unsigned int i = universe.num_bodies++;
|
||||
universe.bodies[i] = (mass_body_t){
|
||||
radius,
|
||||
radius,
|
||||
{x, y},
|
||||
{-0.1f, 0.8f - i * 0.005f},
|
||||
};
|
||||
}
|
||||
universe.bodies[0] = (mass_body_t){100000000000.0f, 200, {0, 0}, {0, 0}};
|
||||
universe.bodies[1] = (mass_body_t){100, 50, {500, 0}, {0, 2}};
|
||||
SetConfigFlags(FLAG_WINDOW_RESIZABLE);
|
||||
InitWindow(800, 450, "Fucked Physics");
|
||||
SetTargetFPS(120);
|
||||
@@ -144,9 +119,9 @@ int main(void) {
|
||||
BeginDrawing();
|
||||
ClearBackground(RAYWHITE);
|
||||
for (unsigned int i = 0; i < universe.num_bodies; i++) {
|
||||
Vector2 scaled_offset = vec2_scale(&canvas_offset, v_scale);
|
||||
Vector2 body_position = vec2_scale(&universe.bodies[i].position, v_scale);
|
||||
body_position = vec2_sum(&body_position, &scaled_offset);
|
||||
Vector2 scaled_offset = vec2_scale(canvas_offset, v_scale);
|
||||
Vector2 body_position = vec2_scale(universe.bodies[i].position, v_scale);
|
||||
body_position = vec2_sum(body_position, scaled_offset);
|
||||
DrawCircleV(transform_to_center(body_position),
|
||||
universe.bodies[i].radius * v_scale,
|
||||
(Color){((256 / universe.num_bodies) * i) % 256,
|
||||
|
||||
+16
-16
@@ -3,7 +3,7 @@
|
||||
#include <stdio.h>
|
||||
|
||||
Vector2 body_collision(mass_body_t *a, mass_body_t *b) {
|
||||
Vector2 diff = vec2_diff(&a->position, &b->position);
|
||||
Vector2 diff = vec2_diff(a->position, b->position);
|
||||
float len = vec2_len(diff);
|
||||
if (len > a->radius + b->radius) {
|
||||
return NULL_VEC2;
|
||||
@@ -15,21 +15,21 @@ Vector2 body_collision(mass_body_t *a, mass_body_t *b) {
|
||||
return result;
|
||||
}
|
||||
|
||||
void collide(mass_body_t *a, mass_body_t *b, const Vector2 *normal,
|
||||
void collide(mass_body_t *a, mass_body_t *b, const Vector2 normal,
|
||||
float resitution) {
|
||||
float inverse_a = 1.0f / a->mass;
|
||||
float inverse_b = 1.0f / b->mass;
|
||||
float inverse_total = inverse_a + inverse_b;
|
||||
float overlap =
|
||||
a->radius + b->radius - vec2_len(vec2_diff(&a->position, &b->position));
|
||||
a->radius + b->radius - vec2_len(vec2_diff(a->position, b->position));
|
||||
Vector2 correction = vec2_scale(normal, overlap / inverse_total);
|
||||
Vector2 a_move = vec2_scale(&correction, inverse_a);
|
||||
Vector2 b_move = vec2_scale(&correction, inverse_b);
|
||||
a->position = vec2_sum(&a->position, &a_move);
|
||||
b->position = vec2_diff(&b->position, &b_move);
|
||||
Vector2 a_move = vec2_scale(correction, inverse_a);
|
||||
Vector2 b_move = vec2_scale(correction, inverse_b);
|
||||
a->position = vec2_sum(a->position, a_move);
|
||||
b->position = vec2_diff(b->position, b_move);
|
||||
|
||||
Vector2 relative = vec2_diff(&b->speed, &a->speed);
|
||||
float v_normal = vec2_dot(&relative, normal);
|
||||
Vector2 relative = vec2_diff(b->speed, a->speed);
|
||||
float v_normal = vec2_dot(relative, normal);
|
||||
if (v_normal <= 0) {
|
||||
return;
|
||||
}
|
||||
@@ -41,10 +41,10 @@ void collide(mass_body_t *a, mass_body_t *b, const Vector2 *normal,
|
||||
|
||||
Vector2 final = vec2_scale(normal, j);
|
||||
|
||||
Vector2 a_diff = vec2_scale(&final, inverse_a);
|
||||
a->speed = vec2_diff(&a->speed, &a_diff);
|
||||
Vector2 b_diff = vec2_scale(&final, inverse_b);
|
||||
b->speed = vec2_sum(&b->speed, &b_diff);
|
||||
Vector2 a_diff = vec2_scale(final, inverse_a);
|
||||
a->speed = vec2_diff(a->speed, a_diff);
|
||||
Vector2 b_diff = vec2_scale(final, inverse_b);
|
||||
b->speed = vec2_sum(b->speed, b_diff);
|
||||
}
|
||||
|
||||
void collide_rigid(mass_body_t *a, float restitution) {
|
||||
@@ -79,14 +79,14 @@ void mechanics(universe_t *uni) {
|
||||
Vector2 scaled_speed;
|
||||
for (unsigned int i = 0; i < uni->num_bodies; i++) {
|
||||
body_1 = uni->bodies[i];
|
||||
scaled_speed = vec2_scale(&body_1.speed, 1.0 / simx);
|
||||
body_1.position = vec2_sum(&body_1.position, &scaled_speed);
|
||||
scaled_speed = vec2_scale(body_1.speed, 1.0 / simx);
|
||||
body_1.position = vec2_sum(body_1.position, scaled_speed);
|
||||
if (i < uni->num_bodies - 1) {
|
||||
for (unsigned int y = i + 1; y < uni->num_bodies; y++) {
|
||||
body_2 = uni->bodies[y];
|
||||
collision = body_collision(&body_1, &body_2);
|
||||
if (!vec2_is_null(collision)) {
|
||||
collide(&body_1, &body_2, &collision, 1.0);
|
||||
collide(&body_1, &body_2, collision, 1.0);
|
||||
}
|
||||
uni->bodies[y] = body_2;
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
#ifndef OBJECTS_H_
|
||||
#define OBJECTS_H_
|
||||
#include "vec_util.h"
|
||||
#include "vec_utils.h"
|
||||
|
||||
typedef struct {
|
||||
float mass;
|
||||
|
||||
-21
@@ -1,21 +0,0 @@
|
||||
#pragma once
|
||||
#ifndef VEC_UTIL_H_
|
||||
#define VEC_UTIL_H_
|
||||
#include <math.h>
|
||||
#include <raylib.h>
|
||||
static const Vector2 NULL_VEC2 = {0, 0};
|
||||
|
||||
bool vec2_is_null(Vector2 v) ;
|
||||
|
||||
Vector2 transform_to_center(Vector2 v);
|
||||
Vector2 vec2_sum(const Vector2 *a, const Vector2 *b);
|
||||
float vec2_len(Vector2 v) ;
|
||||
|
||||
Vector2 vec2_normalize(const Vector2 v) ;
|
||||
|
||||
Vector2 vec2_scale(const Vector2 *normal, const float len) ;
|
||||
|
||||
float vec2_dot(const Vector2 *a, const Vector2 *b) ;
|
||||
Vector2 vec2_diff(const Vector2 *a, const Vector2 *b) ;
|
||||
|
||||
#endif // VEC_UTIL_H_
|
||||
@@ -1,4 +1,4 @@
|
||||
#include "vec_util.h"
|
||||
#include "vec_utils.h"
|
||||
|
||||
|
||||
bool vec2_is_null(Vector2 v) {
|
||||
@@ -15,12 +15,12 @@ Vector2 transform_to_center(Vector2 v) {
|
||||
return result;
|
||||
}
|
||||
|
||||
Vector2 vec2_sum(const Vector2 *a, const Vector2 *b) {
|
||||
Vector2 result = {a->x + b->x, a->y + b->y};
|
||||
Vector2 vec2_sum(const Vector2 a, const Vector2 b) {
|
||||
Vector2 result = {a.x + b.x, a.y + b.y};
|
||||
return result;
|
||||
}
|
||||
Vector2 vec2_diff(const Vector2 *a, const Vector2 *b) {
|
||||
Vector2 result = {a->x - b->x, a->y - b->y};
|
||||
Vector2 vec2_diff(const Vector2 a, const Vector2 b) {
|
||||
Vector2 result = {a.x - b.x, a.y - b.y};
|
||||
return result;
|
||||
}
|
||||
|
||||
@@ -35,11 +35,11 @@ Vector2 vec2_normalize(const Vector2 v) {
|
||||
return result;
|
||||
}
|
||||
|
||||
Vector2 vec2_scale(const Vector2 *normal, const float len) {
|
||||
Vector2 result = {len * normal->x, len * normal->y};
|
||||
Vector2 vec2_scale(const Vector2 normal, const float len) {
|
||||
Vector2 result = {len * normal.x, len * normal.y};
|
||||
return result;
|
||||
}
|
||||
|
||||
float vec2_dot(const Vector2 *a, const Vector2 *b) {
|
||||
return a->x * b->x + a->y * b->y;
|
||||
float vec2_dot(const Vector2 a, const Vector2 b) {
|
||||
return a.x * b.x + a.y * b.y;
|
||||
}
|
||||
+21
@@ -0,0 +1,21 @@
|
||||
#pragma once
|
||||
#ifndef VEC_UTILS_H_
|
||||
#define VEC_UTILS_H_
|
||||
#include <math.h>
|
||||
#include <raylib.h>
|
||||
static const Vector2 NULL_VEC2 = {0, 0};
|
||||
|
||||
bool vec2_is_null(Vector2 v) ;
|
||||
|
||||
Vector2 transform_to_center(Vector2 v);
|
||||
Vector2 vec2_sum(const Vector2 a, const Vector2 b);
|
||||
float vec2_len(Vector2 v) ;
|
||||
|
||||
Vector2 vec2_normalize(const Vector2 v) ;
|
||||
|
||||
Vector2 vec2_scale(const Vector2 normal, const float len) ;
|
||||
|
||||
float vec2_dot(const Vector2 a, const Vector2 b) ;
|
||||
Vector2 vec2_diff(const Vector2 a, const Vector2 b) ;
|
||||
|
||||
#endif // VEC_UTILS_H_
|
||||
Reference in new issue
Block a user