Physics Engine from Scratch

Поділитися
Вставка
  • Опубліковано 28 гру 2024

КОМЕНТАРІ •

  • @MarcusOttosson
    @MarcusOttosson 10 місяців тому +31

    Fabulous! To not only be able to make a physics engine, but to present it well, that's a rare combination of brains and beauty. Very well done.

  • @eduardo98m
    @eduardo98m 9 місяців тому +8

    Superb work!
    For anyone looking to delve more into the physics check out the paper: Detailed Rigid Body Simulation with Extended Position Based Dynamics.
    I implemented some of the features described in the paper and I must admit is trickier than it seems. For you to have implemented all of that is incredible.

  • @loukitmyname
    @loukitmyname 6 місяців тому +13

    As someone from engineering background, I am a sucker for physics engines. Thank you for this video.

  • @nahuelfernandez2352
    @nahuelfernandez2352 Рік тому +4

    It is been a while since I had enjoyed a video this much. Congratulations for your engine, you did something that very few people are able to do.

  • @mmiisshhaa
    @mmiisshhaa 11 місяців тому +9

    god damn, that's sooo good. I recently created my own too, and it's so much slower and worse. I will definitely come back to this video and comment section for resources once I'm ready to improve mine.

  • @Wannabuh
    @Wannabuh Рік тому +7

    I saw your comment on one of my videos and decided to check out your channel. I've got to say...this is amazing! I'm dabbling into physics for my horror game and this is so inspirational! I look forward to more of your content :)

  • @valet_noir
    @valet_noir Рік тому +3

    This is fantastic ! Bravo 👏I'm super impressed by how stable and modular it looks. Amazing 😁

  • @pikuma
    @pikuma Рік тому +6

    Very very good. Kudos.

  • @samsaraAI2025
    @samsaraAI2025 8 місяців тому +2

    Hello! What about energy and momentum conservation. Does it works with those simple constrains? I mean imagine a real 2 rigid body pendulum. It will keep the total energy and follow the correct velocity patern? Thanks a lot!!!

    • @therealjtgill
      @therealjtgill  8 місяців тому +1

      Great question! A pendulum with no friction built out of constraints will slowly lose kinetic energy over time. This happens because of a combination of (1) not using a symplectic integrator and (2) constraint impulse calculations not adhering to conservation laws. More (2) than (1) tbh

    • @samsaraAI2025
      @samsaraAI2025 8 місяців тому

      @@therealjtgill Thanks! I am trying to do constrains that keep the real physics using very strong and short springs, but its a nightmare. I need really small integration steps and using doubles... any idea :)

    • @therealjtgill
      @therealjtgill  8 місяців тому +1

      @@samsaraAI2025 If all you want to build is a simple double pendulum that conserves all the standard mechanics quantities, then I'd look into a minimal coordinate representation and a symplectic integrator. You can derive the dynamics of the minimal coordinate representation by hand by setting up a Lagrangian, or Drake (another physics engine) could probably do it for you.
      Section IV.A of this paper has a short discussion about the difference between maximal coordinate systems vs minimal coordinate systems (slingshot is maximal coordinate).
      arxiv.org/pdf/2203.00806.pdf
      Note that a symplectic integrator won't necessarily conserve energy on a frame-by-frame basis. But over long sampling periods the system's average energy from period to period will be roughly constant.

    • @samsaraAI2025
      @samsaraAI2025 8 місяців тому +1

      @@therealjtgill I will research about that idea. I dont want to use another engine, I want to create my own one. For real simulation you know.. Newtons, meters, real watts etc.. for simulation a car suspension etc.

  • @mfalk6263
    @mfalk6263 Рік тому +2

    Very impressive!

  • @chillyvanilly6352
    @chillyvanilly6352 Місяць тому

    Quick note: CMake is a MEAT-build system, as the actual build system (in the MSVC-context usually) would then be MSBuild (which is what Visual Studio would use behind the scenes)
    lovely vid!

  • @developerdeveloper67
    @developerdeveloper67 9 місяців тому +1

    A physics engine with real time shadows, that is fancy.

  • @kgnet8831
    @kgnet8831 Рік тому +2

    NIce. Keep em coming👍

  • @cmedia425
    @cmedia425 8 місяців тому

    that intro was hilarious! Very impressive work, I know building a physics engine is not an easy feat.

  • @mumblety
    @mumblety 6 місяців тому

    Physics engines are so cool. This video is worth a sub!

  • @paperomo
    @paperomo 3 місяці тому +2

    i was wondering if you had any references for building up a contact manifold from EPA or did you use another algorithm?

    • @therealjtgill
      @therealjtgill  3 місяці тому

      I use a process that's similar to this talk by Dirk Gregorious.
      www.gdcvault.com/play/1022194/Physics-for-Game-Programmers-Robust
      Contact manifolds are made in one shot. I use the contact point and contact normal from narrow phase (GJK+EPA or SAT) to find a closed, convex set of points on body A and body B that:
      1. Contain the contact point
      2. Have a surface normal with maximum cosine similarity to the contact normal (or the negative of the contact normal)
      These closed, convex regions (CCRs, but not the band 🤣) are either faces, line segments, or single points. From here the process is pretty similar to the slides in the link:
      1. Clip the CCRs according to penetration (e.g. only include the parts of the CCRs that are actually inside either body)
      2. Clip the clipped CCRs against each other (gives you a polygon, line segment, or point where the bodies are actually in contact)
      3. If the CCR is a polygon with more than four vertices, find the four vertices on the polygon that give you a smaller polygon with maximum surface area
      4. Reproject the clipped/reduced vertices onto both bodies
      5. Use the reprojected vertex pairs as contact constraints (with the normal that came from narrow phase)
      This is the contact manifold calculation entry point in slingshot:
      github.com/Slingshot-Physics/slingshot-community/blob/main/physics%2Fcollision_pipeline%2Fcollision_contacts.hpp
      Let me know if you have any more questions!

  • @gedaliakoehler6992
    @gedaliakoehler6992 11 місяців тому +1

    Very cool, thanks for sharing! How difficult was it for you to understand and implement these algorithms?

    • @therealjtgill
      @therealjtgill  11 місяців тому +5

      Thank you for watching! It kinda depends on the algorithm - but I'd say anywhere from a few hours to a few months.
      The collision system and the constraint solver are the most math-intensive parts of the engine and the algorithms they use are intricate.
      The notion of mixed linear complementarity problems was the most difficult to tackle and I'm *still* finding ways to improve my implementation.
      Collision detection through contact manifold calculation uses the widest variety of algorithms, and some of those were difficult to implement because they required a bunch of boot-strapping.
      GJK is a good example. If you want to use GJK to figure out if two shapes are colliding/what their closest points of contact are, you have to have:
      - closest-point-on-[0, 1, 2, 3 simplex]-to-point
      - support functions for every convex shape you're going to support
      - barycentric coordinate calculator for [0, 1, 2, 3] simplex
      - probably some coordinate transformation info (but this depends on how you store shape information and how your support functions expect to receive data)
      So that's technically at least 9 algorithms that you have to implement *just to tell if two shapes are colliding*. But the nice thing is that a lot of this can be re-used for algorithms like EPA.
      I'm sorry for the info dump - this stuff is just exciting, and it's cool when people ask about it :)

    • @gedaliakoehler6992
      @gedaliakoehler6992 11 місяців тому +1

      @@therealjtgill thanks for sharing and the info dump is appreciated ;)
      I’ve looked into making a physics engine before but all the collision detection and resolution looked overwhelming so I’ve never dived too deep in. It’s nice to know this wasn’t “done in a day” so to speak.
      I like doing simulations with sound (there’s some demos on my channel if you’re curious). Are you interested in sound related things?
      Great project again and thanks for the reply!

    • @therealjtgill
      @therealjtgill  11 місяців тому +1

      @@gedaliakoehler6992 Your videos are FANTASTIC

    • @gedaliakoehler6992
      @gedaliakoehler6992 11 місяців тому +1

      @@therealjtgill Thank you so much:) I'm glad you appreciate them. They're kinda straight up - here is the demo - but I appreciate more the approach you did and others do where there's a bit more talking 😆

  • @aarfeenanees9147
    @aarfeenanees9147 10 місяців тому +1

    Awesome project. I'm new to programming and wanted to ask :
    1) C++ has no inherent graphics capabilities. How did you implement graphics into your project?

    • @therealjtgill
      @therealjtgill  10 місяців тому +1

      OpenGL :)

    • @aarfeenanees9147
      @aarfeenanees9147 10 місяців тому +1

      ​@@therealjtgillYou really are very very skilled 😵
      How many years have you been a programmer for?

    • @therealjtgill
      @therealjtgill  10 місяців тому

      @@aarfeenanees9147 Thanks! I've been programming for quite a while

  • @shawarmagames1689
    @shawarmagames1689 9 місяців тому

    Cool stuff man. Trying to achieve stable stack of boxes in my end. This is inspiring.

    • @therealjtgill
      @therealjtgill  9 місяців тому +1

      Just keep at it and you'll get there. Stable stacks are probably the most difficult feature for collision detection/resolution. Mine will eventually slide apart if the engine runs for long enough, my contact manifolds aren't spatially consistent across frames. It really just comes down to a combination of a well-tuned constraint solver, consistent (or at least smoothly varying) contact manifolds, and good friction approximations.

  • @tokyospliff
    @tokyospliff 7 місяців тому +1

    Amazing work, very impressive. What's the peformance like with ridiculous amounts of objects, are you using a BVH to cull collision checks, and have you considered adding convex mesh shapes?

    • @therealjtgill
      @therealjtgill  7 місяців тому +2

      Thanks! With ridiculous numbers of objects performance would be pretty bad. I'm running this on a single thread with no BVH, so broad phase is O(N^2).
      Fun story, this engine started out *only* supporting convex meshes, but I switched to quartic shapes to take advantage of axis test speediness. It would be pretty straightforward to include convex meshes. I'm sidetracked on improving the renderer and ECS, but once those are finished I plan on re-attacking slingshot's collision pipeline.

  • @jason_m2003
    @jason_m2003 8 місяців тому

    Amazing video! Pls do more

  • @pchandu1995
    @pchandu1995 Місяць тому +1

    how do i learn to build this?

    • @therealjtgill
      @therealjtgill  Місяць тому

      One feature at a time! Start with a basic n-body physics engine, then add equality constraints, then inequality constraints, then collisions

  • @p.a.minerva
    @p.a.minerva Рік тому +2

    Thank you for sharing your knowledge.
    Can you list some resources you used to study game physics?

    • @therealjtgill
      @therealjtgill  11 місяців тому +5

      Sure thing!
      These are the websites that I probably used the most - most of them have some overlapping content.
      www.gdcvault.com/play/1022193/Physics-for-Game-Programmers-Robust
      archive.org/details/GDC2013Gregorius
      mft-spirit.nl/files/articles/ImpulseSolverBrief.pdf
      www.math.usm.edu/lambers/mat610/sum10/lecture9.pdf
      roboticsproceedings.org/rss04/p12.pdf
      box2d.org/files/ErinCatto_ContactManifolds_GDC2007.pdf
      www.cs.cmu.edu/~baraff/pbm/rigid2.pdf
      danielchappuis.ch/download/ConstraintsDerivationRigidBody3D.pdf
      www.cdsimpson.net/2014/10/barycentric-coordinates.html
      www.cs.drexel.edu/~david/Classes/CS430/Lectures/L-05_Polygons.6.pdf
      Book resources:
      Real-Time Collision Detection by Ericson is an invaluable resource and well-worth the price
      Matrix Computations, 4th edition
      Aircraft Control and Simulation by Stevens, Lewis, and Johnson (for coordinate frame things)

    • @p.a.minerva
      @p.a.minerva 11 місяців тому +2

      ​@@therealjtgill
      Thank you for your detailed answer!
      I was wondering if you have any advice on specific resources or projects that could help in learning ECS and DOD design for creating a physics engine.
      I've always used an OOP approach, but I'd like to transition to a more data-oriented approach. I've wached some videos and read some tutorial, but I'm looking for more detailed and practical aspects, preferably with source code examples.
      Any recommendations you could provide would be greatly appreciated!

    • @therealjtgill
      @therealjtgill  11 місяців тому +3

      I'd say *the* book on DOD is "Data-Oriented Design" by Richard Fabian. There's a big difference between DOD and ECS: DOD is very much application-specific. Your application will have a particular set of data flows (or data usage patterns), and the goal of DOD is to exploit those specific flows to build a maximally efficient piece of software.
      ECS, on the other hand, aren't designed with a single project in mind - they're made to be useful for one or more *classes* of projects. I like to think of an ECS as a library of data usage patterns that arise from applying DOD to several similar projects. In my experience ECS implementations usually cater toward a particular subset of use cases. Those use cases are entirely dependent on the author of the ECS.
      Austin Morlan has a good starting-point ECS implementation (note that it's very much a starting point - AM's implementation only covers very basic use cases of an ECS).
      Austin Morlan's ECS: austinmorlan.com/posts/entity_component_system/
      Sander Mertens has an excellent series of blog posts that go much deeper into ECS design patterns.
      ajmmertens.medium.com/building-an-ecs-1-where-are-my-entities-and-components-63d07c7da742
      There's a discord server for FLECS by Sander Mertens with people who are interested in building/using ECS for all kinds of projects. You should be able to find the link to it in his blog posts or from the github repo for FLECS.

  • @protasov-by
    @protasov-by 11 місяців тому +1

    Hello! Maybe u can clarify a bit obtain distance when I know current velocity and time in seconds where object will decelerate to 0 speed but from max speed, like if we stops 0.2s from 4000 velocity to 0 then from 2000 we should do it in 0.1s accordingly? Classic acceleration formulas seems not consider I compute it in between integrating steps like velocity is not that big or mapped as dt pos += velo * dt, instead a full second range and it provide distance that too much longer than when I integrate with RK4 or velocity verlet 😅 something like 20-25 times longer. Example max speed 4000 and time to full stop 0.2s, i apply deceleration amount as MaxSpeed / decelTime with inverted to velocity direction and I stops something like braking length 500-600 position units, when I try compute somewhere length when in motion for current speed or even on full speed for such time 0.2s via classic newton formulas I get braking distance 2700 something like that. So thought maybe just multiply to dt to obtain intermediate fraction so this will be kinda meaning of what dynamic acceleration equation deriving may looks like, but then values is 5-6 units in position, like 100 times smaller 😢 or I miss something dunno.

  • @eliasrm96
    @eliasrm96 2 місяці тому

    love thanks for share

  • @cvabds
    @cvabds 3 місяці тому +1

    I bet you cant do it on templeOS

    • @therealjtgill
      @therealjtgill  3 місяці тому +1

      I would never think to desecrate those hallowed grounds with my unholy C++

  • @miumpre2839
    @miumpre2839 7 місяців тому

    Wow you are my GOAT

  • @skychip7784
    @skychip7784 10 місяців тому

    nice vid

  • @developerdeveloper67
    @developerdeveloper67 9 місяців тому

    This is very impressive. I'm still trying to figure out how to make my AABB game physics work properly with gravity and pushing against each other (lol). Maybe one day I will get to implement not-axis-aligned box colliders for my game: that is "my dream". :D

    • @therealjtgill
      @therealjtgill  9 місяців тому

      You'll get there! Check out SAT or OBB collisions. There are tons of resources online for collision detection and resolution.

  • @gmdrandom6287
    @gmdrandom6287 11 місяців тому

    Incredible

  • @foxcam3765
    @foxcam3765 Рік тому +1

    noice

  • @mrshodz
    @mrshodz 6 місяців тому

    Cool.

  • @dog_backward
    @dog_backward 6 місяців тому

    fpb