summaryrefslogtreecommitdiff
path: root/src/main.c
blob: 99bb89828c261f5e3d0a396b0f0ee92cd2b84563 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#include "raylib.h"
#include "raymath.h"


typedef struct InitConfig {
    int SCREEN_WIDTH;
    int SCREEN_HEIGHT;
    char TITLE[20];
    int TARGET_FPS;
} InitConfig;

typedef struct Ball {
    Vector2 pos;
    float radius;
    float mass;
    Vector2 velocity;
    Vector2 force;
    Color color;
} Ball;

typedef struct Physics {
    float gravityConstant;
} Physics;

void doInitialization(InitConfig config);
void doDrawing(Ball **balls, int totalBalls);
void doUpdate(Ball **balls, int totalBalls, Physics physics);

int main(void)
{
    InitConfig config = {
            800,
            500,
            "Just a fucking test",
            60
    };
    doInitialization(config);

    Physics physics = {
            .gravityConstant = 50.0f,
    };

    Ball luna = {
            .pos = {(float)config.SCREEN_WIDTH / 2, 0.0f + 60},
            .radius = 5,
            .mass = 200,
            .velocity = { 50.0f, 0.0f },
            .force = {0.0f, 0.0f},
            WHITE
    };

    Ball potato = {
            .pos = {(float)config.SCREEN_WIDTH / 2, (float)config.SCREEN_HEIGHT - 80},
            .radius = 5,
            .mass = 200,
            .velocity = { -55.0f, 0.0f },
            .force = {0.0f, 0.0f},
            YELLOW
    };

    Ball earth = {
            .pos = {(float)config.SCREEN_WIDTH / 2, (float)config.SCREEN_HEIGHT / 2 },
            .radius = 20,
            .mass = 10000,
            .velocity = { 0.0f, 0.0f },
            .force = {0.0f, 0.0f},
            BLUE
    };

    Ball *balls[] = {&earth, &luna};

    while (!WindowShouldClose())
    {
        doUpdate(balls, 2, physics);
        doDrawing(balls, 2);
    }

    CloseWindow();

    return 0;
}

void doInitialization(InitConfig config) {
    InitWindow(config.SCREEN_WIDTH, config.SCREEN_HEIGHT, config.TITLE);
    SetTargetFPS(config.TARGET_FPS);
}

//float pix2m(int pixels) {
//    // Pixel to meter conversion, 1 pixel = 1 cm
//    return 0.01f * (float)pixels;
//}
//
//int m2pix(float metres) {
//    return (int)floorf(metres / 0.01f);
//}

Vector2 computeGravity(Ball *ball1, Ball *ball2, Physics physics) {
    // Gravity felt by ball2
    Vector2 displacement = Vector2Subtract(ball1->pos, ball2->pos);
    float gravityMag = (physics.gravityConstant * ball1->mass * ball2->mass) / Vector2LengthSqr(displacement);
    return Vector2Scale(Vector2Normalize(displacement), gravityMag);
}

void doUpdate(Ball **balls, int totalBalls, Physics physics) {
    float dt = GetFrameTime();
    for (int i = 0; i < totalBalls; i++) {
        Ball *ball = balls[i];
        // Sum over gravity felt from all other balls
        Vector2 gravityTotal = {0.0f, 0.0f};
        for (int j = 0; j < totalBalls; j++) {
            if (i != j) {
                Ball *otherBall = balls[j];
                Vector2 gravity = computeGravity(otherBall, ball, physics);
                gravityTotal = Vector2Add(gravityTotal, gravity);
            }
        }
        // Update position from last velocity
        ball->pos = Vector2Add(ball->pos, Vector2Scale(ball->velocity, dt));
        // a = F / m
        Vector2 acceleration = Vector2Scale(gravityTotal, 1.0f / ball->mass);
        // Update velocity for next frame
        ball->velocity = Vector2Add(ball->velocity, Vector2Scale(acceleration, dt));
    }
}

void doDrawing(Ball **balls, int totalBalls) {
    BeginDrawing();

    ClearBackground(BLACK);

    for (int i = 0; i < totalBalls; i++) {
        Ball *ball = balls[i];
        DrawCircleV(ball->pos, ball->radius, ball->color);
    }

    EndDrawing();
}