Love2D Physics (windfield) in under 10 minutes

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello folks in this video i'm going to cover the basics of using physics within love2d and how we can quickly create colliders that interact with each other and optionally respond to gravity you don't need anything to get started with this any love2d project will do even a brand new one if you're following along with my love2d basics series this video provides some extra information we need to cover before continuing on with that now for physics there's a module for this built directly into love and it is very powerful unfortunately that means it's also complicated but there are open source libraries out there that make working with physics a lot easier the most popular is winfield you can find a link to this repo here in the description winfield provides a bunch of really easy to use tools for us to use that make getting started a lot quicker and your code will be easier to write i personally use this library for all of my games even though it has been archived recently it is still fully reliable with the latest versions of love and it really does make a huge difference to get this code included in your project you can either clone the repo if you know how to do that or you can go ahead and go to code and download the zip then you'll just want to extract the folder and then inside we want to find this winfield folder that's right next to the readme so go ahead and copy this and then navigate to your game project folder in my case i'm using a brand new project for this demonstration it's just a main.lua file i'm going to create a new folder i'm going to call it libraries since i always put my open source code in a libraries folder i'll go in here and then i'll paste in that winfield folder now go ahead and open up your main.lua and for this demonstration i'm starting with an empty file so to start i always put in the load update and draw functions like i did here and the first thing we'll do is import that wind field folder that we just copied into our libraries so we'll do that with wf equals require and then in these quotation marks we'll do the path to that folder which was libraries slash winfield keep in mind that we are importing that whole winfield folder and it's going to be put into this wf variable and now that that's imported we can use it to start working with physics the first step to working with love's physics is to create a world so i'm going to say world equals wf dot new world a world is simply a space where physics objects exist and we use the world to actually create our objects like for instance let's create a player object player equals world colon and we'll make it a rectangle we'll say new rectangle collider then these parentheses except a few parameters the first is the position that we want our player to start at so i'll put him at position 350 for x and then 100 for y then we want the width and height of our rectangle i'll make it with 80 and height 80. so now our player is a rectangle collider and it's being stored in this player variable and when i say collider i'm referring to a physics object that exists in our world part of what winfield does is it makes colliders so easy to work with normally with the standard love.physics library you need to define a body a fixture and a shape and then keep track of everything separately a collider from winfield though is a combination of all three in one place making it so much simpler before we save and run the game we should add two quick things first we want to add world colon update so this makes sure the world is updated and all the physics gets updated then in draw i'm going to put in world colon draw now what world colon draw does is it draws the shapes of all of the colliders in our world so i'm going to go ahead and save and run and there it is this rectangle object right here is our player collider it's not doing anything though one thing that we could do is give this world some gravity this new world function right here actually accepts a few optional parameters the first two of which is the gravity definition so if i do 0 let's try 100 this would be gravity going downwards this is the x value for what our gravity is and this is the y value so since we just have a positive value for y positive means going downwards so all of our physics objects should be affected by downwards gravity so if we save now and run we will see that our player collider is now falling downwards it was falling pretty slowly though so what we could do is bump this 100 up to 500. this makes the gravity more intense therefore our player falls at a faster rate not all objects need to be affected by gravity though let's demonstrate this by creating a platform for our player to land on we'll call it ground and we'll do world colon new rectangle collider and its position i'll put it at 100 is its position and then the width will be 600 and the height will be 100. so if we were to save and run now we see our ground but it also falls with gravity in order to make the ground less prone to gravity or ignore gravity altogether we need to change its type we can do ground colon set type to static so if we save and run now our ground is static meaning it is not affected by gravity and the player lands on it just now i mentioned that there are forces that you can apply to colliders let's make our player jump by using an impulse so down after all this stuff i'm going to put in a function love.keypressed and in here we're going to check to see if key is equal to up meaning we press the up arrow key then we will apply our impulse we're going to say player colon apply linear impulse and the impulse we will apply is zero and let's try negative five thousand now a linear impulse is just a quick force pushing on the collider in this case we applied a negative y value being applied to the player since this negative 5000 is in the y position that's going to push the player upwards because negative goes up if we save and run when i press the up arrow key the player does a little hop because we are applying that linear impulse there are lots of other actions you can apply to a collider let's make the player move left and right by using a force this time as opposed to an impulse so we'll do this in our update function and we'll tie this to the arrow keys we'll say if love.keyboard.isdown and we'll say left so when the left arrow key is pressed then we will apply a force we'll say player colon apply force and we'll do negative 5000 in the x position this time so it's going to move us to the left and then we'll say else if love.keyboard.isdown for right and in this case we will do the same thing apply force but we'll do positive 5000 which will move us to the right and that should be enough if we save and run where if i press left i start moving left and if i press right i start moving right kind of slide along the ground the thing to note about forces though is that they keep stacking and growing more intense which is why the player accelerates so much when moving left and right if you wanted to limit this you could only apply a force if the player is not already moving at max speed we can close out of this so we're going to apply this force here only if the player needs it to get to the max speed otherwise we don't want to apply it otherwise they'll be going too fast so first to get the player's actual speed as it is in this frame we can do local px comma py equals player colon get linear velocity and this function here is going to grab the player's velocity and put its x value in px and its y value in py so now we can check the px value here to determine if we want to actually apply this force so here on this line i'm going to say and px is greater than negative 300 so we'll make it so that negative 300 is the max speed that we can go so we're only going to apply the force if we are below that threshold same thing for the right we will say and px is less than positive 300 so we're doing negative 300 for the left direction moving left and positive for the right direction also to make things a bit more snappy i'm going to change this 5000 here to 8000 that will make us accelerate a little bit faster so if i save and run now if i hold left i move left and if i hold right i move right but you'll notice that i don't end up going too fast like i was doing before overall though this game that we created just now could be the start of a full platforming project and we were able to set it up pretty quickly the github page for winfield provides tons of helpful information about working with physics i highly recommend reading through it for the next video i'll be taking the concepts discussed here and applying them to the top down styled game from the previous videos and showing how to generate colliders from tiled if you found this tutorial helpful please leave a like on the video that helps me out a lot and if you'd like to see more game dev content please subscribe thank you so much for watching to the end and i'll see you in the next one
Info
Channel: Challacade
Views: 1,802
Rating: undefined out of 5
Keywords: physics, love2d, love, lua, programming, game development, coding, windfield, breezefield
Id: YDr4DW0bj38
Channel Id: undefined
Length: 9min 59sec (599 seconds)
Published: Thu Sep 30 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.