/elte-ik-gpgpu-rigid-body-simulation

GPU accelerated rigid body simulation with OpenGL and OpenCL.

Primary LanguageC++MIT LicenseMIT

GPU accelerated rigid body simulation with OpenGL and OpenCL.

Rigid Body Simulation (Click the image for the video) (Video URL: https://youtu.be/ky2dFqLJdGQ)

The main features:

  • Real time simulation with OpenGL (SDL2) wiev.
  • Real time Ball to Wall, Ball to Box, Ball to Ball collision and refelction.
  • GPU Acceleration with OpenCL.
  • Live switch between the CPU and GPU implementation.
  • GUI and live parametering
  • Live camera interaction (Sphere camera rotation)
  • Live mouse interaction.

The envirement:

  • The code is platform free and it use free multiplatform libs, but i developed and tested in Winddows.
  • This is a Visual studio project (Made with Visual Studio 2019).
  • The project need SDL2 OpemCL GLEW IMGUI for compiling.
  • This zip file contains the nececcery dependecies. You can mount the dependencies in a T drive with the script in the zip file.
  • (https://github.com/SandorBalazsHU/elte-ik-gpgpu-rigid-body-simulation/blob/master/dependencies.zip)
  • The Visual studio project configured for this dependecy pack and the T virtual Drive.

The GUI:

  • You can rotate the camera with the mouse, when you hold the left mouse button.
  • You can move the small box with the mouse, when you hold the right mouse button.
  • You can zoom in and out with the mouse wheel.
  • The main GUI in the left top corner. (The window is replaceable and resizable.)
  • Here you can see the FPS meter.
  • ---- All setting is live ----
  • Then you can set the gravity vector.
  • Then you can set the air resistance.
  • Then you can set the simulation box size.
  • Then you can set the balls number. [1-1500]
  • Then you can apply random XZ and Y ball start position. (Works after reset)
  • The most importent is the RUN switch. Here you can START/STOP the simulation.
  • Then you can switch on/off the ball to ball collision.
  • Then you can reinitialise the simulation. (RESET)
  • Then you can switch on/off the small box.

The virtual folders:

  • I made virual folders in Visual Studio for the better transparency.
  • I used CamelCased names and English comments.

The Theoretical background:

I stored the balls positions in float3 and their velocities in an another float3 vector. The new position is the position vector and the velocity vectors sum. I modelld the gravity vith an another float3 vector and i subtract the velocitiy vector with the gravity vector. I add an air resistance vector when slow down.

    glm::vec3 positions[numberOfBalls];
    glm::vec3 velocities[numberOfBalls];
    float resistance;

    velocities[i] = velocities[i] - gravity;
    velocities[i] = velocities[i] * resistance;
    positions[i]  = positions[i] + velocities[i];
    if (ballCollisionRun) ballCollision(i);
    if(barrierIsOn) barrierCollision(i);
    wallCollision(i);

The processing loop

The Ball to Wall collision:

The ball to Ball Collision (The first way, i used this for GPU):

The ball to Ball Collision (The second way, i used this for CPU):

Used sources:

Issues

  • When you set 1500 balls the GPU ball to ball collision sometimes becomes unstable (In this case you can switch to CPU and Back.)
  • With 1500 balls the average FPS is 20-30.