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
|
#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 {
Vector2 gravityVector;
float dampeningFactor;
} 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 = {
.gravityVector = (Vector2){0.0f, 6.0f},
.dampeningFactor = 0.8f
};
Ball greenBall = {
.pos = {(float)config.SCREEN_WIDTH / 2, 0.0f + 20},
.radius = 20,
.mass = 1,
.velocity = { 0.0f, 0.0f },
.force = physics.gravityVector,
GREEN
};
Ball *balls[] = {&greenBall};
while (!WindowShouldClose())
{
doUpdate(balls, 1, physics);
doDrawing(balls, 1);
}
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);
//}
void doUpdate(Ball **balls, int totalBalls, Physics physics) {
float dt = GetFrameTime();
for (int i = 0; i < totalBalls; i++) {
Ball *ball = balls[i];
// Update position from last velocity
ball->pos = Vector2Add(ball->pos, Vector2Scale(ball->velocity, dt));
// Update velocity for next frame
ball->velocity = Vector2Add(ball->velocity, Vector2Scale(physics.gravityVector, 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();
}
|