Godot Nodes Explained: 2D Joints

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
2d joints can be really useful when you want to make a physics-related game in godot plus they're just a lot of fun to mess around with so let's have a look at how they work and grow their three types of 2d joints you have the damp spring joint to the the groove joint 2d and you have the pin joint cd and they all inherit from the class joint city so let's start with a damp spring joint today so when you add this node to the scene you will see that it inherits a couple of properties from the joint2d class it has a node a and node b property these are the two nodes that will be connected together by the joints and then you have the bias this is how much the two nodes that are connected by the joint can pull on the joints and lastly we have the disable collision checkbox if we disable this then node a and node b will be able to collide so with that out of the way let's now have a look at how the spring joint itself works so first we need to create a node a and b if we have a look at the tooltip it says it must derive from physics body to d it basically means that it needs to be a kinematic rigid or static body so i'm going to quickly put together a static body with a sprite and a collision shape and then for node b i am going to create a rigid body with a sprite and a glacier hip so with that setup let's now move them to the center of the screen and let's move the joint to the center of the static body and then let's drag the static body and the rigid body above the damped spring joints so we can see its gizmo above the sprites and then let's change the length of the spring joint so it reaches the center of the ball and then let's make sure that the rigid body is nicely centered at the end of the spring joint so if we run this you will see that it bounces a little but to make the motion a little bit more clear let's rotate the spring joint and then let's move the rigid body to the end of the spring joint again then you will see that the motion is much more pronounced but you will also see that the spring guide actually rotates with the body attached to it you can visualize the spring by enabling visible collision shapes you'll see that it shows the spring and its length but it doesn't actually rotate with the spring what is actually happening is this the spring will swing around with the object attached to it so now we have that down let's have a look at the properties of the spring joint so the joint has a length which you can see about with the yellow line in the editor and it also has a rest length if the rest length is zero is the same as the length but if we increase this the joint will start at the length of 176 and then come to a rest when the length is 63 so that basically looks like this you can see that the ball starts at the end of the yellow line which is the length and then comes the rest when it's close to the green line which is the rest length when the bouncing slowly comes to a stop you will see that there still is quite a bit of distance between the end of the green line and the center of the ball you can adjust this distance by adjusting the stiffness let's reset the rest length and let's decrease the stiffness of the spring now if we run this you can see that the objects attached to the spring will stretch the spring much much further and if we do the opposite and increase the stiffness of the spring to a really high number you will see that the ball gets pulled to the end of the yellow line really quickly this also creates a lot more energy that's why it starts swinging more so the stiffness is not the only thing that affects the length of the spring if we increase the mass of the rigid body and you will see that this also affects the stretching of the spring so let's now reset the mass of the rigid body and have a look at the last property for damping i couldn't really think of a proper way to demonstrate this and i don't think i fully understand this yet so i recommend that you read the tooltip and mess around with this a bit yourself so before we move on to the next joint let me explain why we need to align the center of the rigid body with the end of the joint the end of the joint is actually where the joint grabs onto the rigid body so if we move it over it will grab the side of the ball and if we run it you will see that it indeed grabs onto the side of the ball and the same goes for static bodies so if we would move this over it will now use this point as the anchor points for the spring joint next up is the groove joint 2d let's add this node to the scene and let's move it into view and let's move it to to the center of the static body then let's rotate it a bit and increase this length and maybe tweak the rotation a bit so now we need to set the node a and b again we're going to set node a to the static body again and node b to the rigid body so you will see that it has two properties it has a length and an initial offset and we change your initial offset you will see that it moves along the axis of the joint the easiest way to visualize this joint is to think of it as an actual groove with an object in it so the object can move around freely along the direction of the groove but when the object reaches the end of the groove it can't go any further so the initial offset is basically where the object starts inside of the groove so now let's grab the rigid body and move it to the center of the initial offset we'll see that it will slide to the left of the groove because of gravity and just to prove my point let's try it as a different angle and a different starting point and there we go it will just slide to the right because of gravity the screw in the ball don't collide because we disabled that in the node settings you can enable it if you want and now it's finally time for the pin joint cd so let's add this joint to the scene and let's align it with the center of the static body again and then let's assign the static body to node a again and the rigid body to node b for this example i'm going to change the rigid body to a rectangular shape so we can play around with the weight distribution a little bit easier so similar to the other joints the placement of the rigid body matters where the joint grabs the rigid body let's quickly move the rigid body under the static body so we can see it over the other node and if we have it like this it will grab it on the left side and like this on the right side let's test this out as you can see it looks like the piece of wood is pinned to the static body and it spins on the point where we placed the joint so the pin joint city only has one property which is the softness which basically means how much the rigid body can flex from its original pinned position so let's increase the softness and when we run it you will see that the joint flexes a little by the weight of the blank let's have a look at the practical example for this in this project i've made a car using a rigid body for the car chassis and then for the wheels i've also used two rigid bodies and i've attached the wheels to the rigid body using two pin joints and i've also made the pin joints a little bit softer to make them a little bit bouncy and as you'll see when we run it it will bounce a little which could work really nicely as a car suspension let me know if you want a tutorial on a hillclimb racing style game like this now let's get back to the other project so another thing you can do with pin joints is you can chain them together let's duplicate the pin joint and then let's also duplicate the rigid body let's move it over a bit and let them overlap so now let's move the new pin joints to the location where we want to pin them together and then set node a to reverse vertical body and node b to the second and there we go if we now test this they are chained together nice another thing you can do is combine multiple joints together like right here i have a pin joint and i have a damp spring joint both connected to the same rigid body when we run this you will see that it also uses the spring joint and the pinch weight let's add another piece of wood to drop on top of it if we run this see it kind of works like a bouncy pad or something you can do a ton of fun things with these joints like for example create a marble run or an interactive chain using pin joints so it's basically up to you whatever you make with it i hope you learned something from this tutorial and feel free to suggest any notes you want me to cover in the next episode of godot notes explained i am tired i am going to bed goodbye
Info
Channel: LucyLavend
Views: 10,643
Rating: undefined out of 5
Keywords: godot, godot engine, Joint2D, DampedSpringJoint2D, GrooveJoint2D, PinJoint2D, Physics joints, godot game engine, game development, joints
Id: p6HaqSVbkUw
Channel Id: undefined
Length: 11min 47sec (707 seconds)
Published: Wed Mar 03 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.