summaryrefslogtreecommitdiff
path: root/src/main.c
blob: ff2044f40c3e2d4175bd7ae2e699b6dc4d81a283 (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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
#include <stdlib.h>
#include <stdio.h>

#include "raylib.h"
#include "raymath.h"


typedef struct Config {
    int SCREEN_WIDTH;
    int SCREEN_HEIGHT;
    int ORIGIN_X;
    int ORIGIN_Y;
    char TITLE[50];
    int TARGET_FPS;
} Config;

typedef struct Line {
    Vector2 start;
    Vector2 end;
    Color color;
} Line;

typedef struct Object {
    float posX;
    float posY;
    float radius;
    Color color;
    float charge;
} Object;

typedef struct VectorField {
    int colSize;
    int rowSize;
    int spacing;
    Line **lines;
} VectorField;

void Initialize(Config config);
void InitializeVectorField(VectorField *vectorField, int colSize, int rowSize);
void doDrawing(VectorField *vectorField, int colSize, int rowSize, Object *objects, int objectNum);
void doUpdate(VectorField *vectorField, Object *objects, int objectNum);

int main(void)
{
    Config config = {
            1500,
            900,
            1500 / 2,
            900 / 2,
            "Force Field Simulator",
            60
    };
    Initialize(config);

    int spacing = 20;
    int colSize = config.SCREEN_WIDTH / spacing - 1;
    int rowSize = config.SCREEN_HEIGHT / spacing - 1;
    Line **lines = NULL;
    VectorField vectorField = {
            .colSize = colSize,
            .rowSize = rowSize,
            .spacing = spacing,
            .lines = lines
    };
    InitializeVectorField(&vectorField, colSize, rowSize);

    Object object0 = {
            .posX = (float)config.ORIGIN_X - 200,
            .posY = (float)config.ORIGIN_Y - 50,
            .radius = 20,
            .color = RED,
            .charge = 100
    };

    Object object1 = {
            .posX = (float)config.ORIGIN_X + 200,
            .posY = (float)config.ORIGIN_Y + 50,
            .radius = 20,
            .color = BLUE,
            .charge = -60
    };

//    Object object2 = {
//            .posX = (float)config.ORIGIN_X + 200,
//            .posY = (float)config.ORIGIN_Y - 100,
//            .radius = 20,
//            .color = BLUE,
//            .charge = -100
//    };

    Object objects[] = {object0, object1};
    int objectNum = sizeof(objects) / sizeof(Object);

    while (!WindowShouldClose())
    {
        doUpdate(&vectorField, objects, objectNum);
        doDrawing(&vectorField, colSize, rowSize, objects, objectNum);
    }

    CloseWindow();

    return 0;
}

void Initialize(Config config) {
    InitWindow(config.SCREEN_WIDTH, config.SCREEN_HEIGHT, config.TITLE);
    SetTargetFPS(config.TARGET_FPS);
}

void MemAllocFailed(char *source) {
    printf("Memory allocation for %s failed", source);
    exit(42);
}

//float getRndAngle() {
//    float nMax = 360;
//    return (float)rand() / ((float)RAND_MAX / nMax); // NOLINT(cert-msc30-c, cert-msc50-cpp)
//}

void InitializeVectorField(VectorField *vectorField, int colSize, int rowSize) {
    vectorField->lines = malloc(rowSize * sizeof(Line*));
    for (int i = 0; i < rowSize; i++) {
        vectorField->lines[i] = malloc(colSize * sizeof(Line));
        for (int j = 0; j < colSize; j++) {
            Vector2 start = {
                    (float)((j + 1) * vectorField->spacing),
                    (float)((i + 1) * vectorField->spacing)
            };
            Vector2 end = {start.x + 5, start.y - 5};
            Line line = {
                    .start = start,
                    .end = end,
                    .color = WHITE
            };
            vectorField->lines[i][j] = line;
        }
    }
}

void doUpdate(VectorField *vectorField, Object *objects, int objectNum) {
    float forceConstant = 2000.0f;
    float dt = GetFrameTime();
    for (int i = 0; i < vectorField->rowSize; i++) {
        for (int j = 0; j < vectorField->colSize; j++) {
            Vector2 resultant = Vector2Zero();
            for (int k = 0; k < objectNum; k++) {
                Object object = objects[k];
                Vector2 distanceVector = Vector2Subtract(vectorField->lines[i][j].start, (Vector2){object.posX, object.posY});
                float fieldStrength = forceConstant * object.charge / Vector2LengthSqr(distanceVector);
                Vector2 force = Vector2Normalize(distanceVector);
                force = Vector2Scale(force, fieldStrength);
                resultant = Vector2Add(force, resultant);
            }
            float resultantMag = Clamp(Vector2Length(resultant), -20.0f, 20.0f);
            if (fabsf(resultantMag) < 2.0f) {
                resultantMag = resultantMag >= 0 ? 2.0f : -2.0f;
            }
            resultant = Vector2Normalize(resultant);
            resultant = Vector2Scale(resultant, resultantMag);
            vectorField->lines[i][j].end = Vector2Add(vectorField->lines[i][j].start, resultant);
            float lineLength = Vector2Length(Vector2Subtract(vectorField->lines[i][j].start, vectorField->lines[i][j].end));
            Color color;
            if (lineLength >= 16) color = RED;
            else if (lineLength >= 12) color = ORANGE;
            else if (lineLength >= 8) color = YELLOW;
            else if (lineLength >= 4) color = GREEN;
            else color = GRAY;
            vectorField->lines[i][j].color = color;
        }
    }
    float speed = 20;
    objects[0].posX += speed * dt;
    objects[1].posX -= speed * dt;
}

void doDrawing(VectorField *vectorField, int colSize, int rowSize, Object *objects, int objectNum) {
    BeginDrawing();

    ClearBackground(BLACK);

    for (int i = 0; i < rowSize; i++) {
        for (int j = 0; j < colSize; j++) {
            DrawLineV(vectorField->lines[i][j].start, vectorField->lines[i][j].end, vectorField->lines[i][j].color);
        }
    }
    for (int i = 0; i < objectNum; i++) {
        Object object = objects[i];
        DrawCircle((int)object.posX, (int)object.posY, object.radius, object.color);
    }

    EndDrawing();
}