Back to Projects
ProjectDecember 30, 2024

Simple Physics Simulation: Humble Beginnings

The first part of a basic physics simulation developed in C. It models a particle subjected to gravitational forces aiming to model many general forces.

New ProjectPhysicsC

Introduction

As I am continuing my C endeavors, I have started working on a simple physics simulation. The main idea of the project is to simulate particles; as I want this project to simulate what happens in real life when you suspend some particles in the air and then letting go of them and observing what happens - the collisions between the particles and the walls etc.


The Project So Far

The project currently only displays a rectangle on the screen. Which then a simple algorithm generates a circle from the square.

Drawing the circle

Since there is no built-in method to draw a circle, I need to implement an algorithm that draws a circle based on a rectangle. This algorithm utilizes the simple circle parameter formula: x^2 + y^2 < radius^2. I have a data structure that represents a circle, which holds the following properties: x and y (the width and length, respectively), the center coordinates cx and cy, and the radius r.

Acceleration

To calculate the acceleration of an object, the change in time between the current and previous frames, known as "deltatime," is required. First, I determine the difference in time between the current and previous frames. Then, I have a velocity float variable that I update by adding the acceleration multiplied by the deltatime.

For the acceleration value, I have hardcoded it to 9.81, which is approximately the acceleration due to gravity in the real world. I'm not sure why I chose that specific value.

Next, the draw circle function is called, and a circle struct is initialized. All the properties of the circle are hardcoded, except for the coordinates of the center, which are set based on the position of the particle.

If the particle is suspended in the air (i.e., the coordinates of the circle's center are within the boundaries), I apply gravity to the velocity. However, if the distance between the center of the particle and the boundary is less than or equal to the radius of the circle, I set the velocity to 0.

Display

Clearing the frame before rendering the next step of the simulation is a straightforward process. Additionally, I introduce a 16ms delay, which corresponds to a frame rate of 60 fps.

Keyboard Input

The code block I've developed tracks the mouse position and, when the space key is pressed, teleports a particle to that location. Upon releasing the space key, the simulation resumes its normal operation. Additionally, pressing the 'q' key will terminate the entire simulation.


Problems

The problems outlined below will limit the scalability of the current system:

The code is very messy, with most of it concentrated in the main event loop. This makes the codebase difficult to maintain and expand.

I want to be able to programmatically create and manage multiple particles, but the current implementation does not support this well.

The collision detection is implemented as a series of if-statements in the main loop. This approach will not be efficient enough to handle the desired 100 particles, and will require optimization.


Improvements

These enhancements are solely focused on optimizing performance and enabling scalability.

  1. Dynamic Particle Management: Use an array or a dynamic list struct Circle circles[] to store and update multiple particles.
  2. Collision Detection: Implement collision detection between particles using their positions and radii.
  3. Physics Enhancements: Add properties like individual velocity, mass, and elasticity to each circle to simulate realistic physics, including bouncing and momentum transfer.
  4. Code Optimization: Replace the manual pixel drawing in FillCircle with a more efficient method, such as a pre-rendered texture or optimized algorithms.
  5. Boundary Handling: Add more realistic boundary reactions (e.g., energy loss upon collision, sliding along walls).
  6. Debugging and Visual Feedback: Display particle properties like position, velocity, or acceleration as text on the screen.

Before I Go

Let's not forget our brothers and sisters in Gaza. Please consider making a donation today to provide them with the support and aid they desperately need. Your generosity can make a real difference in their lives. Donate at PCRF (Palestine Children Relief Fund). Anything helps.