Compare commits

...
12 Commits
17 changed files with 371 additions and 156 deletions

No files matched your search

+7
View File
@@ -0,0 +1,7 @@
CompileFlags:
Add: [-xc, -std=c11]
If:
PathMatch: [".*\\.h$"]
CompileFlags:
Add: [-x, c-header]
+12 -5
View File
@@ -1,5 +1,7 @@
cmake_minimum_required(VERSION 3.20)
set(CMAKE_C_COMPILER gcc)
project(fuckedphysics
VERSION 0.1.0
LANGUAGES C
@@ -19,6 +21,10 @@ find_package(raylib REQUIRED)
add_executable(${PROJECT_NAME}
main.c
mechanics.c
vec_utils.c
gravity.c
magnetism.c
)
target_link_libraries(${PROJECT_NAME}
@@ -26,9 +32,10 @@ target_link_libraries(${PROJECT_NAME}
raylib
)
target_compile_options(${PROJECT_NAME}
PRIVATE
-Wall
-Wextra
-Wpedantic
target_compile_options(${PROJECT_NAME} PRIVATE
-Wall
-Wextra
-Wpedantic
$<$<CONFIG:Debug>:-ggdb -O0>
$<$<CONFIG:Release>:-O3>
)
+38
View File
@@ -0,0 +1,38 @@
mass(EARTH_MASS), radius(km), {x(UA), y(UA)}, {vx(km/s), vy(km/s)}, draw_radius(km), charge(C), name
330432.0, 696340.0, {0, 0}, {0, 0}, 696340, 0, Sun
0.055, 2439.7, {0.387, 0}, {0, -38.86}, 80, 0, Mercury
0.815, 6051.8, {0.723, 0}, {0, -34.79}, 190, 0, Venus
1.0, 6371.0, {1.0, 0}, {0, -29.29}, 200, 0, Earth
0.107, 3389.5, {1.524, 0}, {0, -21.97}, 106, 0, Mars
317.83, 69911.0, {5.203, 0}, {0, -12.43}, 400, 0, Jupiter
95.159, 58232.0, {9.582, 0}, {0, -9.11}, 300, 0, Saturn
14.536, 25362.0, {19.201, 0}, {0, -6.80}, 250, 0, Uranus
17.147, 24622.0, {30.069, 0}, {0, -5.43}, 250, 0, Neptune
0.012294, 1737.4, {1.002570, 0}, {0, -30.3083}, 60, 0, Moon
0.000000001775, 11.27, {1.524063, 0}, {0, -24.1070}, 20, 0, Phobos
0.000000000402, 6.20, {1.524157, 0}, {0, -23.3212}, 16, 0, Deimos
0.00000001674, 21.5, {5.203856, 0}, {0, -43.8901}, 15, 0, Metis
0.00000000335, 8.2, {5.203862, 0}, {0, -43.7679}, 12, 0, Adrastea
0.000001256, 83.5, {5.204213, 0}, {0, -38.8569}, 20, 0, Amalthea
0.000000134, 49.3, {5.204483, 0}, {0, -36.3239}, 18, 0, Thebe
0.014956, 1821.49, {5.205820, 0}, {0, -29.7605}, 65, 0, Io
0.008037, 1560.80, {5.207486, 0}, {0, -26.1695}, 60, 0, Europa
0.024813, 2631.20, {5.210155, 0}, {0, -23.3091}, 75, 0, Ganymede
0.018015, 2410.30, {5.215585, 0}, {0, -20.6330}, 70, 0, Callisto
0.000001591, 85.0, {5.279739, 0}, {0, -15.7520}, 20, 0, Himalia
0.000006346, 198.2, {9.583240, 0}, {0, -23.4089}, 24, 0, Mimas
0.000018084, 252.1, {9.583591, 0}, {0, -21.7338}, 28, 0, Enceladus
0.000103479, 531.1, {9.583970, 0}, {0, -20.4559}, 36, 0, Tethys
0.000184187, 561.4, {9.584523, 0}, {0, -19.1353}, 38, 0, Dione
0.000386792, 763.8, {9.585523, 0}, {0, -17.5935}, 44, 0, Rhea
0.0225294, 2575.0, {9.590168, 0}, {0, -14.6817}, 75, 0, Titan
0.000000938, 135.0, {9.592033, 0}, {0, -14.1371}, 20, 0, Hyperion
0.000303071, 734.5, {9.605803, 0}, {0, -12.3738}, 42, 0, Iapetus
0.000011051, 235.8, {19.201865, 0}, {0, -13.4917}, 25, 0, Miranda
0.000226047, 578.9, {19.202277, 0}, {0, -12.3074}, 38, 0, Ariel
0.000195908, 584.7, {19.202780, 0}, {0, -11.4645}, 38, 0, Umbriel
0.000589398, 788.9, {19.203914, 0}, {0, -10.4458}, 45, 0, Titania
0.000504002, 761.4, {19.204901, 0}, {0, -9.9511}, 44, 0, Oberon
0.000008372, 210.0, {30.069686, 0}, {0, -13.0522}, 25, 0, Proteus
0.00358327, 1353.4, {30.071271, 0}, {0, -1.0406}, 55, 0, Triton
0.000005023, 170.0, {30.105758, 0}, {0, -6.5434}, 23, 0, Nereid
+12 -3
View File
@@ -1,7 +1,16 @@
#ifndef CONSTS_H_
#define CONSTS_H_
#define TIMESTEP 1000000
#define simx 2
#define LOOP_TIMESTEP \
1000000LL // Wall-clock nanoseconds between updates (1 kHz).
#define PHYSICS_TIMESTEP 10.0f // Simulated seconds per update.
#define WALL_COLLISIONS 0 // Enable for bounded collision tests.
#define NEWTON_G 6.674e-11
#define EARTH_MASS 5.9722e24 // Kilograms per simulation mass unit.
#define UA 149.6E6
#define EARTH_RADIUS 6378.14 // Kilometers per simulation distance unit.
#define DISTANCE_UNIT_METERS (EARTH_RADIUS * 1000.0)
#define SIMULATION_G \
(NEWTON_G * EARTH_MASS / \
(DISTANCE_UNIT_METERS * DISTANCE_UNIT_METERS * DISTANCE_UNIT_METERS))
#endif // CONSTS_H_
-9
View File
@@ -1,9 +0,0 @@
#include "cphysics.h"
#include "objects.h"
void gravity(universe_t* univ){
}
void magnetism(universe_t* univ){
}
-6
View File
@@ -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_
+25
View File
@@ -0,0 +1,25 @@
#include "gravity.h"
#include "consts.h"
#include "vec_utils.h"
#include <raylib.h>
void gravity(universe_t *uni) {
const float dt = PHYSICS_TIMESTEP;
for (unsigned int i = 0; i < uni->num_bodies; i++) {
mass_body_t *body_1 = &uni->bodies[i];
for (unsigned int y = i + 1; y < uni->num_bodies; y++) {
mass_body_t *body_2 = &uni->bodies[y];
Vector2 separation = vec2_diff(body_2->position, body_1->position);
float distance_squared = vec2_dot(separation, separation);
if (distance_squared == 0.0f) {
continue;
}
Vector2 direction = vec2_normalize(separation);
float factor = SIMULATION_G * dt / distance_squared;
body_1->speed =
vec2_sum(body_1->speed, vec2_scale(direction, factor * body_2->mass));
body_2->speed = vec2_sum(body_2->speed,
vec2_scale(direction, -factor * body_1->mass));
}
}
}
+8
View File
@@ -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_
+32
View File
@@ -0,0 +1,32 @@
#include "magnetism.h"
#include "consts.h"
#include "vec_utils.h"
#include <math.h>
#define EPSILON 8.85418782E-12
void magnetism(universe_t* uni){
for (unsigned int i = 0; i < uni->num_bodies; i++) {
mass_body_t *body_1 = &uni->bodies[i];
for (unsigned int y = i + 1; y < uni->num_bodies; y++) {
mass_body_t *body_2 = &uni->bodies[y];
if (body_1->charge == 0 || body_2->charge == 0) {
continue;
}
Vector2 diff = vec2_diff(body_2->position, body_1->position);
float distance_squared = vec2_dot(diff, diff);
if (distance_squared == 0) {
continue;
}
// Charges are in coulombs; convert acceleration to Earth radii/s^2.
float factor = PHYSICS_TIMESTEP * body_1->charge * body_2->charge /
(4 * PI * EPSILON * EARTH_MASS * DISTANCE_UNIT_METERS *
DISTANCE_UNIT_METERS * DISTANCE_UNIT_METERS * distance_squared);
Vector2 impulse = vec2_scale(vec2_normalize(diff), factor);
body_1->speed = vec2_diff(body_1->speed,
vec2_scale(impulse, 1 / body_1->mass));
body_2->speed = vec2_sum(body_2->speed,
vec2_scale(impulse, 1 / body_2->mass));
}
}
}
+7
View File
@@ -0,0 +1,7 @@
#ifndef MAGNETISM_H
#define MAGNETISM_H
#include "objects.h"
void magnetism(universe_t* univ);
#endif // MAGNETISM_H
+156 -63
View File
@@ -1,16 +1,20 @@
#define _POSIX_C_SOURCE 200809L
#include <bits/time.h>
#include "consts.h"
#include "gravity.h"
#include "magnetism.h"
#include "mechanics.h"
#include "objects.h"
#include "vec_utils.h"
#include <errno.h>
#include <math.h>
#include <pthread.h>
#include <raylib.h>
#include <stdio.h>
#include <stdlib.h>
#include <threads.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include "consts.h"
#include "mechanics.h"
int center_x_text(const char *text, int width, int size) {
return (width - MeasureText(text, size)) / 2;
@@ -20,74 +24,76 @@ static inline int center_y_text(int height, int size) {
return (height - size) / 2;
}
long long timespec_diff_ns(struct timespec end, struct timespec start) {
return (end.tv_sec - start.tv_sec) * 1000000000LL +
(end.tv_nsec - start.tv_nsec);
}
void *physics(void *args) {
universe_t *uni = (universe_t *)args;
struct timespec ot, ct;
struct timespec next_step;
clock_gettime(CLOCK_MONOTONIC, &next_step);
while (true) {
clock_gettime(CLOCK_MONOTONIC, &ct);
mechanics(uni);
ot = ct;
long long elapsed = timespec_diff_ns(ct, ot);
long long remaining = TIMESTEP / simx - elapsed;
if (remaining > 0) {
long long sleep_time = remaining * 9 / 10;
struct timespec sleep_time_spec = {0, sleep_time};
thrd_sleep(&sleep_time_spec, NULL);
next_step.tv_sec += LOOP_TIMESTEP / 1000000000LL;
next_step.tv_nsec += LOOP_TIMESTEP % 1000000000LL;
if (next_step.tv_nsec >= 1000000000L) {
next_step.tv_nsec -= 1000000000L;
next_step.tv_sec++;
}
gravity(uni);
magnetism(uni);
mechanics(uni);
struct timespec now;
clock_gettime(CLOCK_MONOTONIC, &now);
if (now.tv_sec > next_step.tv_sec ||
(now.tv_sec == next_step.tv_sec && now.tv_nsec > next_step.tv_nsec)) {
next_step =
now; // An overrun starts a fresh schedule, without catch-up steps.
}
// Sleep only until the deadline; processing time is already included.
while (clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, &next_step, NULL) ==
EINTR) {
}
do {
clock_gettime(CLOCK_MONOTONIC, &ct);
} while (timespec_diff_ns(ct, ot) < TIMESTEP / simx);
}
}
int main(void) {
int main(int argc, char **argv) {
if (argc != 2) {
printf("No config provided\n");
return EXIT_FAILURE;
}
FILE *file = fopen(argv[1], "r");
if (file == NULL) {
printf("File not found");
return EXIT_FAILURE;
}
int cam_lock = -1;
float v_scale = 1;
Vector2 canvas_offset = {0, 0};
pthread_t physics_thread;
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;
}
char line[1024];
fgets(line, 1024, file);
while (fgets(line, 1024, file)) {
printf("%s", line);
float mass, radius, x, y, vx, vy, draw_radius, charge;
char name[32];
sscanf(line, "%f, %f, {%f, %f}, {%f, %f}, %f, %f, %31s", &mass, &radius, &x,
&y, &vx, &vy, &draw_radius, &charge, name);
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;
}
universe.bodies[universe.num_bodies] = (mass_body_t){
.mass = mass,
.radius = radius / EARTH_RADIUS,
.position = (Vector2){x * UA / EARTH_RADIUS, y * UA / EARTH_RADIUS},
.speed = (Vector2){vx / EARTH_RADIUS, vy / EARTH_RADIUS},
.draw_radius = draw_radius / EARTH_RADIUS,
.charge = charge};
unsigned int i = universe.num_bodies++;
universe.bodies[i] = (mass_body_t){
radius,
radius,
{x, y},
{-0.1f, 0.8f - i * 0.005f},
};
memcpy((void *)&universe.bodies[universe.num_bodies].name, (void *)&name,
32);
universe.num_bodies++;
}
fclose(file);
SetConfigFlags(FLAG_WINDOW_RESIZABLE);
InitWindow(800, 450, "Fucked Physics");
SetTargetFPS(120);
@@ -95,17 +101,104 @@ int main(void) {
while (!WindowShouldClose()) {
int key_pressed = 0;
do {
key_pressed = GetKeyPressed();
switch (key_pressed) {
case KEY_I:
v_scale *= 1.26;
break;
case KEY_O:
v_scale /= 1.26;
break;
case KEY_LEFT:
canvas_offset.x += 100;
break;
case KEY_RIGHT:
canvas_offset.x -= 100;
break;
case KEY_UP:
canvas_offset.y += 100;
break;
case KEY_DOWN:
canvas_offset.y -= 100;
break;
case KEY_ONE:
cam_lock = 1;
break;
case KEY_TWO:
cam_lock = 2;
break;
case KEY_THREE:
cam_lock = 3;
break;
case KEY_FOUR:
cam_lock = 4;
break;
case KEY_FIVE:
cam_lock = 5;
break;
case KEY_SIX:
cam_lock = 6;
break;
case KEY_SEVEN:
cam_lock = 7;
break;
case KEY_EIGHT:
cam_lock = 8;
break;
case KEY_NINE:
cam_lock = 9;
break;
case KEY_ZERO:
cam_lock = 0;
break;
case KEY_F:
cam_lock = -1;
}
} while (key_pressed != 0);
if (universe.num_bodies < 1024 &&
(IsMouseButtonPressed(MOUSE_LEFT_BUTTON) ||
IsMouseButtonDown(MOUSE_RIGHT_BUTTON))) {
Vector2 mouse_pos = vec2_scale(
transform_to_center(vec2_scale(GetMousePosition(), -1)), -1);
mouse_pos = vec2_scale(mouse_pos, 1 / v_scale);
universe.bodies[universe.num_bodies] =
(mass_body_t){100000.0f, 0.1, mouse_pos, {0, 0}, 10, 0, ""};
snprintf(universe.bodies[universe.num_bodies].name,
sizeof(universe.bodies[universe.num_bodies].name), "Body %u",
universe.num_bodies + 1);
universe.num_bodies++;
}
BeginDrawing();
ClearBackground(RAYWHITE);
for (unsigned int i = 0; i < universe.num_bodies; i++) {
DrawCircleV(transform_to_center(universe.bodies[i].position),
universe.bodies[i].radius,
(Color){((256 / universe.num_bodies) * i) % 256,
255 * (i % 2), 255 * (i % 2) * (i % 3), 255});
Vector2 local_offset = canvas_offset;
if (cam_lock != -1) {
local_offset = vec2_scale(universe.bodies[cam_lock].position, -1);
}
Vector2 scaled_offset = vec2_scale(local_offset, v_scale);
Vector2 body_position = vec2_scale(universe.bodies[i].position, v_scale);
body_position = vec2_sum(body_position, scaled_offset);
body_position = transform_to_center(body_position);
float draw_radius = universe.bodies[i].draw_radius * v_scale;
if (draw_radius < 2) {
draw_radius = 2;
}
DrawCircleV(body_position, draw_radius,
(Color){((256 / universe.num_bodies) * i) % 256 / 2,
255 * (i % 2) / 2, 255 * (i % 2) * (i % 3) / 2, 255});
// Keep labels readable at every zoom level.
DrawText(universe.bodies[i].name,
(int)(body_position.x + draw_radius + 6),
(int)(body_position.y - 8), 16, BLACK);
}
DrawText(welcome_text, center_x_text(welcome_text, GetRenderWidth(), 20),
center_y_text(GetRenderHeight(), 20), 20, BLACK);
// DrawText(welcome_text, center_x_text(welcome_text, GetRenderWidth(), 20),
// center_y_text(GetRenderHeight(), 20), 20, BLACK);
EndDrawing();
}
+32 -35
View File
@@ -2,9 +2,8 @@
#include "objects.h"
#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;
@@ -16,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;
}
@@ -42,15 +41,15 @@ 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) {
float max_x = GetRenderWidth() / 2.0f - a->radius;
float max_y = GetRenderHeight() / 2.0f - a->radius;
float max_x = GetRenderWidth() * 100 / 2.0f - a->radius;
float max_y = GetRenderHeight() * 100 / 2.0f - a->radius;
if (a->position.x > max_x) {
a->position.x = max_x;
@@ -73,26 +72,24 @@ void collide_rigid(mass_body_t *a, float restitution) {
}
}
void mechanics(universe_t* uni){
mass_body_t body_1;
mass_body_t body_2;
Vector2 collision;
Vector2 scaled_speed;
void mechanics(universe_t *uni) {
// Advance every body to the same simulated time before checking contacts.
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);
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);
}
uni->bodies[y] = body_2;
}
mass_body_t *body = &uni->bodies[i];
body->position = vec2_sum(body->position,
vec2_scale(body->speed, PHYSICS_TIMESTEP));
}
for (unsigned int i = 0; i < uni->num_bodies; i++) {
mass_body_t *body_1 = &uni->bodies[i];
for (unsigned int y = i + 1; y < uni->num_bodies; y++) {
mass_body_t *body_2 = &uni->bodies[y];
Vector2 collision = body_collision(body_1, body_2);
if (!vec2_is_null(collision)) {
collide(body_1, body_2, collision, 0.99);
}
collide_rigid(&body_1, 1.1);
uni->bodies[i] = body_1;
}
if (WALL_COLLISIONS) {
collide_rigid(body_1, 1.0f);
}
}
}
+8 -5
View File
@@ -1,12 +1,15 @@
#ifndef OBJECTS_H_
#define OBJECTS_H_
#include "vec_util.h"
#include "vec_utils.h"
typedef struct {
float mass;
float radius;
Vector2 position;
Vector2 speed;
float mass; // Earth masses.
float radius; // Earth radii (physical collision radius).
Vector2 position; // Earth radii.
Vector2 speed; // Earth radii per simulated second.
float draw_radius;
float charge; // Coulombs.
char name[32];
} mass_body_t;
typedef struct universe {
+4
View File
@@ -0,0 +1,4 @@
mass(EARTH_MASS), radius(km), {x(UA), y(UA)}, {vx(km/s), vy(km/s)}, draw_radius(km), charge(C), name
396518.4, 760000, {-0.85, 0}, {0, 45.0}, 220000, 0, Alpha
297388.8, 650000, {-0.35, 0}, {0, -37.0}, 200000, 0, Beta
495648.0, 900000, {1.30, 0}, {0, -14.5}, 250000, 0, Gamma
-21
View File
@@ -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_
+9 -9
View File
@@ -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
View File
@@ -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_