I'm Coding an Entire Physics Engine from Scratch

Gonkee
9 Oct 202109:19
EducationalLearning
32 Likes 10 Comments

TLDRIn this engaging video, the creator tackles the daunting task of building a 3D physics engine from scratch during lockdown as a mental escape. With no prior experience in 3D physics, they embark on a journey through coding with C# and OpenGL, learning and experimenting with various sphere mesh types. The video documents the process of implementing soft body physics, using principles from Hooke's law and the ideal gas law to create a bouncy ball simulation. Despite the challenges and imperfections, the creator shares their progress and invites viewers to check out the GitHub repository for 'Geep3D', promising to address issues and possibly explore fluid simulations in future videos.

Takeaways
  • πŸ”’ The video is about a lockdown project where the creator decides to build a 3D physics engine as a distraction.
  • πŸ› οΈ The creator has previous experience with building a graphics library but is new to 3D physics.
  • 🎨 The project aims to include various simulations such as soft body, fluid, cloth, and rigid body dynamics.
  • πŸ’» The chosen programming languages and tools for the project are C# and OpenGL.
  • πŸ“ˆ The creator had to learn C# and OpenGL from scratch, which was a challenging and time-consuming process.
  • 🌐 The initial focus was on creating a sphere mesh, with the Ico sphere being chosen for its even vertex distribution and triangular faces.
  • πŸ€” Physics principles from high school textbooks, like Hooke's Law and Newton's laws, were used to create the soft body dynamics.
  • πŸ”„ The video describes the process of implementing a spring-mass system and using the ideal gas law to inflate the ball.
  • πŸ‘¨β€πŸ« The creator struggled with the explicit Euler method for physics simulations and eventually switched to the Runge-Kutta 4 method.
  • πŸ”§ Collision detection was a painful process, with the creator sharing their experience and the resulting code.
  • πŸš€ The project, named 'geep3d' for Gonki's Epic Physics Engine, is available on GitHub for others to view and contribute to.
Q & A
  • What is the main challenge the speaker is undertaking during lockdown?

    -The speaker is undertaking the challenge of creating a 3D physics engine as a means of distraction during lockdown.

  • What previous experience does the speaker have with coding libraries?

    -The speaker has previously created a graphics library, which took months of work, and has received comments asking about the animations made with it.

  • Why did the speaker choose to use C# and OpenGL for the project?

    -The speaker chose C# and OpenGL because they have never used C# before and have not done anything 3D in OpenGL, making it a new and challenging learning experience.

  • What type of simulations does the speaker plan to include in the physics engine?

    -The speaker plans to include various simulations such as soft body, fluid, cloth, and rigid body simulations in the physics engine.

  • What are the issues with the UV sphere mesh according to the speaker?

    -The UV sphere mesh has uneven distribution of points with many vertices on the top and bottom and fewer in the middle, and it contains quadrilaterals which are not preferred in computer graphics.

  • What is the advantage of the ico sphere over the UV sphere and subdivided cube?

    -The ico sphere is advantageous because it is made entirely of triangles, has a completely symmetrical structure, and evenly distributes vertices, making it ideal for 3D modeling.

  • What physics concept does the speaker use to prevent the ball from exploding?

    -The speaker uses Hooke's law in a spring mass system to maintain the original length of each edge and prevent the ball from exploding.

  • How does the speaker address the issue of inflating the ball in the physics engine?

    -The speaker uses the ideal gas law to calculate the pressure inside the ball and apply a force proportional to the area of each triangle on the ball, pushing the points outward.

  • What numerical method does the speaker initially use for simulating motion in the physics engine?

    -The speaker initially uses the explicit Euler method for simulating motion in the physics engine.

  • Why does the speaker switch from the explicit Euler method to Runge-Kutta 4?

    -The speaker switches to Runge-Kutta 4 because the explicit Euler method is less accurate, especially for the spring mass system, and Runge-Kutta 4 provides a more sophisticated and accurate solution.

  • What difficulties does the speaker encounter with collision detection in the physics engine?

    -The speaker encounters significant difficulties with collision detection, which is as painful as expected, and requires a lot of time and effort to implement correctly.

  • How does the speaker share their experience and code with the community?

    -The speaker shares their experience and code through a GitHub repository called 'geep3d' for Gonki's Epic Physics Engine, which is linked in the video description.

Outlines
00:00
😎 Taking on a 3D Physics Engine Challenge

The speaker, in a lockdown with nothing to do, decides to tackle a complex coding challenge: creating a full 3D physics engine. Despite having no experience in 3D physics, they have previously developed a graphics library, which they now consider improving. They plan to include various simulations like soft body, fluid, cloth, and rigid body dynamics in their engine. The speaker opts to use C# and OpenGL, languages and libraries they've never worked with before, and humorously documents their steep learning curve. After two weeks, they have a basic rendering setup, albeit with poor performance and aesthetics, and begin exploring sphere mesh generation for soft body simulation, eventually choosing the ico sphere for its triangular symmetry and even vertex distribution.

05:01
πŸ€” Implementing Physics for a Bouncy Ball

The speaker delves into the physics of their 3D engine, starting with the creation of a bouncy ball. They discuss the use of Hooke's law for spring forces to maintain the ball's shape and Newton's second law for applying mass to each point on the ball, affecting its movement. To give the ball its bouncy characteristic, they apply the ideal gas law to pressurize it, using the relationship between pressure, volume, and force to calculate the outward force on the ball's surface. The implementation is described as challenging, taking days to complete and resulting in a less-than-perfect code. The speaker also mentions the limitations of the explicit Euler method for simulating motion and the switch to the more accurate Runge-Kutta 4 method. The paragraph concludes with a brief mention of collision detection, which is acknowledged as a difficult task, and ends with a cliffhanger as the speaker's soft body simulation struggles to clear gaps, hinting at unresolved issues and future improvements.

Mindmap
Keywords
πŸ’‘Lockdown
Lockdown refers to a state of restricted movement or activity imposed by authorities in response to a crisis, such as a pandemic. In the video, the creator mentions the lockdown as the reason for undertaking the coding challenge, indicating a desire to stay productive and engaged during a period of isolation.
πŸ’‘3D Physics Engine
A 3D Physics Engine is a software system that simulates the physical behavior of objects in a three-dimensional environment. It's central to the video's theme, as the creator aims to develop one from scratch, showcasing the complexity of the task and the learning process involved.
πŸ’‘Graphics Library
A Graphics Library is a collection of software routines that assist in the rendering of 2D or 3D images. The creator mentions having previously developed a graphics library, which is foundational knowledge for tackling the 3D physics engine project.
πŸ’‘Soft Body Simulations
Soft Body Simulations refer to the computational process of simulating the behavior of flexible and deformable objects. The video creator plans to include this in the physics engine, indicating the ambition to tackle advanced and realistic physical phenomena.
πŸ’‘Fluid Simulations
Fluid Simulations are algorithms that mimic the flow of liquids and gases in a virtual environment. The script mentions the intention to include fluid dynamics in the engine, highlighting the creator's goal to cover a wide range of physical simulations.
πŸ’‘C Sharp
C Sharp, often stylized as C#, is a programming language developed by Microsoft. The creator chooses C# for the project, despite having no prior experience, which underscores the learning challenge and the video's educational aspect.
πŸ’‘OpenGL
OpenGL is a cross-platform API for rendering 2D and 3D vector graphics. The script mentions using OpenGL for the 3D aspects of the project, emphasizing the technical skills the creator is acquiring.
πŸ’‘Sphere Meshes
Sphere Meshes are geometric representations of a sphere used in 3D graphics. The video discusses different types of sphere meshes, such as UV spheres and ico spheres, which are essential for creating realistic 3D objects in the physics engine.
πŸ’‘Spring Mass System
A Spring Mass System is a model that uses Hooke's Law to simulate the behavior of springs connecting masses. The creator applies this concept to the physics engine to maintain the structural integrity of soft body objects, like the bouncing ball.
πŸ’‘Ideal Gas Law
The Ideal Gas Law (PV=nRT) is a principle in physics that describes the behavior of an ideal gas. The video creator uses a simplified version of this law to pressurize the soft body objects, adding to the realism of the simulation.
πŸ’‘Explicit Euler Method
The Explicit Euler Method is a numerical technique for solving differential equations. The creator initially uses this method for simulating motion but later finds its limitations, leading to the adoption of a more advanced method, Runge-Kutta 4.
πŸ’‘Runge-Kutta 4
Runge-Kutta 4 is a more sophisticated numerical method for solving differential equations, offering greater accuracy than the Explicit Euler Method. The script describes the transition to this method as a significant challenge in the development process.
πŸ’‘Collision Detection
Collision Detection is the process of determining when two or more objects in a simulation come into contact with each other. The video script describes the painstaking effort involved in implementing collision detection for the physics engine.
Highlights

Lockdown boredom leads to taking on the challenge of creating a 3D physics engine.

Previous experience with building a graphics library inspires a new project.

The decision to build the engine with C# and OpenGL despite having no prior experience.

Two weeks of learning and initial results with C# and OpenGL, despite poor performance and aesthetics.

Introduction to different types of sphere meshes for 3D modeling.

The choice of the ico sphere for its even vertex distribution and triangular symmetry.

Understanding the algorithm to generate an ico sphere through GitHub code.

The physics of the 3D engine starts with a simple collision model of a bouncing ball on a floor.

Incorporating Hooke's Law for spring mass system to maintain the shape of the ball.

Assigning mass to each point on the ball to simulate movement based on weight.

Using the ideal gas law to pressurize the ball and give it a bouncy effect.

Days of implementation to achieve a bouncy ball effect in the physics engine.

The limitations and inaccuracies of the explicit Euler method for physics simulations.

Transitioning to Runga Kutta 4 for more accurate physics calculations.

The struggle and learning curve of implementing Runga Kutta 4 in the physics engine.

The project's scalability and the decision to move on to collision detection.

The painstaking process of developing collision detection for the physics engine.

A demonstration of the soft body physics engine with a ball navigating gaps.

The challenges faced in fine-tuning values for collision and maintaining the engine's stability.

Invitation to the audience to view and contribute to the project on GitHub.

A humorous conclusion with the creator's intention to fix issues in a future video.

Transcripts
Rate This

5.0 / 5 (0 votes)

Thanks for rating: