5.0a: Introduction to Physics Engines Part 1 - The Nature of Code

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello welcome to the first video in a playlist about physics engines now strangely enough this video that I'm making right now that you are watching because somehow you are is made in 2017 but the next videos that follow in this playlist were made several years ago and those videos cover two specific engine physics engines box2d and toxic lives so what I want to do in this video is kind of make a little update to what's the state of physics engines today what are physics engines what are some examples physics engines that you might be interested in what are some of the features of them how do they work and then if you are keep it being interested you can continue and watch them the more specific tutorials at the very end of this playlist you'll find some new updated physics and Guice tutorials with some newer physics engines that I've only started recently looking at most notably a JavaScript one that runs in the browser called mater dodging this is an example of a processing sketch a computer program drawing graphics to the screen and using a physics engine behind the scenes to figure out what is when do things collide what should they do after they collide all of the motion and animation the location position velocity angle all of the stuff that's telling me what to draw in this is a convention that makes no sense whatsoever let me try to make this make sense okay so if you've watched any of my previous tutorial videos one of the many and many exist many programming tutorials this is a thing that I do I have a variable let's say I'm writing code in JavaScript I have a variable called X maybe and then somewhere in an animation loop like the draw function in PJs I might say something like x equals x plus 1 and what that gives you is if I want to draw something at that particular X location maybe at a Y location of 100 if I increment its location every frame it's going to start to move this in a way is a physics engine what's the world of physics it's a very holistic world things can only move at a constant velocity on one dimension by one unit every moment of time but ultimately this is a physics engine now if you follow some of my other tutorials I start to add in a lot more things like a lie and maybe I start using a concept called a vector whether it's a P vector and processing or a p5 dot vector and then I'll start to have things like oh there's a position and there's a velocity and there's an acceleration and there's a force and so and then acceleration positive opt-ins allowed to go to the position all of this kind of stuff to to formalize and encode a framework a system for how things move around a computer screen models off of the way things generally pretty much move in the real world you know lots of scientific caveats aside so this is the trajectory that you'll find if you go back and watch a lot of my nature Code video tutorials or even a lot of the coding challenges where I'm moving in animating things in a computer graphics window now what is some day so you know this bit will get any pretty far and it's actually a really nice way to work because I have total control to invent my own creative take on physics and and different kind of behaviors and make creatures that have thoughts and desires and all sorts of things could leave you do a lot of exciting interesting things but there are moments where you might think to yourself up all of this vector math and I got to do a collision and suddenly you know I want to know what happens when this strange polygon collides with the circle and if it bounced off this way you know you're starting to go down this road and suddenly you're you know spending years of your life developing these very elaborate sophisticated algorithms to figure out how to make things move based on a physics and these are not new problems and so there are library frameworks that you can import into your code to eliminate the need for you to write the lines of code where you add velocity to position or add acceleration to velocity and all you do the way that a physics engine generally works is you can think of this as the physics engine it's a box and in some ways it's a black box you don't have to know how it works inside to make use of it although that you know it's kind of hard to just use one without some of this basic sensibility and having done some of this yourself to understand about forces and vectors and all that kind of things because these concepts are all embedded in physics engines but if you have this black box you have a moment writing in your program like the cameras over here but I'm over here do you see me there's a moment where you have the program start and you say hey world of the thing that I'm making I would like you to have some circles floating at the top of the screen and then maybe your rectangle at the bottom and then you should have this little like spinning wheel and then I'm just going to say go so it's the idea where the physics engine is that you have this kind of setup where you tell the world what should be in it so in setup you might say here's what I imagine the world to be it starts with in one of the coding challenges I hope to do that'll be in a future video let's Plinko so I want to have you know a bunch of static knobs in the window and then I want to have a thing that's at the top and then I want to have some buckets at the box what's a weird example okay so it's all about him setup it's saying here's what's going to be in the world then you tell the physics engine here's all the things here's where they are here are their sizes and then you say go and then there's this animation each the draw loop in processing or p5 every moment in time and what you do every moment in time you can say all physics engine where is the stuff so you set up world the things are all of the things of like moving the stuff around checking if it's colliding and then if it's colliding and velocity changes all of that is done inside of this so-called black box the physics engine and you're only driving to say where is everything now I would like to draw it so this is the paradigm that you'll find in physics engine after physics engine and there's a bunch that I want to discuss and talk about so let me make a list of different physics engine talk about some of their features so box2d being one of the more famous well-known physics engines that's behind a lot of well-known games like Katniss or 2d mobile games like Angry Birds fill in all the other ones after that box duty there's a physics engine and actually something called toxic lives which is promising but what started at specifically a to start it specifically for processing a Java library is actually a much broader law library beyond physics but one of its components is a simple physics engine we'll have there is something called a Bullock which is a physics engine specifically for 3d there is a physics engine I want to look at called matter AF cannon is is another one that I recently found and the list goes on so I'm sure I'm missing you know your favorite physics engine or a really good one or one that's really useful in this particular context or for this particular programming language but this is a list of ones that I know something about or that I find useful in some context whatsoever so one thing that might consider for you over there you watching keep your run away I would totally understand one thing that you might consider is whether you can do this in 2d or 3d so box 2d is noted by the name is 2d only 2d toxic lives has both 2d and 3d physics bullet is a 3d physics engine matter chess is 2d and Canon EF is 3d so I don't know why they all get an exclamation point but they're very excited about their number of dimensions I have a 2d for the Glendon another thing that you might think about which physics engine is what eaters do they have so you know what language is a 2d or 3d but really keeping its features and actually I realized that this list is kind of useless I'll leave it there because what I want to just take a moment well you know I the kind of key teeth here which is if I if I need to tell the world what kinds of things are in it well how do I do that so what are the kinds of elements that go into the world so tip most almost all of these physics engines use the same paradigm which is and I'm going to use each visit as you might use a slightly different term for the same thing but for all these physics engines there's generally something called a world so the world is that thing that the world is the entire world of what you're the sort of space of physical space letters 2d or 3d that the entities that you're going to place in that world live it and it could have on you know it could have kind of global properties like it could have a global gravity it could have boundaries you know where things can't go outside us so typically most of these physics engines have a world the other thing these physics engines have typically is something called a body a body being an entity that goes into that world and it might be a circular body a rectangular body an arbitrary polygon type of body and I'm kind of conflating something though which is box2d in particular has two concepts which is a body and then a shape so the body meaning the sort of singular empty point that's flow think that's moving around through the space and a shape being actual physical geometry that's attached to it so we could think sort of but some of these physics edges that are a little less sophisticated are just it's sort of the same thing that body and shape are the same thing so body and shape the other thing that most of these physics engines have is something called trying to think of which term to use in box2d it's referred to as a joint in matter chess it's a constraint in toxic lives it's a spring and so I'm going to call it just a connection I'm going to call it a spring now let's call it the connection and so what I mean by that is what if you had as your physics engine there is a body and it's connected to another body that's swinging like a pendulum how can you have a constraint or a spring or a joint that ashes to body and it's kind of amazing what you can build just from that generically if I have a way of placing any kind of shapes in the world connecting them in some way that I want and then say go experience gravity bounce into each other interact with the valve experience of force there's a lot of such there's a lot of worlds you can create that are quite engaging exciting and to play with so so business these are kind of a teachers but there's a kind of key things books I certainly should have started this whole video if you're still watching good for you because here's the real thing I when you want to use the fence physics engine and when do you not want to use a physics engine and it's sort of like a strange question because no matter what you're using a physics engine the question I'm really asking is when do you want to just use your own custom baked x equals x plus one or position not add velocity or your own kind of cooked up mechanic for how things move around the space versus just use somebody else as a mechanic that you get you set up and then reap the benefits of as you draw things and there's no answer this question and most of the time you're in a sort of in-between space but there are a couple scenarios that I think are key scenarios when using a physics engine is incredibly valuable key scenario number one collisions collisions collision khalif's like it's like real estate location location location physics engine super collisions collisions collision so if I'm ever working on a project and I'm moving stuff and use exterior be hey VIIRS I add gravity and I've got all this stuff hooked up to my code that runs what I really need is for these shapes as they move around to bounce off of each other this is generally where I want to move towards using a physics engine because rather than spend you know hours and hours on stackoverflow and rejects your lots of Wikipedia pages to work out all sorts of geometry math and collision detection and resolution these are solved problems that are built into a lot of these physics engines so that's kind of arbitrary thing number one and where I am on this white board which is to use collisions not to use it to use a fixed engine if what you need is collision here's another place so I'm at what this what reminds me of this is this idea of physics engines having a world Abad work a world that's full of bodies and connections and so one thing that physics engines are very good at is if you want to build some type of simulation that's based on a connected system so uh and so some examples of this are cloth simulation and you know Oh cloth simulation how is that a connected system well actually a way and I have a coding challenge that I'll link to in here that builds the cloth simulation using the toxic little physics engine but you can think of well what is a cloth what if you had a lot of dots you're going to have to watch this video for a little bit while I draw a pollution pot and then what if you connected all of those dots with some sort of springy elastic connection joint spring and then you pulled on one and I'll show you a version of this in the middle I'll show you some example so this is the way then you textured it with color or whatever you that sort of thing so a cloth hair these types of connective a systems what if you wanted to make you know a cute cuddly character like this I don't know it's for ears and it's sort of like it's soft and it bounces and it jiggles that Wiggles around well you could film this by creating a set of particles perhaps that are connected with Springs and maybe some interior connections to keep it from collapsing on itself and that could be the skeleton physics behind this particular character and there are lots of examples so the other thing that's sort of key about using physics engines is you don't have the way you've set up the world and tell the physics engine what's in it could simply be a skeleton for what you lay on top of it you might say hey there's a lot of circles in this world but what you're going to draw are you know images of first that are Euler and there's a lot of rectangles in this world what you're going to draw our like little cardboard box textures wood box textures that type of thing so collision collision is a key thing and also connected systems so these kind of soft body simulation cloth simulation chain pendulum you know there's a lot of scenarios were this idea which a physics engine is going to kind of do a lot of this stuff for you it's a kind of known way of implementing something okay so what I want to do here is thinking about collisions and connected systems is I want to look at these and kind of think about which which what kind of features these engines have so box2d is like kind of amazing for collisions so if you want to work with collisions in two dimensions of any arbitrary geometry box2d does this incredibly well if you want to work with connected systems springy soft body simulation squash based simulations that type of thing possibly does this really well now you can do those constellations in box 2d the toxic lip is really just highly optimized towards this world of particles Springs and top eclipses has no collisions so toxic lives is just a particle spring based physics engine with no geometry whatsoever so in this sense if you want to work with so these are sort of features here bullet for 3d I actually really barely any experience whatsoever I just could be one example that use of a problem library for bullet but for 3d collisions you can use bullet for that and then matter gjs really I would say collisions and connected systems looking at it there's a lot of features for both of those things so you know you kind of I guess there's a puzzle here which is I'm working on a project that I want it to be in a browser and I wanted to just be a cloth simulation you know may be toxic lives is all you need well I'm working on a processing sketch and I really need to have I'm making a 2-d game where all the stuff is like bouncing off of it since all these like strange polygons well maybe I was using box2d but if I mean the browser and I kind of just won a very generic physics engine that has collisions and brick constraints and I'm like as mad as a chance so hopefully if you actually watch any of these videos and see some of the examples you'll see where some of the physics engines really shine and where others maybe aren't as as useful you know one thing I'll say in terms of degree of difficulty there's there's a level of complexity that the engine has in terms which is which you know if it has a high level of complexity that's really great in terms of possibilities but also makes the overhead of working with it so kind of extreme that sometimes not worth it you know if all you want is like a bouncing ball you know you don't really want to go through all of the rigmarole of importing all the box of thee libraries and learning how all the pieces work and you've got set up all this extra code but but in terms of seeing is this working with I would say boxing is probably one of the hardest on the Toxic lives in many ways is one of the easiest but you know it's a library for many years ago and there is finding the documentation examples is a bit difficult to do whether it's box2d has such a large user base there's a lot of documentation and examples for it albeit you know depending on what environment you're working with you know there's not necessarily as much for working with it in the browser but matter touch is one of the reasons I wanted to explore it is it has a really nice website with thorough documentation a lot of good examples if I come over here you can see so this is an example of box2d running and processing and one of the nice things about box2d it has it has a concept of a motor so you can see there's actually this the motor is on right now if I click the mouse you can see this this is a revolute joint it's actually a way of attaching to things based on like a singular point that one object can revolve around and so you can see almost like a wheel you can see without the motor this this kind of seesaw just responds to what's landing on it but if I turn the motor on its going to turn and actually active activate and start to hit these circles that are falling from the sky so to speak if I go to another use for this is an example of box2d which has something called a change shape which I'm using to create this surface and you can see here that I'm also using revolute joints five acres exist not but our revolute joints to have the wheels of this car turn and move along this surface so that's you can see some possibilities here with box2d and this is an example of using these are called distance joints which are like spring Levin boxes where all these circles are connected creating this like springy like bridge and if I click here I can add rectangles to the system and you can see they're kind of landing and getting caught on this particular bridge so these are the kinds of things that you can think about making in a system like box 2d now what else do I have to show you so in terms of thinking about toxic lives and connected systems this is actually toxic load simulation where it looks like oh I've got this like stringy rope based thing and I can kind of click on this and grab it and move it but really what this is if I were to go into the code just for a brief second and just change this to ellipse pxpy 8 comma 8 I'm going to draw a little circles there this is actually what this is actually what's going on so if i zoom in here you can see that what this actually is I'm not drawing the lines between them as these are just a lot of particles each connected by a spring so when you pull on monocles on the other but depending on how I render it I can make I can render it all of the continuous line which gives it kind of like a string like quality to it a string or rope like quality to it let's put this back and I can display a larger circle at the bottom and now you can sort of see now you can sort of see more what it look what it looks like here with this kind of view so this is toxic lives and I could open up another toxic lives example just to show you this idea of like the cloth simulation so you can see this is a lot of particles all connected by Springs but in a grid with some gravity pulling down and the two they're just locked in attached to the top and you can see it has a kind of cloth like quality to it and I think and then a batterer Dutch as so matter to here's the website for a matter judge ask and you can see what's really nice is just right and then on the home page it's showing you a lot of demos of the kinds of things you can do with with with matter judge is this is my favorite these little like car things actually that's my favorite I love this one I got built I got to build some of these was like a challenge or something okay so that's a bit of an overview about physics engine I'm doing this as a live stream right now you might be watching this as an archive video and so what I'm going to do is now take some questions from the chat to see what I missed and answer them you
Info
Channel: The Coding Train
Views: 155,664
Rating: undefined out of 5
Keywords: JavaScript (Programming Language), live, programming, daniel shiffman, creative coding, p5.js, coding challenge, p5.js tutorial, p5js, javascript (programming language), tutorial, javascript, coding train, physics, physics engine, collision detection js, collision detection javascript, matter.js, box2d, toxiclibs, canon.js, bullet physics library, physics javascript, physics engine p5.js, physics processing, physics engine intro, introduction physics library, intro physics engine
Id: wB1pcXtEwIs
Channel Id: undefined
Length: 22min 23sec (1343 seconds)
Published: Mon Mar 06 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.