summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRosyid Haryadi <rosyid_haryadi@protonmail.com>2023-12-13 01:20:18 +0700
committerRosyid Haryadi <rosyid_haryadi@protonmail.com>2023-12-13 01:20:18 +0700
commitdddd49ca24fe0f0f6ee5b86f7dc2131a53e7e6ee (patch)
treeab21ec5d3cb70e89950016913e9fdd22b3e1408f
parentccdb9e90612bbaa0bb6357b56c0c95083f6104d6 (diff)
template
-rw-r--r--src/main.c106
1 files changed, 6 insertions, 100 deletions
diff --git a/src/main.c b/src/main.c
index 519dbfc..e05d3e1 100644
--- a/src/main.c
+++ b/src/main.c
@@ -9,22 +9,9 @@ typedef struct InitConfig {
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);
+void doDrawing();
+void doUpdate();
int main(void)
{
@@ -36,43 +23,10 @@ int main(void)
};
doInitialization(config);
- Physics physics = {
- .gravityConstant = 70.0f,
- };
-
- Ball luna = {
- .pos = {(float)config.SCREEN_WIDTH / 2, 0.0f + 100},
- .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 - 320},
- .radius = 5,
- .mass = 200,
- .velocity = { -80.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 = 15000,
- .velocity = { 0.0f, 0.0f },
- .force = {0.0f, 0.0f},
- BLUE
- };
-
- Ball *balls[] = {&earth, &luna, &potato};
-
while (!WindowShouldClose())
{
- doUpdate(balls, 3, physics);
- doDrawing(balls, 3);
+ doUpdate();
+ doDrawing();
}
CloseWindow();
@@ -85,62 +39,14 @@ void doInitialization(InitConfig config) {
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);
-}
-
-Vector2 computeGravityNDimension(Ball *ball1, Ball *ball2, Physics physics, int dimension) {
- // Generalisasi. Nyoba kalo ada N-dimensi ruang
- Vector2 displacement = Vector2Subtract(ball1->pos, ball2->pos);
- float distance = Vector2Length(displacement);
- float distanceFactor = (float)pow(distance, dimension - 1);
- float gravityMag = (physics.gravityConstant * ball1->mass * ball2->mass) / distanceFactor;
- return Vector2Scale(Vector2Normalize(displacement), gravityMag);
-}
-
-void doUpdate(Ball **balls, int totalBalls, Physics physics) {
+void doUpdate() {
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) {
+void doDrawing() {
BeginDrawing();
ClearBackground(BLACK);
- for (int i = 0; i < totalBalls; i++) {
- Ball *ball = balls[i];
- DrawCircleV(ball->pos, ball->radius, ball->color);
- }
-
EndDrawing();
} \ No newline at end of file