5.0b: Introduction to Physics Engines Part 2 - The Nature of Code

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
okay I've got a set of questions that came from the chat I'm going to try to answer some of them which are end you know who might not be answer or I may not know the answer but I'm going to discuss them with you to the best of my ability and you're going to write in the comments and help people who come back and watch this video later to see other thoughts and perspectives and answers here okay so first question here's help from Nick and I'm combining it rewording some of these questions so Nick asks how flexible are the engines and how easily can you integrate them with code and related to that Jonathan asks are there abstract uses of a be using these physics engines you know without even computer graphics and drawing simulation if we come back over here and I think you know in some ways there's a different answer for a lot of these questions one thing that makes box2d difficult to work with is box2d is actually in many ways sort of a silly word to use but a very pure physics engine it knows nothing about computer graphics and pixels and it's units of measurements are actually meters seconds real world measurements now there's some nuance to this because it's sort of finely tuned for small units of measurements but but that aside when you actually do this prophets effect where all the things are you have to give box to be a set of locations in which physical measurements as if it's in a real physical space and box2d will tell you where the stuff is at a moment in time and you have to convert that back to a pixel space so in that sense is incredibly flexible because it abstracted from computer graphics at all and you could actually use it in the program and never draw anything and can use it to simulate some aspects of things it makes it kind of difficult to work with but because you have to do all this conversion and thinking and it makes it more complex than say matter that chance or toxic list where you can just give the engine pixel based coordinates and it gives you back pixel based coordinates but it does give it us through level of sophistication and perhaps even accuracy you could maybe say I mean I'm not expert enough in the lower level mechanics of all these engines to really say which one's more accurate than the other but but that certainly is an aspect of this for sure okay we come back and see what the next question is a flip kid and the app can use multiple engines at once you absolutely could I think you're probably asking for trouble in some ways I mean I'm sure there are some very unique interesting situations where that makes sense but there's enough time and overhead in sort of learning it's certainly worth learning multiple physics engines but you know there's enough specificity to what are the objects that are available one of the names of all the functions and there's you know if each physics engine probably has its own vector object and you can't give box2d a vector from toxic lives and you can't give toxic lives a vector from box of these you're gonna have to be converting so I'm not so sure that you would really want to do that but you again this is anything is possible because you are the designer of the system and you are the drawer of the knee or the person rendering the world so you could give the information to multiple physics engines and try to like get stuff out and send it back you know in put it back into the other ones and do some kind of crazy convoluted thing but I'm not so sure one thing I should mention is that you don't actually have to do this necessarily matter touch is and up for example I know very specifically has a kind of automatic debug view so you can just say to matter here's a canvas draw where everything is on the canvas is behind the scene in its code has the ability to do that but my point of view and showing you these things is really to focus on how you draw the stuff yourself because that's where you can really explain your own design sensibility and that's kind of one of the questions here I'm going out of order here but Albert asks I can use shaders and how can you really put your design it's really that's a really key aspect of being creative and working with physics engines in creep and making something that's not just looking like a demo so all of my examples are demos because I'm taking I'm taking the physics engine and kind of drawing the literal pieces of it on the screen in a literal way i'm overusing the word literal but but i'm saying what stay in the physics engine let me draw exactly what's in the physics engine as for simple geometric shapes whereas there's no reason why you couldn't make fuzzy textures and blend modes and do all of your creative drawing using the coordinate and geometry information that's coming out of the engine okay what else did one of us what else do I got here on this question so I this this performance considerations is absolutely a big one so one of the nice things about using a physics engine is that you know assuming it's something that's worked on by a lot of people over a long period of time you know all the ones I'm talking about our open source engines that are most of them the source code is on github and have a wide variety of contributors but probably for you know people have been working for years on optimizing them so in many ways it's going to be more efficient and have higher performance than what you might throw together and today that said these types of computations are expensive to look at every object in a system and weapon warehouse colliding with every other object and it's going to have some kind of complex polygon geometry so you know there certainly is a slowness here but you know box2d is very efficient runs very fast especially if you're in C++ that's going to run slower in the canvas and the browser qurani slower so you know yes there are performance considerations you could I sit here and like talk about them for I'm like asking myself questions now the answer that is no like I I'm that I think Marisa kind of a lot of trial and error and digging into the source code and seeing where how things worked at best okay Nick does not deserve credit for the first question I don't know who who asked that question was a great question it was not by dick thank you and let's see do some simulate more accurately than others that's you know that's another consideration here there's all usually often a trade-off right it's going to be slower if it's more accurate it's going to be faster if it's less accurate apparently that question is from gap Gabriel frappe and Kenya okay another question here is from Philippe how to use for this I reward this question but how do use force this with physics engine and can control the losses this is a big question which I will cover and look at in a lot of my example but the question really is I kind of set you up in this way of like hey let's put stuff in the world it's in the world now let's go over here let's read where the stuff is and let's draw it to the screen but what if the thing listed in the world I want to control how it's moving or I want to like add a force into the world that's going to affect the way that the world is behaving so most of the physics engines are going to have a mechanic for this for example the body object you create this body object you put it in the world you can keep a reference to that body object and later you might be able to call the function code like add forth or apply force to cause the body to experience maybe a sudden gust of wind for example there's another there's a question those like well what if you want to affect these not with a force but by actually say hey look this object's velocity to be this and yes physics engines will let you do that because you know number one is you can just kind of dig into the source code they're going to body's going to have a variable in it someone it's like the velocity you could set it but you have to watch out a big problem here it's very easy to violate the laws of physics in witnessing extensions it's kind of hard to do that although I would love that's a good like real world challenge let's violate the laws of physics sure that would be an exciting YouTube video get lots of views but we can easily violate the laws of physics for example what if I just take an object that's in the world and I say hey you know its position is over here now instead it's positioned over here I can actually do that and it's going to appear over there but I what I've essentially just done is i teleported it which violates the laws of physics you know at least in terms of specific action and so in that case you might start to see some strange behavior but if I wanted to move something around manually through an interaction without teleporting it to cause some strange behavior what I might be able to do is attach a connection maybe I captured a connection to the object and I connect it also to my mouth and then I move the mouse around and I'm tugging around looks like I attached the string to something and moving it so that's a way that's one strategy for moving something around through you know a pearly noise algorithm or to user interaction by still staying within the sort of laws of physics of that it depends on how the physics engine works but this is certainly a common problem and another was really a question I added to my list another very common thing about physics engine is oh it's doing all this collision stuff for me that's great yeah I don't do the collision stuff but what I really want is when they collide I want to think play a doughnut music or something so if you want to do that you have to know when the things collide and that's happening in here how do you know when it collides well you could say okay well what I'm going to start to do is I'm going to look at all the things and look at where they are in relation to each other when they're when they're within a certain distance they must be collect but then if you go down that road thinking it's completely defeated the whole purpose of them using the physical engine you're starting to really implement those collision algorithms yourself so most physics engines will have this idea of a collision listener so you can write a callback function or some create some type of object that sits there and tells you information about hey there's been an event these two things collided here's object one here's object two here's their properties you know what are you going to do about this like oh if it's a purple object with a red object it plays you know see if it's a green object with a pink object it plays the note D so you can you're going to want to look for something like a listener to be able to also not just extract where the things that you want to draw but to extract moments in time that a certain event like collision events that you want to handle in a certain way okay so I don't this is like a ridiculously long intro to physics engines tutorial video that I can't imagine anybody really wanted to watch but if you're still here thank you for watching and I think the other videos in this particular playlist will have much more information and actual code about how to do these things and how they work so I hope you asked them and I hope you enjoy them and I'm sure and I hope you leave me some comments feedback okay thanks very much and I'll see you [Music]
Info
Channel: The Coding Train
Views: 35,319
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: kRdw-Cm8BZ4
Channel Id: undefined
Length: 10min 48sec (648 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.