Intro to Graphics 24 - Physics Based Animation

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
so thank you all for joining another lecture for introduction to computer graphics um today we're going to be talking about as promised physics-based animation and we talked about animation in general last time and we said we're going to talk about physics-based animation in more detail and that's what we're going to do today now specifically we're going to talk about mass spring systems um because i wanted to talk about physics-based animation in a more specific context uh because i think that's going to make it a little bit easier for you guys to sort of understand these concepts now we're going to start with talking about it in more general terms and then we're going to dive right into mass spring systems and mass spring systems reason why i pick mastering systems is that they're probably the easiest simulation system they're also very versatile they're used for all sorts of different things uh so they're used very very they're useful in practice they're also probably the easiest physics-based animation that you can actually implement hopefully that will help you understand these concepts of physics-based animation quite well so let's dive in the physics-based animation let's start with animation all right so let's say that i have this relatively simple animation here i have this this ball and in the air and this is gonna fall down all right and hit this ground floor and it's gonna jump back up you see let's it's uh going down hits and jumps back up and then falls down again all right let's stop here um so this is the animation that i want to produce somehow so when i generate this animation the frames that i'm going to get will probably look like something like this right so i have a bunch of frames i label them with different times like time zero one two three fours and and at different times uh my ball is at different heights right it's coming down and then balancing and then going up now obviously the position of the ball at any frame is important and we're going to be computing that the position of the ball i'm going to label the position of the ball with the vector x uh and at every time step i have a different position so in parentheses i'm showing that the value at that time right so x the value at time step 8 is over here value at time step 7 is over there um along with these positions i'm also going to have to track the velocity of this this object because that's going to sort of determine where it's going to be in the next time step and that's why we're going to be computing so we also have velocities and those velocities are different we have different velocities at um every frame here right and these um positions and velocities this combined positions and velocities at any given time step is going to form my simulation state and i'm going to be keeping track of this simulation state at all times because this simulation state is what will allow me to compute the next frame and where my object is going to be in the next frame right so let's take a look at this simulation state a little bit a little bit more so at any time i have the position and i will also have the velocity at that time and the obviously the velocity is the time derivative of the position and this is a notation that's that's used in physics sometimes so it's um x dot is that means the time derivative of x all right so this means specifically that it's just a shorter way of writing this this derivative right so time derivative of x is the velocity um i am showing you this notation because this notation is going to come up later on when we're talking about other stuff all right so but super simple concept all right so what this simulation is going to do is we're going to be computing every frame from one frame we're going to compute the next frame so we're going to have multiple steps and every step with every simulation step we are given the state at in the beginning of the time step and we're going to compute the the states the positions and velocities at the end of the time step right i wrote that as like t and t plus one but it's it's typical to think about a time step as something that can be varied so let's uh replace this this one with delta t so delta t is my time step size and i'm given the state at t and i'm computing the state at t plus delta t right so that's the that's the idea that's what i'm going to do with pretty much any simulation this is this is what we do any physics based simulation graphics this is what we do and uh for that you know we could use also the different simulation systems uh what we're going to do what we're going to talk about today is a relatively simple simulation system and a very very common way of uh thinking about this simulation so we're going to be using newtonian physics so it's going to be relatively simple right so nifty newton's first law of motion what does it say it says if there is no external force an object that's moving is going to continue moving right an object that stationary is going to be stationary so basically if there's no external force then the velocity of the object does not change right if there's nothing impacting an object it's and if it has some speed this this is vt it's going to continue with that speed so it's speed is not changing within the time step right very very simple if there's no external force velocity is constant now if the velocity is constant the object is going to be moving with constant velocity right if the object is moving with constant velocities position is going to change and this position is going to change exactly like this so this was the original position at the beginning of the time step so delta t times velocity this is constant velocity so position i'm adding time step times velocity i get the position at the end of time step fairly simple right and it's all coming from hitting's first law of motion now if we wa but this only happens if there's no external force oftentimes we're gonna have external force like for example gravity is some sort of external force if we have external force we need to think about newton's second law of motion now if there is external force then the external force will will apply some acceleration to the object so it's going to change its velocity that means it's going to have some acceleration and that brings us to the very famous formula of f is equal to m a so the force is equal to the mass of the object times acceleration right and we can use this formula given the force we can use this formula to compute the acceleration so that's exactly what we're going to do and going back to our notation acceleration is of course the change in velocity that's the time derivative of velocity which is the second derivative of position right so this double dot um yeah it's just just notation all right so if there is external force we're going to be using this formula to compute acceleration how we're going to do that very very easy right just uh divide the given force by the mass of the object and there's our acceleration if there's acceleration that means it's the velocity the object's velocity is going to change right and the velocity is going to change like so the velocity at the end of the time step is going to be equal to the velocity the beginning of the time step plus time step size times the acceleration right and using using this we can also write the um the position now the position turns out to be this equation so it's impacted by its velocity it's also impacted by the acceleration so this is this becomes the equation of position but this these equations only hold under one condition one very important condition that this external force is constant if the external force is constant these are equations now can you think about constant external force for example for an object with constant mass pretty much all objects like for an object with constant mass a constant external force is going to be gravity right gravity is going to be pulling an object down and that's going to be a constant force applied to an object so the easy question is actually holds for an object that's in free fall this is these are the exact equations for object and free fall newtonian physics all right so if the external force is not constant if the force is not constant force applied to an object is not constant if it's varying then things are going to be a little more difficult now this force is not constant so let me uh write it like this like force is varying in time so with the force at a particular time uh satisfies this equation and i can compute the acceleration at a given time if i know the forces at that time but the if the force is changing then the acceleration is changing right acceleration is not constant anymore if the acceleration is not constant the exact equation for the change in force and change in uh position so our central velocity and position is going to be these integrals right if the acceleration is changing then i need to integrate the acceleration from the beginning of the time step to the end of the time step and that's going to give me the change in velocity and then i need to integrate velocity from the beginning of the time set to the end of the time step and that's going to give me the change in position right so these are the general equations they always hold however you know how am i going to compute these integrals so it all depends on this the sports formulation right if those forms formulation is something simple then i can actually compute these integrals and find out find out what these equations are exactly supposed to be but oftentimes unfortunately even with a relatively simple simulation scenario these these force formulations they will be a little too complicated for us to be able to solve these equations all these intervals easily right so we're not going to worry about that in most cases for most simulation scenarios that we're interested in computer graphics this force formulation is going to be way too complicated for us to just uh go and figure out what this integral is supposed to be so we're not even going to try to do that what we are going to do is instead of trying to compute these integrals we're going to sort of try to estimate the results of these integrals and the way that we're going to estimate the result of these integrals is we're going to use some some numerical integration treatments so we're going to numerically try to estimate what the the result of this this integral is supposed to be right numerical integration is the most important thing when it comes to physics-based animation so let's let's talk about that now there are a whole bunch of different numerical integration techniques for estimating this integral as accurately as possible and as efficiently as possible too so this can be very very expensive to compute so what we're going to talk about is going to be the easiest one of them all the uh the early integration is known as the the easiest integration technique and it's very very simple you know what i'm going to do you know that integral that was here i am going to estimate this integral as um just a constant i'm going to assume that it's uh you know what i'm going to assume that this acceleration that i compute at the beginning of the time step does not change i'm going to assume that the force here is constant i know it's not constant but i am going to assume that it is constant because if it is constant then my velocity update becomes this all right super simple now i technically i estimated that integral yeah this is a very simple estimation it's probably not accurate not very accurate and it's not going to be very accurate in a lot of cases and it's only accurate if this force is constant and oftentimes it isn't so you know it's going to be a solid estimation but it's probably not going to be a very accurate estimation but it turns out it works actually if this delta t is sufficiently small this turns out to be an okay estimation because if the time step is really really tiny the change in acceleration the change in force becomes very very tiny as well so the error you make by estimating that it's constant it becomes okay in practice so not necessarily a bad idea but as i said this is the simplest integration technique we could do better but we're we're really not going to all right early integration assumes that the force is constant now the other integration when it comes to computing the position it's going to have a different a different assumption it's going to assume that now that the velocity is constant now velocity is obviously not constant i'm changing the velocity here but i'm going to assume that it is constant because if it is constant then this becomes my position update very very simple right so now i estimated this integral as as a constant and again you know this is not a very accurate estimation but if if this delta t is sufficiently small it's going to be okay it's not going to it's not going to be too bad right so that's the that's the idea now this is what we call is explicit early integration now this is called explicit integration because here i can compute the acceleration explicitly using this force now this force at time t is is something i can actually compute just fine now i'm not telling you how to compute it just yet we're going to get into that we're going to get into force formulations and how to compute different forces but this is going to be something i can easily compute because i know the states at and i know the positions and velocities at time t that's the beginning of the time step so i can explicitly compute this force that means i can explicitly compute the acceleration and using that acceleration everything is explicit here and that's why it's called explicit error by integration there is also a different form of airline integration looks kind of similar but actually very very different now pay attention to this now i'm going to change these t's with so t's with t plus delta t's now what's happening here is it's very very similar as you can see it's still early integration but this is what's called implicit error integration because i'm updating the i'm not taking the position by using the velocity at the end of the time step not in the beginning of the times about at the end of the time step right that's what i'm doing here and here i'm updating the velocity using the acceleration at the end of the time step now i would like to do this and you may think that oh this is as inaccurate as the other one and and it's true that it is probably as inaccurate as the other one but but in practice this does something very very different this becomes numerically a lot more stable so simulations using this sort of formulation they again have numerical error but that numerical error uh what what it does is it sort of slows down the simulation slows down the animation the the system loses energy which is okay you know i have some motion it just dies down oftentimes i want my animation to sort of take her off and die down anyway so it for a lot of graphic simulation this turns out to be something that i actually want even though it's it's still inaccurate but the explicit integration the one i showed you earlier here this explicitly integration again this is inaccurate but this inaccuracy adds energy to the system uh in practice and by adding energy to the system is your numerical uh integration sort of explodes um you'll you'll see examples of that just take my word for it for design being this is a lot better but now here's the here's the tricky part to be able to compute the accelerations at the end of the time step i need to know the forces at the end of the time step now i cannot compute the forces at the end of the time step using the states at the beginning of time step i need to use the state at the end of the time step now there is a little bit of a chicken and egg problem here to be able to compute this force i need to know these but to be able to compute these i need to know the force now that's the chicken and egg problem so this becomes quite difficult to implement in comparison to explicit earlier integration oftentimes this forms and a series of equations and and we solve that implicit system using some numerical methods so that we can sort of estimate the positions and estimate the state as we're estimating the forces together so this forms a giant implicit system that we saw because of that implementation of this becomes a lot more difficult so it looks very similar but implementation wise it's a lot more complicated but there is something sort of in between the two that's what we call semi-implicit early integration and this is a very very popular thing to do with semi-implicit integration i am explicitly computing the velocities but then i'm using the implicit integration formula for positions and this is very easy to do because because i can compute the acceleration explicitly using the current state i compute the forces i got my acceleration i got my velocity and once i have my velocity i might as well just use that velocity for updating my positions right so this is super easy to implement as easy as explicit earlier the semi implicit earlier is as easy as explicit earlier and it turns out to be um somewhat more stable than the explicit early integration numerically so this is oftentimes preferred the only difference if you compare it to the explicit euler integration is that you know i changed this term right instead of using velocity at the beginning of the time step i'm going to use the velocity at the end of the time step right so this is the explicit integration all right so i i think now we're ready to talk about how we compute this force all right so let's start with the simplest force the gravity force all right uh so the gravity force is going to be a constant force as slow as the mass is constant and the gravitational acceleration is constant so f g f gravity is mass times the gravitational acceleration so gravitational acceleration is sort of constant at any one place on earth and mass of an object is often times constant so this becomes a constant force right so this is a vector multiplied by a scalar so it's directly pointing down towards the earth or whatever planet you're on i hope you're already all right so this is the the simplest uh force now the force that we're going to be using for our mass spring stimulation is going to have a different formulation that's going to be a spring force more specifically linear spring force now this is the formulation of a linear spring force it looks a bit more complicated but it's not really that complicated so a spring is something just like a spring that's attached to our object and the other end can be fixed or can be attached to some other object it doesn't really matter let's assume that it's fixed for the time being now the spring force is can be computed for for linear spring can be computed using this formula so the spring force over here so there's going to be this constant factor the spring coefficient or stiffness i represented here with k and that's going to be multiplied by the difference in length so the spring length minus the rest length of the spring so the spring is going to have a rest length and if you if you change the length of the spring if you compress it or extend it it's going to exert some force to try to pull the string back to its rest length so as you can see here if the spring length is exactly the rest length this term over here becomes zero so that becomes the force becomes zero and the force direction is going to be along the direction of the spring all right whatever the direction is now in in this formulation i pick this as the force direction as the spring direction uh so if i pick this as the spring direction then there's going to be a minus sign over here that basically becomes the same force so um obviously what happens here is that if i extend the spring it's going to apply a force pulling the object back such that the spring length will be moving towards the the rest length similarly if i compress the spring it's going to exert a force in the opposite direction it's going to try to extend the spring right so you know this is our our spring and if i simulate an object like this let's say that i don't have any gravity i have an object like this in space attached to something fixed if i compress the spring and just let it let it run then i i'm going to get some motion like this i did object it's going to go back and forth back and forth i'm applying some some force because it's compressed uh and as it's extending it's it's getting the force applied in the opposite direction and it's going back because of its velocity it's sort of overshooting and and this never ends it keeps doing that now if i'm computing this numerical integration accurately this is what it's supposed to do um and oftentimes this is not very desirable yeah oftentimes because that's not what happens in reality in reality what happens is that if you have an object like this if you have a system like this it's not going to keep going like this forever right eventually this is going to stop because in reality we have a lot of other things going on we don't have perfect systems where everything's isolated and such that the system never loses energy in reality it's going to lose energy right um some of the energy of this motion is going to turn into heat and then other things and so this motion is going to slowly die off that's what happens in reality um so to be able to get a spring that does that that doesn't infinitely go like this forever to be able to sort of stop this motion at some point we're going to apply what we call a damping force a damping force is going to be and another type of spring force uh and the stamping force its whole point is to slow things down depend on the velocity of the object more specifically the speed with which the length is changing and this is the formulation for the damping force l dot here is the time derivative of the length so this is that doesn't depend on the rest length it's just the time derivative of the length how fast the length is changing so if the length is changing really fast it's going to apply a stronger force if the length is changing slowly it's going to apply a weaker force right so that's going to be our damping force formulation as you can see it's fairly fairly simple okay uh so we've seen some forces i think we've seen enough forces i think we're ready to start talking about our target physical base system that is a mass spring system now before i go into the details of mass spring system i want to show you something that i i've shown you last time remember this video so here in this in this animation is this cloth and taurus models these donuts are actually simulating using mass spring systems uh so you know you can get all sorts of really interesting animations with masterpieces the fluid is not mass spring system it's different in this case this fracturing wall is a mass spring system too so you can do all sorts of interesting things with mass spring systems it's a very versatile simulation model we're not going to be generating animations quite like these but actually what you will be implementing as a part of our last project is something that could be used for simulating stuff like this is they're not very different the only part that's going to be different is going to be part that you're not going to be implementing the the integration part is going to be a little bit different to be able to get simulations like this but they're actually very very similar to what you will be implementing and it's going to be relatively simple you'll see so a mass spring system is going to have springs big surprise and it's going to have point masses it's important that there are point masses though i'm i'm representing them as like balls with some size but uh physically the the equations that we're going to be forming will be assuming that these don't have any size there's going to be these are gonna be just points right but you know you can't see a point so that's why i'm representing this as these spheres so point masses enemy springs that's what makes this the system really really uh simple and these types of point masses are used a lot in computer graphics for all sorts of simulation systems now in this uh simulation example i showed you a little bit ago as i said the cloth and the wall were using mass spring systems and the flute was not using mass spring system but it was still using these point masses for the fluid simulation and we call them particles in general and you know there are a whole bunch of different particle-based simulations in computer graphics i'm going to tell you a little bit about them next time uh i'm going to be presenting different forms of physics physics-based simulations using computer graphics and and mass spring systems are just one way of using particles one with one particular particle-based simulation system so these point masses are particles and the springs are basically connecting those particles so each one of these particles of course will have their their states i'm going to have position and velocity for for both of these particles and if i know the positions and the velocities for these particles that are connected by a spring then i can easily actually compute the spring force here right spring force let me write down the formulation the same formulation i showed you a little bit ago it's it's this right so it's the same formulation the spring force applied on particle zero here is equal to this this formulation where l the spring length is just the the length of the vector that's the the difference between these two positions right so it's a vector from here to here it's just i'm looking at its length um l is going to be its length and this direction vector is going to be in this case the vector from here towards this one so this is going to be my direction vector g all right and this is the formula now if i'm applying a force of f 0 s on on this particle yeah as you would expect i'm going to have an equal and opposite force on the other end of the spring right so i'm going to have a force applied to the this other mass and that's going to be equal to the opposite of of this force f zero all right so if you compute f zero uh s you can compute f one s easily right so that's the formulation for the for for the spring force and of course um you know the direction of the the direction of the force depends on this comparison between the current length and the rest length like in this case the force is pulling the spring uh pulling the particles together that means the spring was extended that means this rest length was was shorter than its current length here if the rest line was was longer the spring would be applying forces in the opposite direction we will try to extend extend the spring to bring it closer to its wrist length right so this is the formulation for the spring force and my damping force is going to be you know fairly similar again the same formulation it depends on the speed with which the length is changing and i can compute the speed with which length is changing with this formula i compute the difference between the the velocities and i do a dot product of that with a spring direction and the reason why i'm doing this dot product is that because i want to know the change along the spring direction right i want to know the speed difference along this direction i don't know if there's any speed difference in some other direction i'm just interested in this spring direction because that's going to give me how how fast the length of the spring is changing right so that's why i have this dot product right so that product of two vectors is going to give me a scalar and that scale is going to be in speed and again the spring direction is the same direction all right very very similar to the spring force computation it's just i just have this doppler left here and i'm using the velocities that's really the only difference here all right so that that's really it that's the spring mass spring system so in a mass spring system i'm typically going to have more than one mass i'm typically going to have a bunch of masses connected by a bunch of springs and these springs can be you know connected in all sorts of different ways uh and and you know it's how these springs are connected and how these spring uh stiffness coefficients are defined we'll sort of determine what kind of system that we're seeing so if you're using some cloth simulation you're going to be using different kind of spring connections if you're simulating um a a brittle object and fracture you're going to be using different types of springs with different stiffness right but the underlying concept is going to be the same so since we have a whole bunch of objects here in the mass spring system our state is going to contain all of them right so if i have n mass particles that means my my position state is going to be this vector that contains all of them and my velocity is going to be this vector contains all of the velocities of all of my particles so now i'm going to talk about how one could implement a simulation using mass spring system right so a general simulation is going to start with some initialization so first we're going to initialize the simulation how am i going to initialize the simulation i'm going to give i'm giving out a bunch of particles and positions and velocities and typically i'm going to say you know in the beginning of the simulation there's no motion all the velocities are zero and particle positions are exactly what's given to me that's basically simulation initialization initializes state velocities are zero and the positions are whatever that's given to me and then for each time step i am going to do the simulation step the simulation step is going to compute the next frame the positions and velocities in the next next break it's going to compute the next next state and then i'm going to take that next thing i'm either going to display it or i'm going to just record it on this so if i'm using a real-time simulation for video games or something i'm going to be displaying that uh if i'm doing an offline simulation that's going to be used in the video i'm going to record that stage that's the basic idea that's what we're going to be doing so the part that you guys are going to be implementing as a part of our next project is going to be the simulation step part right so we're going to talk about this part in a little more detail in a little bit but i want to show you what something like this might look like in a javascript form so this is not the exact code that we're going to be using it's not the code that i'm going to give you for the upcoming project but yeah it's just sort of representative of what's going on just to keep just to be a little more specific i would like to show you this so i'm going to start by initializing the positions which is whatever that's given to me calling some function that does that let's say that i've implemented something like this and then my velocities are going to be the same length of course and they're going to be zero like i fill it with zero initially there's no motion that's very something difficult to do you don't have to do that this way you can initialize the simulation differently but typically we initialize it as like no motion in the beginning of the simulation and then you know i'm recording the first frame though this is my beginning frame i'm recording this i'm recording the positions because probably i don't need to record the velocities because i need to know where the object is i don't need to record the velocities sometimes i do sometimes i don't sometimes my rendering system may need those velocities for motion blur and things like that but if my system doesn't require that then i don't have to record it and then i'm going to compute my time step my time step is going to be the frames per second that i want to generate my video or in my live real-time animation whatever frames per second that i want my time step size is going to be one over that in seconds all right and then for each frame that i want to compute i'm going to call this simulation step just advance this state by delta t and then record next frame right that's that's the idea right so this is this is like offline simulation right i'm recording the frame if i want to get something like real-time live simulation i could i could do something like this for example i you know initialize it and display the first frame and again compute my time step size and then i use that time step size and convert it to a milliseconds i use this window the set interval method so it's calling that function every so many milliseconds and in that function i am just advancing the simulation and displaying it displaying the results right so that's um that's a very typical way of handling this so the the heart of the simulation obviously is this simulation step part so let's take a look at that in a bit more detail so this is why we talked about the about simulation step i've shown you this slide before so we're given the states at the beginning of the time step we're going to compute the state at the end of the time step and when i have n particles this is going to be what's given as the the state and i'm going to be computing the next state with using semi-implicit over integration for example let's let's take a look at that uh first we're going to start with computing the total force on each particle now now this is this is important you need to first know all of the forces the total force applied on each particle you cannot consider the forces one by one that's going to be all right i shouldn't say you cannot it kind of makes things uh quite a bit more difficult so what we typically do is we first finish computing all the forces and then we do we do velocity updates velocity and position updates so compute all of the forces after i'm done with computing all of the forces acting on all of the particles then i can use that force to compute the acceleration and using the acceleration i can outdate the velocity and using the updated velocity i can update the position so this becomes the typical semi-implicit early integration all right so um you know what what we typically do in a simulation system is that oftentimes we don't get a state and then return a completely separate set separate area of states we typically get a state when we update the state right we get velocities we change the velocities that's what we typically do because i oftentimes don't need the state in the beginning of the time step anymore i'm done with that i just need the new time steps right so typically what happens here is instead of i'm just going to write in a simpler form basically the same thing i'm updating the velocity right so i updated the velocity by the acceleration that i computed and then i'm updating the position by the velocity that i computed so this becomes because the velocity updates happens before position update this becomes semi-implicit or integration right and if i want to do explicit error integration i need to swap them so i first do positions before i change the velocities and then i change the velocity and then i change the velocities so this becomes explicit formula right so again this was semi-implicit earlier very easy to implement and this uh this is explicit alright so as you can see here blending semi-implicit error is as easy as implementing uh explicit error so you might as well do that now the question of part here is that how am i going to compute these forces that's going to be important now we typically start by saying all the forces are zero and no force is acting on anything and then i'm going to add gravity i apply gravity for each particle i'm multiplying the gravitational acceleration with the mass of the particle well fi in this case is zero so you could probably omit that and then for for each spring between particles i and j i'm going to be computing the spring force and the damping force and i'm going to add the spring force and the damping force to both particle i and particle j of course in particle j i am using the negative the inverse of this force right that's that's about it joe this is how we compute forces and this is the stuff that uh you will be implementing for the upcoming project except that there's one more thing one more thing that's going to be important that's going to be collisions now without collisions it's kind of hard to keep an object in place if you apply gravity and there are no collisions the object is going to keep falling so you want to keep the object into contained in some space we need to apply some collisions right the object needs to collide with something so it stays within some within some limits all right so the concept of collision is relatively simple but actually it's a very very complicated topic and there are so many details in here for example now what happens with the collision is that i'm thinking about a particle colliding with some stationary objects like it says falling down and it hits this object right so the first thing that needs to happen here is collision detection i need to know when this happens i need to know when this happens and then once i detect collision i need to know what to do with that collision i need to know what am i supposed to do once i figured out okay there's a collision but that much now that would be the collision handling part now depending on the simulation model that you use this stuff can both of these can be relatively complicated now that what we're going to be implementing for our outcome project is going to be relatively simple but this can be complicated things to implement right but joe once we figure out how to handle collisions we sort of expect this object that tell on the floor to go jump back up right so if you implement collision handling properly it's going to be jumping back up so if you look at this frame by frame in the beginning your object was in this position and next time step it fell fell down a little bit and down down a little more and a little more a little more and finally it hit this this ground plane now in this case we were very lucky we were super lucky i'm reasonably lucky actually because what are the chances that the exact time points when this ball hits this plane is exactly at this frame four right you know the object did not have to be exactly there it could be just a little below right or it could be just a little above that would be maybe a bit more difficult well in that case there would be no collusion at this point and if the object continued then the collision would happen over here actually the exact time of the collision would be in between these two frames right the exact time of the collision would be over here now depending on the type of simulation that you're implementing it might be vital to find exactly when this object collided with this with the floor it might be vital to find this exact time in between these frames when this object collided the floor but sometimes it's not that important for our implementation we're going to pick that an easier route and we're not we're not going to care when exactly this happens we're just going to detect it after it happens so after this object goes through this collision that collision point that's going to be the ground plane we're going to say oh collision effort the object collided i only know that the collision happened before this time step in between these two time stats now i'm going to dissect that during my time step integration after i computed the new positions so by my simulation step i'm going i'm given this state and from this state i computed this state and then i checked for collisions did a collision happen if a collision happened then you know i'm going to handle it but let's talk about collision detection first how am i going to figure out that this happened well let's say that this is a plane and there's an axis aligned plane and it's aligned with the z-axis right so it's uh its normal direction is the z-axis here and this plane over here is at z is equal to z zero some some value and the yeah i've been using x to represent the position of my object here so x z is going to be the z coordinate of my object's position if this test is true if the z position is you know less than z zero that means the object sort of went inside this plane fairly easy check right in this specific case it's fairly easy it becomes slightly more difficult when my plane is not anticipated it becomes a bit more difficult than that if it's not an infinite plane but we're not getting into that we're just looking into evidence of this simple case all right so in this case my object actually penetrated inside this display and it wasn't supposed to do that so i kind of need to figure out a way to take this object and get it out right it's not supposed to be below this plane uh it's not supposed to go through the plane so my time integration which did not consider collision found that the next position at the next time step was supposed to be here but no this is not a valid position so i'm going to move it out so how am i going to do that first i am going to figure out how much the object penetrated this collision wall floor in this case so this h here is the length this this length is of course the z coordinate minus d zero right and i am going to take this object and let's say that um my my collisions are handled fudge that this object is going to bounce off this bounce right back up all right so it's going to bounce right back up by some amount let's call it h prime and h prime is going to be this length is h prime is going to be r times h is this value and r is my restitution coefficient uh so this sort of models what percentage of the object's energy is lost so r is is supposed to be always supposed to be less than one or maybe one is okay that means it's preserving all of its energy just bounce right back up it almost never happens in reality so you just multiply by some restitution coefficient that sort of models the amount of energy the percentage of energy that is lost during this collision event all right so this is how i can compute the updated position of my object at the end of the time step the updated position of my particle at the end of the time step of course i'm going to have to modify its velocity too because the velocity over here was still down right it was still moving down why wouldn't it but after it collided with with this plane my velocity now is going to be pointing up right and it's going to be the updated velocity i'm representing it as v prime is going to be minus restitution coefficient times the original velocity so it's minus sign just flips the velocity in z direction and restitution coefficient sort of determines what percentage of that velocity is is preserved all right so that's the that's the idea for a very simple collision handling model that we're going to be using as i said we could do more more complicated things here but this is definitely good enough especially for particle-based simulations you'll see that this is definitely good enough uh are we doing any air resistance or anything like that or just keep it simple yeah we are going to keep it simple air resistance um it's a more proper term for those will be drag and lift forces properly computing them requires some sort of fluid dynamics or there are ways to sort of estimate them without fluid dynamics and oftentimes we ignore them because for for a lot of cases they won't matter too much sometimes they are very very important uh sometimes they're not all that important so we don't we don't always consider that and in this case we're we're going to ignore all that additional complexity right so now there's not going to be any air in the vacuum they're going to be the vacuum there's going to be no air or anything impacting the motion of the objects the other thing that i would like to stress here is that this was an active line plane because it's an accident line plane it's only going to change the z component of the velocity the x and y components of the velocity for for this collision plane they're not going to be impacted now if you apply friction there's got to be friction if you're hitting something then it's going to change the velocity in the perpendicular direction as well oh another typical model for handling things like this is instead of you know reflecting the the object position like this and just letting it bounce often times we treat the distribution coefficient as zero for by updating the position this is called position correction so we take this and just put it right over here right over here i don't you know reflect it and go all the way up and just pull it right over here where it's the barely touching the collision object that's what we call position correction uh sometimes we don't we don't use position collection uh sometimes we attach a spring between this this plane and the object so there are different ways of handling collisions this is just one way of handling collisions i'm thinking this is relatively simple to implement and it's going to be good enough for our for our upcoming project that's why i'm talking about this in detail but yeah we have different ways of handling collisions different ways of detecting collisions for all sorts of different simulations now if i have an infinite plane it's very easy to detect collision but if i have a finite plane it becomes a bit more difficult to detect collision because my object can go through the plane and because it's a finite plane i may or may not know if the object went through the plane just by looking at his position i may need to look at his trajectory and that's what we call for example continuous collision detection so you'll look at the continuous motion from here to there and that's how we try to find exactly when and where the collision happened so the collision detection and handling can be a very very complicated process but what we're going to be implementing for our next project is going to be probably the simplest one the this one that i just described all right so now this is what i plan to do for today but if you guys want to see it i think i could do a sort of live demo of our final project would you like to see it i already recorded the video and you can watch it but all right let's let's do a live version and see what happens okay and there's our project all right so we're starting with a teapot uh this is a mass spring simulation of a low resolution teapot uh feel free to use a high resolution teapot for for this project and you're going to see that it's it's not going to perform so well it's going to be too high resolution probably all right let me reset all these values and reset the simulation and start the simulation so the object is falling down and bouncing off so we had a wiggly jiggly teapot and you know you can click any of these vertices any of these vertices and drag it around and it moves like this it's bouncing all over the floor and it's also bouncing off of the the other walls as well i can make it a bit more stiff let's see ah even more stiff all right i'll reduce the time staff just a little bit and increase damping there you go all right so this is what the sort of finished project will look like okay uh so we have a whole bunch of parameters here let me play with them a little bit now if you increase the mass uh let's reset it if you increase the mass these spring forces are going to be a little weaker in comparison so let's let's see this with yeah this is good let's see the sort of default settings here is wiggly jiggly and if i increase the mass uh the spring forces are not going to be as strong so it got inverted oh no all right what do you say so it doesn't impact the amount of gravity uh the the acceleration due to gravity because you you'll multiply by mass and then divide by mass but sorry it impacts the mass sort of impacts how the spring forces are impacting the the object if i reduce the mass too much it's going to be unstable it's going to should explode oh there it is it exploded let's reset and oh it exploded and it exploded all right if you don't want it to explode that easily you can reduce the time step size and that's going to make things a bit more accurate so the you know the earlier integration explicit or semi semi-implicit oh integration is uh going to be more accurate more and more accurate actually any kind of type of time integration going to be more accurate when you reduce the time step size i can reduce it all the way down to a single one millisecond here uh in this case it you know visually slow down because my computer can't quite keep up with the speed with which it's supposed to simulate uh if the time step size is one millisecond that means it needs to recompute a new frame every millisecond and it cannot quite do that it takes more than a millisecond in javascript to compute this the simulation but if i increase it just a little bit then you know it starts catching up so hasn't slowed down anymore all right so let's reset this all right so there's going to be all these parameters and you know you guys should um play around with it and see what you get uh that's that that's the idea right all right so uh that's all i have planned for today we covered everything that you guys need for implementing the finder projects i'm very happy to hear these just to see these nice comments thank you thanks
Info
Channel: Cem Yuksel
Views: 1,116
Rating: undefined out of 5
Keywords:
Id: F9TP48yXs3s
Channel Id: undefined
Length: 57min 23sec (3443 seconds)
Published: Sat Nov 27 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.