Godot Destructible Body Addon

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
all right so uh today guys i'm gonna be going over my add-on that i made for uh destructible bodies so um in a couple of video games i i think this was most prevalent in crisis one if i recall uh but you're able to destroy certain objects um now this add-on will not do that it won't be able to dynamically say like destroy a tree and then like the tree falls over and stuff like that now this is more for specifically like concrete breaking apart um or you know any sort of chunky looking uh like rock uh breaking stuff apart not not necessarily full scale destruction but like you know just a little bit um so this is ideal for pretty low poly games uh since it can get taxing uh if you apply it to something with lots and lots of detail lots of polys uh it will not work all right so you're gonna get really bad performance if you do that but uh if you're going for you know kind of low poly or you're having destruction only on the lowest poly objects like you're pretty much golden all right so let's go ahead and see what this does here uh here we go uh and so i'm able to walk around and you see i had that cube fall and then it hit that and it was like it was that is a velocity-based destruction and so you're able to actually trigger um a break off chunk like this with velocity and then in this demo uh in my my github i will link that below you can also just click to kind of remove a chunk um you can also configure the size of the chunk that you uh remove and so you can see um i think the meme was that this looks like cake so um so yeah you see that we break part the cake now this is not perfect you see there's these little itty bits that are stuck around these are just going to be artifacts that you might be able to mitigate but it's going to be pretty difficult and yeah there's another one right there so i'd recommend like i said try to go uh if you're gonna use this make sure you use it in a relatively low poly art style where inconsistencies like that won't really make a difference also the collision is updated so i'm able to like stand where the party's chunk has been broken off and yeah here's here's this again i'm able to actually stand with my uh my player capsule here uh and in those little dents you can do it for all these objects here like this one has a bigger chunk that breaks out and then afterward it actually reverts to a rigid body of the original object so that would be ideal for something that you want to be able to move after you've broken it up and you can see all these chunks you know they they have full collision and everything and so you could have some pretty interesting game mechanics and yeah here's like you know really big chunks here yeah very very fun you know you can get stuck in there and then in this case i also have a limiting amount of chunks that you can break off because the more chunks you do the worse your performance will be and this is just a matter of the fact that csg was not designed to run in real time it was designed to run in the editor and then like you know be fully ready to go by the time you hit the play button and godot so it's not ideal for really large scale things that you know have lots and lots of bits breaking off so if you limit the number of bits the number of chunks that break out uh you should be good to go and plus you know once something looks destroyed enough it's passable if you keep on shooting it and it doesn't really react to anything anymore at some point so yeah there's a there's a compromise for you all right and then let's see uh in some cases you can choose to not actually have a chunk piece come out and that will save on performance you can just have just breaking away all right uh i'll show you actually the uh the collision shapes really quick did it just show you that yes this add-on does actually modify the collision shape fully and so that's why you know it looks it can look pretty good you can also adjust the launch speed you know how much the piece launches out from where it was at and that the mass of the the mass density will affect that as well and so it's all you know pretty physically accurate so and yeah see this is a this is a rigid body now but as you can see the indents are not actually concave because rigid bodies that are uh concave are are not supported they have to be convex shapes and so if you have your your object designed to uh become a rigid body and you know kind of get new physics after it's been kind of fully destroyed um that's what will happen and so now let's actually look at this so this is uh if you actually want to add it you know you can look it up uh i can't remember which one i i think it's just the the one without the gd um and that will give you the correct destructible body but it's you can see as you can see it's a subcategory of rigid body um so it extends from that and here we go over here you should be able to assign the uh the base mesh and that's pretty straightforward i tried to get everything running as smoothly as possible in the editor but it frankly is is not the greatest experience for example it doesn't have bounding boxes and so you can't actually select your destructible bodies uh in the editor i haven't figured that out and good oh if you know exactly what to do please feel free to contribute to my uh my my get lab branch and then you would be fully credited in the uh readme but yeah absolutely if you know something i don't please help me out um because uh yeah this is this is the first ad that i've ever made in good oh uh so yeah we're able to choose our base mesh uh for example we could do a new cube mesh and you see that updates live in the editor we have the base material and then we have the underlying material so you can think of the base material as say if we had a big building you want your base material to be that all those window panes and maybe the the roof and that could be uv laid out uh in in this case yeah these are all uv uh aligned uh textures and then actually my underlying material i made that in particular be um uh i made that uh in this case i made it uh uv based you could make it world trying to try planar base but be aware that if your chunk moves like the world triplaner will be kind of uh it'll cause weird warping on the textures and so you usually want you want to have uv and also i'll show you why it's able to stay so consistent so in our chunk mesh here i just have a really really simple uv sphere here and we have radial segments is five and rings is two you can increase that uh per object say if you have a highly detailed object i remember i i would recommend having a lower detailed chunks and if you have a low detail object you could have a little bit higher detail chunks on the mesh but the chunk mesh you generally want that to be low detail and then also you want it to be relatively round so that you don't end up with weird artifacts you know those little bits and pieces hanging out from before those will be exacerbated exacerbated if you have uh even more weird shaped chunks uh but these are going to basically be the spawn shape that the the csg logic uses to um you know get out of that uh all right and then so you can change that to whatever you want potentially uh if you just don't like you know having a ubc there uh and then chunk damping that will basically make it uh so that you know your your chunk might not immediately break away from the from the thing the same thing with chunk explosiveness so this the chunk damping is more of the rigid body um relevant issue where you know you have the the angular and and and uh translational damping on rigid bodies uh that's what that you know each breakaway chunk will have that applied to it your chunk explosiveness once again it's going to uh that's going to determine how much it you know that your chunk gets sent out um af after it's been made your chunk scale adjustment uh in this case you'll want it to be about point eight if it's one your chunk could just kind of stay in the slot and just vibrate uh continuously because it's like a perfect fit for where it was so i recommend doing a 0.8 scale factor um and that will make sure that there's no collision problems and then on top of that kind of looks like there's material broken away um your chunk density um that's kind of just determining how much each piece is going to weigh so you could have like you know your material is really dense it could mean that you know your chunk density is gonna you know your chunks might uh not be affected by other physics you know because it's so heavy right uh your chunk lifetime if this is uh i believe zero it will yeah okay if your chunk lifetime is negative one your chunk will never disappear but if it's two that means after two seconds your chunk is going to disappear and that way you can you know free up resources so that's a that's a pretty convenient thing for you um and then here's the debris instance so i just made mine just a little um uh quad particle and then i have like a particle material and so that means that it will like spawn just some you know random debris at that at the point of uh contact and that way it looks more like you know stuff is breaking apart with you know the or smaller stuff is breaking apart with the overall general chunk that's being taken out and so that'd be good for like you know if you had like a chunk of a building you know taken out you could have like a little bit of glass or um you know some other structural fragments that kind of fly out and then use inertia this this toggle will basically make it so that um if you have any sort of uh rigid body that's coming at your object with high speed it's going to it at a certain point with the inertial with the inertia tolerance um that means that you will have a chunk generated uh as opposed to like the click you know remove the chunk in this case i had the demo show where this cube falls onto the cake a piece of cake over here and that spawns a chunk because of the uh let me select that i know i i hate that i can't have it uh okay there's cake uh kate that can't have it selectable on the in the view because i can't figure out the bounding boxes issue um and yeah so we get the uh yeah we get the cake right here uh and it's yeah use inertia and the social tolerance is 1000 and so it's going to break apart when the mass of something that hits it times the velocity is 1000 or more all right so that's kind of uh that's kind of the theory behind that um and you can change the contacts reported and that will make sure that you're not kind of uh that that sort of thing can make it so that you you sometimes won't detect some collisions and so you want might want to make sure that's about five or six um definitely greater than two if you only have two contacts you could just have something that's touching two things and then it won't ever detect anything else again so um so you wanna keep that pretty high uh your particle's lifetime in this case you know that bigger debris um and yeah this this part right here has debris and so my particle's lifetime for that is uh one second so it's going to show the brief for one second and then it's going to kill it off and then here's the max destruction pieces and so for this object i have three um it can be i would say no more than 10 for simple shapes as just csg gets slow after a while and then set rigid on set rigid on max destruction so if you notice in the actual rigid body component of this it's set to static i'd recommend keeping it static doing anything else will make it behave strangely so keep it on static and then once it's done being destroyed with all the chunks it's going to set it to ridgid if that's enabled and that's pretty much the walkthrough through my plugin i can actually show you you know if you're not interested in the actual script feel free to leave but you know feel free to leave a comment if you have a question about how this works once again the uh the github is available in the description so feel free to check it out you can check out the source code for yourself but i'll just walk through this so upon entering the tree this is more of the editor side of things but i'll actually show you the where's the good oh yeah here we go all right so there's this function called destruct and so uh this is called when uh inertia is like this or it can be called from say your player character with a raycast you know going to a point and then you can call destruct on the object and so uh you can see i actually have a call deferred here and that's because yeah you we uh with csg it does not like playing on the main loop all right godot hates it all right so i had to basically jerry rig this thing to uh uh to fit in the uh deferred uh you know whatever the deferred method it can be and so that's gonna basically call it on a separate separate thread not the main physics thread or the main whatever thread um and so i emit a signal uh so you can connect that to something if you really need it um and uh all right so i you know go through all the logic so we basically take out the chunk uh with the we mask it out first so we create a new csg mesh and go through this and uh then we remove the mask from the base mesh and so that will basically eat off the original mesh and then we'll optionally call the spawn chunk and uh when we spawn the chunk it's going to basically do so with the previous one it basically calculates what it's going to eat away from the original object and then when we spawn the chunk we calculate what we want to keep based off of our our chunk mass all right from the original object and so basically uh if you have a sphere eating eating halfway through you know your your uh your original base object we just mask it out so that this part is all taken away from the base object but then the rest of the object is taken away from the chunk all right so that's kind of you know we we do the the same chd operation but uh one is a union versus the other is an intersection um and yeah and then we make sure we generate all the collision shapes um appropriately we had to uh in this case i had to get the convex shape from the csg um and with the csg there is a yeah so there's also get concave shape from the csg and so that way we can have a concave shape for the base but we have to use the convex for anything that becomes a new uh standard rigid body coming out of it um yeah and so here's the my my functions to get the concave shape or get convex shape it's just wonky mat so don't worry about that but yeah you should be able to uh this is hopefully somewhat readable it is com complicated because it has to be uh deferred and multi-threaded sort of so um but this is generally uh or this uh probably has room for optimization so uh if you are actually really interested in this feel free to contribute to the github i'm serious about that because uh obviously this is all written in gd script and it's probably not ideal probably better in c plus plus or gd native so feel free to contribute to that but if there's any uh other questions feel free to comment on this video i hope this was uh informational for you for those that stuck around uh to look at the code um and uh yeah hopefully you can use this in your game i would love to uh get a tag if you're already using this and you know just haven't credited it yet you know feel free to i'd love to see your work you know uh and i'll give you a shout out thanks everybody have a good one
Info
Channel: Toadile
Views: 2,200
Rating: undefined out of 5
Keywords:
Id: 9U0m_pXBto0
Channel Id: undefined
Length: 17min 3sec (1023 seconds)
Published: Fri Aug 20 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.