r/cprogramming • • 39m ago

Coding a satisfying animation in C / Raylib

https://youtu.be/9f1FRngwWac

Tons of YouTube channels are making really nice physics animations. Most of them look quite similar, and I suppose everyone's vibecoding them. Anyways, vibecoding or not, I love these animations, and I want to learn how to code them myself.

The problem is that most of these channels don't really share the implementation details.

Well... in this video, I'm building one from scratch. So, if you want to learn how to program these kinds of animations, I'm providing the clean source code and the full coding session so you can follow along.

AI Disclaimer:
I didn't use AI

Full project:
https://github.com/FractalCodeRicardo/dopamine-programming/tree/main/squares-balls

Code:

#include <raylib.h>
#include <raymath.h>
#include <stdbool.h>
#include "time.h"

#define screenSize 800.0
#define gridSize  25.0

typedef struct{ 
  Vector2 pos;
  Vector2 dir;
  Color color;
} Ball;

typedef struct{ 
  Vector2 pos;
  Color color;
  bool empty;
  int life;
} Square;

typedef struct{ 
  Ball items[2500];
  int size;
} Balls;

typedef struct{ 
  Square items[2500];
  int size;
} Squares;

Squares squares;
Balls balls;

float squareSize;
float ballSize;

Sound sound;

Color RandomColor() {
  Color colors[] = {
    GREEN, YELLOW, RED, BLUE, PURPLE,
    ORANGE, BROWN, SKYBLUE, DARKPURPLE, MAGENTA
  };

  int index = GetRandomValue(0, 9);
  return colors[index];
}

Vector2 RandomDir() {
  float sign[] = {1 , -1};

  float x = GetRandomValue(5, 10) / 10.0;
  float y = GetRandomValue(5, 10) / 10.0;
  
  x = x * sign[GetRandomValue(0, 1)];
  y = y * sign[GetRandomValue(0, 1)];

  return (Vector2) {
    x, y
  };
}

void InitSquares() {
  squareSize = screenSize / gridSize;
  
  int center = gridSize / 2;
  for(int y = 0; y < gridSize; y++) {
    for(int x = 0; x < gridSize; x++) {

      bool empty = false;
      if (x >= center - 2 && x <= center + 2)
        if (y >= center - 2 && y <= center + 2)
          empty = true;


      int i = y * gridSize + x;

      squares.items[i] = (Square) {
        (Vector2) { 
          x * squareSize,
          y * squareSize
        } ,
        RandomColor(),
        empty,
        5
      };

      squares.size++;
    }
  }
}

void InitBalls() {
  ballSize = (screenSize / gridSize) / 2;
  
  float center = screenSize / 2;
  for(int i = 0; i < 4; i++) {
    balls.items[i] = (Ball) {
      (Vector2) { center, center} ,
      RandomDir(),
      RandomColor()
    };

    balls.size++;
  }

}

void Init() {
  InitSquares();
  InitBalls();
}

void DrawSquares() {
  for (int i = 0; i <squares.size; i++) {
    Square s = squares.items[i];

    if (s.empty)
      continue;
    
    DrawRectangle(
        s.pos.x + 1,
        s.pos.y + 1,
        squareSize - 1, 
        squareSize - 1, 
        s.color);
  }
}

void DrawBalls() {
  for (int i = 0; i <balls.size; i++) {
    Ball b = balls.items[i];
    
    DrawCircle(
        b.pos.x ,
        b.pos.y,
        ballSize / 2,
        b.color
        );
  }
}

void Draw() {
  DrawSquares();
  DrawBalls();
}

void ChangeSquareColor(Square *s) {
  s -> color.a = s -> color.a - 10;
}

void AddBall(Vector2 pos) {
  if (balls.size >= 1000)
    return;

  balls.items[balls.size] = (Ball) {
    pos ,
    RandomDir(),
    RandomColor()
  };

  balls.size ++;
}

void HandleCollision(Ball *b) {

  Rectangle ballRect = (Rectangle) {
    b -> pos.x - ballSize/2,
    b -> pos.y - ballSize/2,
    ballSize,
    ballSize,
  };

  for(int i = 0; i < squares.size; i++) {
    Square *s = &squares.items[i];

    if (s -> empty)
      continue;

    Rectangle squareRect = (Rectangle) {
      s -> pos.x,
      s -> pos.y,
      squareSize,
      squareSize,
    };

    Rectangle collision = GetCollisionRec(ballRect, squareRect);

    if (collision.width <= 0 ||
        collision.height <= 0)
      continue;

    if (collision.width < collision.height)
      b -> dir.x *= -1;
    else
      b -> dir.y *= -1;

    s -> life--;
    ChangeSquareColor(s);

    if (s -> life <= 0) {
      s -> empty = true;
      AddBall(b -> pos);
    }

    PlaySound(sound);

    return;

  }
}

void MoveBalls() {
  for(int i = 0; i < balls.size; i++) {
    Ball *b = &balls.items[i];
    b -> pos = Vector2Add(b -> pos, b -> dir);
    HandleCollision(b);
  }
}

void Update() {
  MoveBalls();
}

int main() {
  SetRandomSeed(time(NULL));
  Init();
  SetTargetFPS(200);
  InitAudioDevice();
  sound = LoadSound("jump.wav");
  SetSoundVolume(sound, 0.1f);
  InitWindow(screenSize, screenSize, "Squares/Balls");

  while(!WindowShouldClose()) {
    BeginDrawing();
    ClearBackground(BLACK);
    Draw();
    Update();
    EndDrawing();
  }
  CloseWindow();
}
1 Upvotes

0 comments sorted by