How to Apply Forces to a RigidBody in Godot (2D & 3D)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
so in this tutorial we're going to be having a look at the different ways you can add forces to rigid bodies using code we'll also have a look as how you can add forces within a given region using area nodes sounds good then let's get rolling [Music] here i have a couple of static bodies and the rigid body for the ball let's have a look at its properties gravity is the only force that's applied to the rigid body by default there's also the linear velocity let's set the x velocity to 1000 and if we run this you will see that it will start with an impulse to the right and in the same section we have damping this is how fast the object slows down we set this to a higher value you'll see that it slows down a lot faster if you set this value to -1 it will resort to the default setting the default for this can be found under project settings physics 2d and then the default linear damp so this is how you set the starting velocity of the rigidbody let's reset these values and let's have a look at the angular velocity similar to the linear velocity this is the angular impulse the object starts with so let's set this to 40 and if we start the game you will see that it starts with a rotating force and then slows down again and here the damping means how quickly the object will slow down with rotating now let's reset that and let's go into the last section which is applied forces these are forces that will be continuously applied to the rigid body the force is similar to the linear velocity so let's set this to thousands again and if we run this we'll see that it keeps being pushed to the right and let's reset that the torque is similar to the angular velocity but for some reason we have to set this to a really high number i'm not sure why if you understand the physics behind this please leave a comment because i would love to know if we run this you'll see that it will keep spinning but to make the motion clearer let's set it to an even higher number let's add another zero and here you'll see that it will spin faster and it'll keep spinning forever one thing you should really avoid is setting these values directly this also goes for setting the position this is because your code and the physics engine won't be in sync and this can create some unexpected behavior on the rigid body you can set these values safely in the integrated forces method i will have a link in the description to a video by kid scan code that goes more in depth on why and how but in this video i want to talk about the six functions a rigid body gives us to manipulate its forces for continuous forces we have the force functions which are add central force add force and add torque and for implying impulses we have apply central impulse apply impulse and apply torque impulse let's start with apply central inputs this let's attach a script to the rich body and in there let's add the process function in the process function let's add an if statement checking for if the ui accept action is pressed this is either the space bar or the enter key then in there let's add the apply central impulse function as you can see this takes a vector 2. the vector 2 will decide its direction and the length of the vector will decide its strength so similar to what we've done before let's set the x value to 1000 and the y value to zero now you can press the spacebar to push it to the right to make this motion a little bit more pronounced let's change the x value to 7 000 and the y value to minus 3 000 the y is a negative number because we want it to go up and now it goes up and to the right when you press the spacebar so with this out of the way let's move on to the next function which is apply impulse as you can see this function takes two factor twos an offset and an impulse so the impulse is the force itself and you could use the offset to offset the position where the impulse is applied an example of this would be hitting a ballyard ball on its side so it starts spinning more now let's set the offset to be more on top of the rigid body like the example i just showed then for the impulse force let's set it to 5000 on the x and 0 on the y if we run this you will see that it gets more pushed forward and starts rotating more you'll also see that the offset point doesn't rotate with the object itself so the last impulse function is apply torque impulse this function takes a float for the torque and torque needs to be a really high number so let's set this to 800 000 if we run this you'll see that if you press the space bar it starts spinning like crazy i really like how this looks so in 3d it's pretty much the same the only difference being that it takes a vector 3 as an input also note that these values are lower because in 3d you use units to measure distance and not pixels the apply torque impulse function also takes a vector 3 instead of a float this right here means that we will give a torque impulse of minus 400 on the x-axis which looks like this the value needs to be a lot slower than in 2d and it also seems to depend on the weight of your rigid body now that we've covered all the impulse functions let's move on to the force functions which are for adding continuous forces let's start with add central force so on our 2d rigid body let's add the ready function and in there let's add the add central force function as you probably expected it takes a vector too and similar to what we've done before let's set the x value to 10 000 and the y value to zero now you'll see that it will keep being pushed to the right forever so how do you cancel out this force you can cancel out the force by creating a force of equal strength going into the opposite direction to test this let's add the process function and the input check again and then let's add the add central force function again but then let's have it go into the opposite direction you'll see that the force gets cancelled out when we press the spacebar and if we press again then goodbye rigidbody so next up is the add force function this one works basically the same as the apply impulse function except this one is a continuous force so we will go on indefinitely so let's just fill in the same values as we did on the apply impulse function and when we run this you'll see that it will keep applying a force to the top of the ball yep next function the next and the last function is add torque which does what you would expect it adds a constant rotational force let's again set this to 800 000 and when we run this yeah that's what you would expect a consent rotational force and it'll keep going forever so you'd think that the ad force functions would work exactly the same with 3d the only difference being that they use a factor 3. when we run this you'll see that it doesn't it bounces and then slowly comes to stop this is because the add force functions currently do not work correctly currently the add force functions function exactly the same as the reply impulse equivalent don't worry there is still a way to add a continuous force to a 3d rigid body to do this let's add the physics process and then there let's add the apply central impulse function and then the function let's put the vector 3 with the force in the direction that we wanted to go and then we need to multiply the vector with delta this is to keep the distance it travels per physics frame consistent voila it now has a continuous force that pushes it into the wall we've covered all of the functions nice so now let's have a look at what we can do with area nodes i've re-edited the code from earlier that lets you apply an impulse on a button press let's go into the scene and let's add an area 2d node this area also needs a collision shape let's set it to how to select the parent so we can easily move it without selecting the occlusion shape and let's move it over here let's also do the same for the rigid body let's have a look at the properties of the area node here you'll see that it has space override option which is set to disabled but it also has combine combine replace replace and replace combine which can be quite confusing when there is no explanation for this explanation let's keep this property in mind the priority and also notice that it has its own gravity factor so the gravity direction and its own linear damp let's hop over to the documentation so disabled just means that it's disabled and it doesn't do anything combine means that it will combine the gravity and damping of the rigid body with the gravity and damping set on the area to these that it's currently colliding with and it will do this in the order of priority combine replace means that it will add the gravity and damping of the rigid body to the area to the that has the highest priority number and will basically replace all the area 2ds that have a lower priority number replace will just forcefully replace the gravity and damping of the rigid body and replace combine means that it will replace the gravity and damping calculated so far but it will keep calculating the rest of the areas i don't quite understand this last one but let's just move on let's set the space override to combine the next two variables are for setting the gravity to a specific point in space let's see ignore those two for now we'll come back to that later let's change the gravity so we're not technically adding a force to the rigid body we're changing the gravity but since we've set it to combine it will combine the gravity of the rigid body with the gravity of the area node so now let's set the gravity factor on the y to -1 re-run this you'll see that it will float a bit because it combines the gravity of the radio body with the gravity set on the area 2d right now we can't see the area so what we can do is go into debug and then we can turn on visible collision shapes we're under in it we'll see all the condition shapes and where the area is if you want to visualize the area without having visible collision shapes turned on you can either add a sprite what i like to do is just simply add a polygon to the we add this and then use the first tool to add points let's just click on all the corners to create a shape let's close it up and then let's set it color to blue and whoops we can set the alpha to be a little bit transparent so we can see behind it there we go we can now see it there are some other things we can set on the area to denote for example the gravity strength if we turn this up you'll see that it will get pushed up more we can also change the linear damp which if you remember is how fast the rigid body slows down let's also reset the gravity strength as you can see if we set it to a really high value then you can see that it slows down really fast and lastly we can also set the angular damping which makes the rotation slow down really fast in this scene i've given the area a round collision shape and i've turned on gravity point the tooltip tells us that if it's enabled the gravity is calculated from a point set via gravity fact so i've set my gravity factor to 0 0 for it to be in the center for point gravity there's also the gravity distance scale which says that the greater value the faster gravity decreases with distance so i've set this to zero right now so that when the rigid body touches the area it will immediately get pulled towards the center of the area and that will look like this the ridge body is still affected by gravity and will also be pulled towards the center of the area once it touched the collider and as you can see when it touches it it will get pulled towards the center so going back to the gravity distance scale let's increase it a tiny bit and if we run it you can see that it already falls off too quickly this is because i made my rigid body is too heavy so let's increase the strength of the gravity and let's test it again you'll see that it won't be affected when it's too far from the center but when it bounces closer to the center you'll see that will be affected in 3d they're just completely the same the only difference being that they use a vector 3 for the gravity factory and here to visualize the area i've just used a mesh instance with a custom material goodbye so you can use areas with an upwards gravity factor and damping to create something like this or you could use it to create something like those booster things and happy wheels so that was about it if you have any questions or suggestions on topics you want me to cover be sure to leave a comment i hope you have a great day and i will see you later goodbye
Info
Channel: LucyLavend
Views: 9,037
Rating: undefined out of 5
Keywords: Godot, Rigidbody, physics, addforce, apply impulse, torque, add_force, apply_impulse, Area, Area2d, move, 3.3, 3.2, godot engine, godot game engine, game development
Id: XSFkAzXQSWE
Channel Id: undefined
Length: 17min 50sec (1070 seconds)
Published: Wed Apr 07 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.