2D Physics Engine! // Code Review

Keep exploring at brilliant.org/TheCherno/ Get started for free, and hurry-the first 200 people get 20% off an annual premium subscription.
Patreon ► / thecherno
Instagram ► / thecherno
Twitter ► / thecherno
Discord ► / discord
Code ► github.com/St0wy/GPR4400-Phys...
Designing a Physics Engine in 5 minutes ► • Designing a Physics En...
Send an email to chernoreview@gmail.com with your source code, a brief explanation, and what you need help with/want me to review and you could be in the next episode of my Code Review series! Also let me know if you would like to remain anonymous.
CHAPTERS
0:00 - Recap
1:14 - How to design an API
2:52 - 2D Physics Engine design
9:02 - Pointers and ownership semantics
13:00 - Using std::weak_ptr to be "safer"
14:39 - More physics
16:42 - Spatial partitioning for optimization
22:55 - Be careful when trying to optimize!
26:37 - Physics solvers
31:50 - Final thoughts and extra challenges
This video is sponsored by Brilliant.
#CodeReview

Пікірлер: 77

  • @TheCherno
    @TheCherno Жыл бұрын

    And so the trilogy comes to an end. I’m gonna need fresh code so make sure you send it in! And in the meantime, keep exploring at brilliant.org/TheCherno/ Get started for free, and hurry-the first 200 people get 20% off an annual premium subscription.

  • @-TL-15

    @-TL-15

    Жыл бұрын

    Help please !

  • @ng.h9315

    @ng.h9315

    Жыл бұрын

    Amazing Course's 👏♥️ Thanks Cherno!

  • @dylanwilliams9860

    @dylanwilliams9860

    Жыл бұрын

    KZread needs a submissions system the creator can "open" and "close" at will. So many reviewers would benefit... Also C/C++ code only for the submissions?

  • @danilomitrovic3954

    @danilomitrovic3954

    8 күн бұрын

    Triology :"The Milkening!!!"😅😅😂😂😂

  • @ryans9094
    @ryans9094 Жыл бұрын

    How on earth he can look at, understand, see what's wrong with and fix everything in seconds is amazing to me. I strive to be this good in the future.

  • @AirAKose

    @AirAKose

    Жыл бұрын

    Experience and familiarity. Once you've looked at enough code, you pick up on patterns and common problems - and get better at recognizing them Familiarity with the tools + ease of navigation through the code helps immensely as well

  • @Mcdude22

    @Mcdude22

    Жыл бұрын

    I hope to be this good at *literally anything* someday

  • @cyberchef8344

    @cyberchef8344

    8 ай бұрын

    Keep at it - if you spend ~2 years writing c++ regularly you would be able to spot all of the same issues.

  • @rmt3589

    @rmt3589

    8 ай бұрын

    Have hundreds of nights where it's broken, you can't fix it, you go to bed, then in the morning you solve it.

  • @Stowy
    @Stowy Жыл бұрын

    Once again thanks a lot for the code review, I learned a lot ! I'll definetly try to improve it in the future :) For the space partitionning, I mostly did it because our school told us it's a good idea to try it (to learn what it is, how it works etc), but it's true that i've never tried to test and measure if that brings performance improvements haha

  • @xeridea

    @xeridea

    Жыл бұрын

    If you would like to learn quadtrees, there is a good video by OneLoneCoder.

  • @Dr-Zed
    @Dr-Zed Жыл бұрын

    I'm not doing any game development. These videos are still very educational and I can't stop watching.

  • @SoDamnMetal
    @SoDamnMetal Жыл бұрын

    25:37 Oh damn, straight through the heart with that one!

  • @tyuchevv341
    @tyuchevv341 Жыл бұрын

    These video's are incredible Cherno! I'm studying CS & Game programming and its fascinating to see you dissect others projects/code & suggest improvements. It really helps me think about my own implementations!

  • @LULEIVON
    @LULEIVON11 ай бұрын

    9:06 - I guess that RigidBody() situation is why some of us were taught to camelcase and include a verbs in function names. 😅

  • @on-hv9co
    @on-hv9co Жыл бұрын

    A partitioning technique I've come to love is using a grid with BVHs. the grid takes care of the global partitioning and the BVHs take care of the local partitioning inside the cell. and a benefit of using the grid is that checking the "next" cell means that the "next" cell can avoid the previous cell check meaning that if you begin at some corner you can grow outward along the cells and perform only 4 checks per cell instead of 8

  • @slygamer01
    @slygamer01 Жыл бұрын

    The adding of the rigid body to the dynamic world would be done in the derived classes so they can set further properties on the rigid body before it is added to the world.

  • @zhivkobogdanov2845
    @zhivkobogdanov2845 Жыл бұрын

    One comment that could be added to the grid implementation, which is typically seen, is that you could totally avoid having the triple nested `std::vector`s by converting the 2D index into a 1D array index and vice versa.

  • @SeanJMay
    @SeanJMay Жыл бұрын

    The full Knuth Quote, from The Art of Computer Programming is: “The real problem is that programmers have spent far too much time worrying about efficiency in the wrong places and at the wrong times; premature optimization is the root of all evil (or at least most of it) in programming.” In full context, he’s actually getting at the worries you illustrate; it's just been co-opted by people who use it out of context (often to the opposite effect). This isn't the only misunderstood quote like that by a long-shot. “KISS” usually makes me furious, when I hear it from a manager.

  • @hbobenicio
    @hbobenicio Жыл бұрын

    I'd give a 9 or 9.5 for this. Very good code, much better than lots of dev mates I've worked with lol. Good series too

  • @Moonz97
    @Moonz97 Жыл бұрын

    Awesome trilogy! I'm amused with your annotation skills; could you share what software you are using @34:00 to draw the red annotations?

  • @saeedbarari2207
    @saeedbarari2207 Жыл бұрын

    "I'm banning myself from talking about anything except physics" *2 seconds later* "it's good that you have different classes for collision and solvers because it's always good to..."

  • @bunpasi
    @bunpasi Жыл бұрын

    Be careful with using a grid system when calculating collisions. A large object can span even outside of the surrounding blocks. Look for example at the long horizontal bar in the demo.

  • @Stowy

    @Stowy

    Жыл бұрын

    In my implementation i look a the bouding box of the collider, and add the object in every cell it's overlaping, to avoid this problem

  • @nielsdaemen
    @nielsdaemen Жыл бұрын

    Basically collisions are O(n^2) and sorting all the objects into a grid will be O(n)

  • @shaiavraham2910
    @shaiavraham29108 ай бұрын

    What software do you use to zoom and annotate the screen when explaining things? I know it was already asked and he mentioned it in some video, but I can't remember where.

  • @bluesillybeard
    @bluesillybeard Жыл бұрын

    As for the rating, I would say having a rubric would be great. For example, points could be rewarded for having better memory management, or having better/more useful comments, etc.

  • @hugoalmeida1291
    @hugoalmeida1291 Жыл бұрын

    Cherno this theme you're using in your vs code is awesome. How can we get it?

  • @vectoralphaAI
    @vectoralphaAI Жыл бұрын

    I know NOTHING about game engines, and having watched your videos for a couple of weeks i feel like youre really knowledgeable and the person to ask. Was just wondering with your experience what you think is the "Easiest" Game engine to use to build games for a complete beginner to games development? I have programming experience just never worked in game dev. What do you think are the easiest to use game engines to build games for beginners like me? Anyway love the content.

  • @Wolferey

    @Wolferey

    Жыл бұрын

    What game engine you use really comes down to 3 parts: type, resources, community. - What type of game are you making? 2d? 3d? rpg? visual novel? c++? c#? etc., if you know what type of game you are making then it'll naturally narrow down your game engine choices. - Resources, like their own documentation, youtube tutorials, samples etc. the bigger game engines like unity, unreal, godot have lots of resources which will naturally make it easier for you to make a game. - Community, which can go both ways. Big engines have big communities so there is lots of information you can find, but it might actually be harder to ask and get help when you are starting out versus a smaller engine where everyone wants to help because they want more people using their engine. In the end, game engines are just tools, figure out what kind of tool you need and then test out the selection you can find, the "easiest" game engine out of those will be whichever fits those 3 parts. If you don't know what type of game you are making, then any popular game engine will probably have lots of resources and a solid community for you to start learning, those will be the "eaiser" ones until you get more experience and hit the bounds of what the engine can do.

  • @bluesillybeard

    @bluesillybeard

    Жыл бұрын

    I would say Godot or Unity, but as @Wolferey said, it really depends on what kind of game that you are going to make.

  • @leonardoguarilha9293
    @leonardoguarilha9293 Жыл бұрын

    Hey Cherno, What is your VS Theme?

  • @Spartan322
    @Spartan322 Жыл бұрын

    18:29 Even just a spatial hash grid would be pretty simple here.

  • @benhetland576
    @benhetland576 Жыл бұрын

    30:03 IMO a pointer type IS boolean by its very nature, at least in C++ and in C. This is because it either "points" (to something) or it doesn't point to anything (=nullptr). This is the traditional idiomatic way of using pointers in both of those languages, so the way you illustrated it can be done is the way to go! Many prefer to write stuff like "if (ptr != nullptr) ..." or even "if (flag == true) ...", but I would argue that those are tautologies that do nothing good while even reducing readability at the same time.

  • @MrVintarb

    @MrVintarb

    Жыл бұрын

    No it could be much more complicated scenarios. For example, tagged pointers.

  • @Exodius1999
    @Exodius1999 Жыл бұрын

    Hey cherno, have you ever used CUDA?

  • @medul8039
    @medul8039 Жыл бұрын

    im in your walls

  • @user-hz4tc2pf3x
    @user-hz4tc2pf3x Жыл бұрын

    Seems like Cherno whatched some Handmade Hero

  • @Solemn_Lemon
    @Solemn_Lemon Жыл бұрын

    I own pointers

  • @ZihadJoy
    @ZihadJoy6 ай бұрын

    create a new series of making a 2d physics engine

  • @lolcat69
    @lolcat69 Жыл бұрын

    I am making a game engine, so maybe I will implement this physics engine and I'll scrap what I have now ( is not even a physics engine, like, is just AABB Collision detection ) xd

  • @stio_studio
    @stio_studio Жыл бұрын

    I am making a bloon tower defence game in scratch. Whana do a code review on it? (when it is done)

  • @__hannibaal__
    @__hannibaal__ Жыл бұрын

    These programmer need to return to mathematics, i mean hard mathematics, set theory, GROUP Theory, Logic and prédicat calculus, Number theory and graph with topology theory is very helpful if we interpreted to Program, and it will beautiful program . Check with Stepanov; and first programmer all Mathematicians when there no CS.

  • @anon_y_mousse
    @anon_y_mousse Жыл бұрын

    Now if only you could do a video on how to get a good digital artist to do a bunch of work for free.

  • @theonetribble5867
    @theonetribble5867 Жыл бұрын

    I learned programming C/C++ and thought I understood pointers/references. Then I learned Rust and now when programming in C/C++ I always get Anxiety working with pointers because I can't guarantee that the code is safe.

  • @GamingDemiurge
    @GamingDemiurge Жыл бұрын

    What about using a reference instead of a raw pointer in the map?

  • @joysaha3927
    @joysaha3927 Жыл бұрын

    Where is your cat cherno?😞😞

  • @ScorpioHR
    @ScorpioHR Жыл бұрын

    Cherno doing code review is like little red riding hood in the flower field. The difference is, when he gets too far into the dark forest, Big Bad Wolf runs away

  • @adonaidiogenes8536
    @adonaidiogenes8536 Жыл бұрын

    Every time I see you coding your Game Rngine, I think how difficult it is to make a Game Engine in this modern standard aimed at retro consoles like GBA, MegaDrive...?🤔

  • @FBI_Master
    @FBI_Master Жыл бұрын

    C++ makes my head hurt why can we just use C# and get along or hell C is a better language from a not losing my mind perspective

  • @Stowy

    @Stowy

    Жыл бұрын

    Well C# is JIT compiled and has a garbage collector, so it's too slow haha, and some features of c++ are really usefull for game programming

  • @isakthehackervlogs
    @isakthehackervlogs Жыл бұрын

    6:52 And that is why you NEVER begin your functions with a capital letter. Only Types begin with a Capital letter!

  • @sleyeborgrobot6843
    @sleyeborgrobot68437 ай бұрын

    thank you jimmy falln

  • @CreativeOven
    @CreativeOven Жыл бұрын

    Is this code available to the public to take it into Javascript ... L :

  • @Stowy

    @Stowy

    Жыл бұрын

    Yes ! You can find it on my github ! (you can see the link in the first episode)

  • @CreativeOven

    @CreativeOven

    Жыл бұрын

    @@Stowy ok thanks 🙏👍😼

  • @CreativeOven

    @CreativeOven

    Жыл бұрын

    @@Stowy Any shortcut on how to run it I see a lot of cmake files and such no good feeling about that XD....

  • @benhetland576

    @benhetland576

    Жыл бұрын

    @@CreativeOven I guess just "cmake" then "make" should do the trick on a decent system, otherwise either the CMakeFile or the system itself sucks :-)

  • @CreativeOven

    @CreativeOven

    Жыл бұрын

    @@benhetland576 Ok thanks I see where the compass is facing then thanks ! yeah I know where to go basically....

  • @galacticslime6824
    @galacticslime6824 Жыл бұрын

    Second

  • @jimmychen4796
    @jimmychen4796 Жыл бұрын

    who can beat me!

  • @marcotroster8247
    @marcotroster8247 Жыл бұрын

    Dude, idk why C++ devs love their inheritance so much even when it makes no sense at all 😅 Most of the classes are malformed, contain weird operations that don't fit there, etc. TDD/DDD exist for a reason 😅

  • @jimmychen4796
    @jimmychen4796 Жыл бұрын

    first!

  • @Jkauppa
    @Jkauppa Жыл бұрын

    its the original hardcore brute force way to not use partition/acceleration grids

  • @Jkauppa

    @Jkauppa

    Жыл бұрын

    in first ray tracing and such, low number of elements, not production quality, just first trial fast non-thought-through implementation

  • @Jkauppa

    @Jkauppa

    Жыл бұрын

    if you do raster render by tringle lists, you are not using the space partition efficiency (boundary volume hierarchies)

  • @Jkauppa

    @Jkauppa

    Жыл бұрын

    you should do a high-level render pass with hierarchical geometries

  • @Jkauppa

    @Jkauppa

    Жыл бұрын

    dont limit anyone to any mold "scientist"

  • @Jkauppa

    @Jkauppa

    Жыл бұрын

    remove yourself if you put rules in place

  • @anthonysteinerv
    @anthonysteinerv Жыл бұрын

    When you doing a 3d/2d physics engine tutorial series?