Custom 2D Platformer Physics In Godot GDScript (Part 1 - Basic Movement)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments

Very cool, I will definitely keep watching this series! Thank you for making it and sharing it with us!

๐Ÿ‘๏ธŽ︎ 1 ๐Ÿ‘ค๏ธŽ︎ u/golddotasksquestions ๐Ÿ“…๏ธŽ︎ Apr 29 2021 ๐Ÿ—ซ︎ replies
Captions
welcome to this small series on how to implement your own physics system using godot engine uh the reason i'm doing this is because i started messing with godot and i found out that the built-in physics system is like a black box to me i'm not sure what's happening at any moment and i wanted something more [Music] i could say simpler for the game i'm designing and i came across an article by matt thorson the creator of celeste and he explained how they implemented their own physics system and the first concept they used is that the positioning and movement in the game are all done in integer numbers and the second idea is that every collider in the game is an axis align bounding box this makes things like collision detection and much simpler it's the first time i'm ever doing a youtube series so sorry and forgive me if it's not perfect and i still hope you are going to enjoy it nonetheless and enough talking let's go okay so to start things off we need some scenes so first i'm going to create a 2d scene i'm going to rename it to level and then i want to add a new scene but before that i'm going to save this to a new folder here i will call it scenes and save i'm going to create a new one and this one is going to be the player usually when you use the built-in physics system you're going to use the kinematic body but because i'm not using the um the built-in system we're going to use our own custom system we're going to just select a node 2d and i'm going to rename the node to d2 player and then i'm going to save this one as well so another scene we're going to use is a wall because we need something to collide with so it's also going to be a no to d and i'm going to rename this one a wall save it okay so let's start off with the player okay now we're going to need some sprites i added the link to the sprites i'm using they are free to use and now i'm going to copy them into the project and as i'm copying one thing to note here is after it's done importing just make sure that your sprites looks like mine which is pixelated because it's a pixel out if they are not like this one thing to do is to select all your sprites and then click filter off and do re-import and that should fix your issue okay so now i'm gonna delete this now we're going to add a new node it's going to be animated sprite and we need to add frames so click on that on friends in the inspector and do new sprite friends and click again and we don't need the default one the default state we can delete this and we're going to add a new one and you can rename this to idle now we're going to add the frames for idle i'm going to go into sprites individual sprites and i'm going to search for the idle sprites where are they ideal there we go and mark zero zero idle zero one make sure not select the idle two and take idle zero two and idle zero three so four frames open and this is the frames for the idle states you can you can see how it looks by clicking the playing and you see now it's moving and you can change the speed if you want also you can put 10 fps and now it's going faster as you can see i'm going to just leave it at 5 8 fps and we do want loop because it's the idle we're not moving so i do want looping and now we're gonna start writing the code to move the player but first of all i want to add the player to the to the level scene so i'm just gonna link it and i'm gonna select the player now is in our scene i'm gonna place him here for now and i'm gonna go back to the [Music] player and add a script and i want to add the scripts not to the sims folder but to the scripts folder so i'm just gonna create a new folder which is scripts open create okay so i'm gonna delete all this and the first function we're gonna use is the process function so i'm gonna use func process right now you should know what the process function do if you don't know anything you can just control click and you will get to the help files and you can read what it does okay so we're going to start off with the movement basic movement on the x axis and for that we need the few variables the first one is the velocity which will hold the speed of the player the current speed and it's a vector 2. and then we need the max run which is the maximum run speed and i'm going to set it to 100 and then we need the run acceleration just to run excel equals 800 and now so how do we start moving the character first of all we need to get the inputs right so i'm going to do var direction equals input dot get action strain which is the value between 0 and 1 and we want to get ui right minus get action strength ui left now this will return number that can be a fraction so we don't necessarily want that because we want in this game to move either left or right we don't have the slow moving speed or something like that so we need to add a sign here and what sign do is if the number is equal is equal to uh sorry if the number is positive then it will be one if the number is negative it will be minus one and if the number is zero it will stay zero so you can read about it here okay so now we need to move the character and how we're going to do that we're going to change the velocity velocity.x equals there is a function named move toward it takes three parameters the first one is the from value so it's velocity x our current value and we want to change it to what to max run but not just max run we need to take account of the direction right so we multiply by the direction so if the direction is zero we're not going to move right we are going to move towards zero which is good and if the direction is right we are going to move in minus 100 okay so next the third value is by how much by what is the delta so the delta here is uh run excel but there is a catch here if we move it by run excel it's a very big value it's 800 so the change will be so fast we need to multiply it by delta so it happens each frame a little bit right so we're going to multiply it by delta so now it's going to be smoothed out from velocity x to max run multiplied by direction next we need to move the character right so now we only change the velocity so to move it we need to change the global position dot x and add velocity dot x but then again uh if we do it like that we're going to add each frame 100 units it's too fast so we need to moderate this again with delta okay so now we're gonna check this i'm gonna press f5 and okay if you press f5 and you didn't select the scene yet make sure you select your first scene to be the level and click okay and then it should work for you and now you can see we when i'm hitting the the right and left arrows i'm moving but there's a small issue here because my character is always facing right which is wrong so to fix it close the game and now we're going to add a simple if statement but actually before that i want to add a reference to the animated sprite so we can flip it so we're going to use already animated sprite we call it equals dollar and it lets you select the component so animated sprite it's this one now we're going to do the if statement if the direction is greater than zero so moving to the right we want to do animated sprite dot flip h equals to false but l if direction is smaller than zero then we want to do animated sprite yeah got the colon here animated sprite dot flip h equals to true so now if we run it as we're working towards the right it's good and now the left awesome okay one last thing on this video that i want to add is that while moving i want to change to a running sprite so let's first add the sprite we're gonna go to the player scene animated sprite and we're gonna add a new animation rename it run click this folder add texture and we're gonna select all the running frames from zero zero to zero five open and it's a bit slow so change it to ten okay now we're going to save and go back to the script and now let's do a new if i know it's almost like this one but i want to separate stuff so on the new if let's say if direction is not zero we want to change the animation so you do animated sprite play and you give the name we want to run else direction sorry animate sprite play idle save it hit f5 to run the game and now you can see that we are on the idle when we are not moving and when we start to move we are on the run animation great so that's it for this video i hope you learned something if you liked it please hit the like button i'll be seeing you on the next one
Info
Channel: Game Code Samurai
Views: 12,549
Rating: undefined out of 5
Keywords: Godot, Platformer, 2D Platformer, Custom Physics, Celeste, Celeste Game, 2D Physics, In House Physics, DIY Physics, Godot Engine, DIY Physics Godot, Games, Game Coding, Game Dev, Game Dev Tutorials, Game Dev Guides, Coding Tutorials, Godot Tutorials, Godot Guides
Id: LkbEu4yxUNw
Channel Id: undefined
Length: 13min 9sec (789 seconds)
Published: Sat Apr 24 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.