I'm Coding an Entire Physics Engine from Scratch

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
it is lockdown where i live and there's nothing to do so as a means of distraction from the fact that i have no redeeming qualities i'm gonna take on the hardest coding challenge i could think of an entire 3d physics engine i haven't done 3d physics but i've made another library before in my earlier videos i got a lot of comments asking me how i made the animations and the answer is actually i spent months and months writing my own graphics library basically i looked at three blue one browns animation library and thought you know i could probably do it better that was a waste of time but i'm bored so let's do it again [Music] gunki i hear you asking what is this physics engine gonna include well my friend if i just we're gonna do all the exciting simulations known to man soft body [Music] simulations check i'm going to start with soft bodies in fact i think it's probably the easiest one fluid sims you bet we're going to have i i don't know what happened to the resolution of the image cloth simulations rigid bodies you name it i'm just going to try to make this into one big sandbox with everything available i actually have no idea how to do these things but i'm sure we'll figure it out now it's really important that i pick the right programming language so for this project i'm gonna go with microsoft visual basic no god please no i'm gonna use c sharp and opengl i've never used c sharp before and i haven't done anything 3d in opengl so here's an epic montage showing how i learned all of c sharp and opengl [Music] so it is now two weeks later i've probably lost like five years off of my lifespan but at least we've got something drawing on the screen the code is terrible the performance is garbage and it looks like trash but we can use the excuse that those things are not the priority the priority is of course to make a soft body we need to make a sphere but how allow me to introduce you to the wondrous world of sphere meshes first we have the uv sphere pretty standard you know your average globe looks like this with the longitude and latitude but this has some problems the points are really uneven with lots of vertices on the top and bottom but not many in the middle it also has a bunch of quadrilaterals and computers don't like quadrilaterals they like triangles next there is the subdivided cube oh you have a cube just cut it smaller and smaller again and smaller again and then make it round the vertices are more evenly distributed than in a uv sphere but it's still a bit uneven and all the faces are quadrilaterals oh this is terrible where are the triangles at but fear not we got the best type of sphere coming up the ico sphere it's all triangles and it's completely symmetrical with all the vertices being distributed so evenly you can't even tell which way is up if that's not the perfect sphere i don't know what is so let's make it now [Music] after some research i actually now fully understand the algorithm to generate one of these guys and by understand i mean i found a github page with code that i can copy look at that now is it just me or is that sphere looking kind of thick don't steal my line scrub my bad but yeah let's finally start to do some physics starting with collision so let's leave collision for later we just need the ball to bounce and that only requires a floor we can do that using one float yeah that'll do so now with that out of the way let's get to the important stuff what makes a ball a ball essentially there has to be a force that pushes the ball out and a force that pulls the ball in so let's open our favorite high school physics book and see if there's anything useful oh i found something we can make what's known as a spring mass system using hooke's law where the force of a spring is equal to negative displacement times spring constant so let's just connect each point on the ball with a spring and then that'll make each edge maintain its original length and stop it from exploding hold on what's that high school physics book oh my god newton says force equals mass times acceleration alright let's give a mass to each point on the ball if it heavy it move less if it light it move more hey so we got something to hold the ball together but now it's just flopping around on the ground like it's looking for the meaning of life so what will we use to inflate it oh glorious high school physics textbook i found another useful discovery ideal gas law pv eagles nrt nrt those three things don't matter so let's just put it into one constant value but we can now just look at the volume of the ball and pressurize it accordingly the pressure in the ball equals that constant value nrt divided by volume so how do we convert that into a force pressure equals force divided by area from the book of course so we just equate them two things and boom just go through all the triangles on the ball calculate the area of each triangle and apply force that's proportional to that area pushing out the points in the direction of its normal hey it's bouncy now i make it sound so easy but this took days to implement if i were to cover all the wisdom that i've gained learning to do this and all the pain and suffering this video would be two hours long so i know what you're thinking gonki is the code on github what a great question yes it is and it's called geep3d for gonki's epic physics engine obviously the link's in the description so please go have a look at my code and help me fix that because look at all these issues i'm having if i just change some of the values then hey yo what's going on here why is it exploding i had to research this but it turns out it has to do with the acceleration velocity and position right now we just multiply acceleration by time and add it to velocity and then multiply velocity by time and add it to position this is called the explicit euler method and every once we've ever made a video game has probably done this a million times so it should be fine right wrong it's the most basic method and it's for dummy sorry i rephrase it turns out the accuracy of this euler method is a bit garbage especially especially for the spring mass system we need something else introducing runga kuta4 which is now in big boy territory because while all the normie game devs know about the euler one all the physics devs know about rongikuda four so cultured that was another painful idea to implement half my code went in the trash and i had to redo everything but hey now we can make bouncy balls with bigger values and they work just fine finally we can scale the project to be as large as we want i can't do it guys my brain is not big enough we'll just have to leave it like that as jank as it is to move on to collision detection collision was every bit as painful as it sounds so i'm just going to put a massive time lapse here in an effort to share my experience of suffering with you you know as a community perhaps the thing that unites us is the universal post traumatic stress disorder we acquire from browsing too much stack overflow hopefully some university professor can stumble upon my github page and magically help me fix everything alright now is the moment of truth everybody what we've all been waiting for look at this 300 lines of blood sweat and tears all right as you can see there's a few gaps for the soft body to get through it's taking its swell time right now i really hope it doesn't explode on the way and it is about to clear the first gap as you can see all right take your time no rush hold it together don't please don't explode stay calm okay we're almost there oh i don't i don't like the look of that oh it is in bad shape right now that's not that is not good that's definitely not good just it is one more gap just hold it hold it together come on i uh that's it take it or leave it i don't know i'm not big brain enough to figure out what's wrong with that it took me a while to figure out a set of values that actually allows it to get through that semi-intact damn it is tragic how bad the code is right now alright you know what just subscribe to my channel i'm gonna fix it in the next video i don't know maybe make a fluid sim while we're at it [Music]
Info
Channel: Gonkee
Views: 356,161
Rating: undefined out of 5
Keywords:
Id: iSMbRGTBOHU
Channel Id: undefined
Length: 9min 19sec (559 seconds)
Published: Sat Oct 09 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.