(Click the image for the video) (Video URL: https://youtu.be/ky2dFqLJdGQ)
- 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 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.
- 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.
- I made virual folders in Visual Studio for the better transparency.
- I used CamelCased names and English comments.
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);
- Ball to wall reflection: https://math.stackexchange.com/questions/13261/how-to-get-a-reflection-vector
- Ball to wall collision (Way 1): http://exploratoria.github.io/exhibits/mechanics/elastic-collisions-in-3d/
- Ball to wall collision (Way 2): https://en.wikipedia.org/wiki/Elastic_collision
- Phisical simulations: https://www.khanacademy.org/science/physics/linear-momentum/elastic-and-inelastic-collisions/a/what-are-elastic-and-inelastic-collisions
- 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.