C# Basic 2D Game Engine | Adding Physics

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello lawrence from express unity and today i'm going to continue um the c-sharp 2d game engine that i ended up working on on stream um now uh i don't quite have enough time to actually do a stream on it but um just before i left on that stream i said that we were gonna work on some uh physics for it so i'm just gonna make a quick video by quick it's probably gonna be almost an hour long maybe um but yeah i'm just gonna make a quick video on implementing some physics and we're going to do this with box 2d now this is a kind of a wrapper for the actual box 2d as box 2d is a c plus library um so you can come in here and read all the c plus documentation it's actually very well documented [Music] but what we're going to do for our needs so go to the this github i will put it in a description and come here to the uh the fork from google and then you just want to download this top one uh october 16 2008 and once you download it you should have this um this uh uh zip file here and in here we have some things that we want to use now we don't really care about all these opengl and uh free free guard and all that i only care about the box 2d um dll so you're going to want to open whatever your main engine is um and then just drag that dll there now what you want to do is go back into the game engine and just drag that inside of there uh no that's not what we want to do where is it uh we want to drag it on the main bit here i believe yeah there it is okay so we don't actually want that alrighty so now what we want to do is click on the dll and when it says copy to output directory we want to set this to copy if newer basically what this will do is whenever you build the game uh it will also uh extract this dealer hour alongside it as we will be needing it now what we want to do is add a reference and we're going to browse and that is another game engine i've been working on but we want to go uh here and we just want to get this dll and click ok and it's got an error let's see what it is telling us uh there was a mismatch between the processor architecture of the project being built today um what does it want uh let's uh i don't think this will be a problem but just in case i forget where to change it and let's try 64 with any cpu okay all right so and let's just let's just uh uh why can't i add another one okay here we go and we'll get new again and a new cpu okay wait did it yeah okay ah there we go okay so that fixed that now what i'm going to do is just go into the examples folder here and hello world and the program.cs and we're basically going to be copying this example and we will be kind of putting it in the places that our game engine will need so the first thing we want to be doing is using all of box2d's um references here so let's just and just to make sure box hoodie is implemented let's just talk cd dot yeah it looks like it's all that so let's do that okay let's not do that box2d dot collision looks like we either have an older version or a newer version but that shouldn't really matter it should more or less be the same box2d dot dynamics right great so the first thing they do is specify a world size now you can read the documentation and what this does uh but basically this is the simulation space so we're going to just copy this and at the top of our oh wait sorry i put this on our sprite 2d which is not what we want we actually want to do this inside of our main game engine uh so let's copy those in there and now let's copy this world space code here and our and box 2d uses its own color so we're going to have to change all this as well so system.drawing and system.drawing there's one more error probably caused by drawing yes it is over here so let's qualify system of drawing okay so that should fix all that and now just below all of this let's now define our world space and now we have some issues with it conflicting with our own uh vector format uh so let's say you cannot explicitly commit wait okay i don't know i don't know okay well if that worked it worked um yeah so the issue is did it just remove something or no it should have added like box2d box2d.vector2 or something like that so it's a bit odd um but anyway uh yeah so let's now add our gravity or just define it here this can give us the same error no okay that's a little bit odd oh wait did it change our vector file no okay oh wait interesting ah okay it did um that's interesting okay well in that case let's implement this with our own vector 2 because i wasn't going to do this i was just going to use box 2d's own vector but what we're just going to do is basically do this here so we have a v dot x and then v dot y and i don't think we want this to be a static do we it's kind of weird the way it's done this user-defined operator must be declared static and public okay so i think what we'll do is we won't worry about this but instead we will say yeah let's uh let's not worry about adding this we'll just do it the way i was originally gonna do it um so back in our engine now where this gives us an error um we actually just want to say new vector2 and actually what was the default values here 100 100 okay yeah so new vector 2 100 100 and then this is going to be 2 negative 100 and negative 100 like that now safe to say that this gravity is going to need to be box 2ds vector2 so let's just change that as well alright so now i don't really care about if the physics is sleeping or not so i'm going to ignore that but now let's copy our world and this is where we actually start creating the physical world space and so uh this needs to be inside of our main uh basically before we run the window we do this in here and do sleep we'll just quickly read what this does and it doesn't show anything so i'm just going to set it to false because i believe what it says is if there's no physics moving it just kind of freezes the physics um so we've implemented that and now this stuff here is stuff that we're going to want to add on our actual sprites so for now we're going to skip over this just quickly have a read to find the gram body color body factory find the grand box okay so this is the bit that we would create for our sprite and then this is for any uh like a solid object or something like that so we will skip that and go straight down to the code to actually run our physics step so just above our game loop here oops oh wait is it the game loop or renderer that we want i think we might do it in the renderer uh so yeah just paste these in there and because we're already iterating uh without um without game loop we don't really need to to have this here so we should be able to just copy the world step and i can't remember if we do it before or after our rendering i believe we do it before so just up here we'll put that down and world does not exist so over here well we are going to set it when the game engine first gets cold we actually do want it to be a global and by default we're gonna set it to null which should be yep okay so now this should be fine and yeah and then this that's so good okay so if we save this and just run what we have it's not going to do anything but it should now have a physical world space that we can we can use uh is that just that weird arrow we're getting on streams if i run this again will it work no okay maybe i've changed something in the demo game which would be where all our sprites are loaded let's let me just see what i've done here all right okay so because we changed it uh change the kind of the build for the cpu it's uh being compiled in a different directory and we don't actually have our sprites in there and you can also see it's compiled dll so that's good so let's just copy our assets folder here and put that in the new directory all right so that should be good great so nothing's really changed here same old um but now we actually have a physical space that we can do physics in so we will need to change a little bit here just quickly run through here i think all this is fine now so in our sprite 2d we're now going to want to use our position and scale and all that and just set up a uh let's see here so this is defining a ground body so this is going to be like a a static object so what we can do here just trying to remember how we can do this so we'll start off with the dynamic body because i think that'll be the easiest so we have a body new death body.position and then polygon density crate fixture and set mass from shape and embody.get position okay um we i believe we need to set the world space somewhere otherwise the body can't actually register itself am i missing that i have to be missing it somewhere uh ah here world.createbody okay okay um so how can we do this now so i think the world space for our game engine will need oh maybe it won't work now then maybe maybe it will um i don't think it will public static is that gonna work it does okay so what we want to do inside of creating a sprite i think we might make a new public method here so we'll say public method public method no i've been doing too much python and it's just wording is different in my head right now public void um and we'll say create dynamic right and to create a dynamic object we are going to copy and paste all this and we're going to piece it together bit by bit here and we're going to also want to include all of our box 2d references inside of our sprite class that should fix some things and then of course we need to change vector 2 to effect 2 and world will be expressed engine dot world and crate fixture it's not um let's see don't create okay that's i think we are using an older version um but that's fine that's fine no problem okay so we are now basically setting up an object and registering it with the world however we want a few things to be public here and so the body definition i believe we want to be kind of up here so we can reuse it and then we want the body to be up there as well so we can reference it okay now we're creating and what we want to do is instead of passing in a new vector we want to pass in this dot position dot x and this.position.y and what we can do so we should only need to run this once so this would be something we call on the start of our game engine but then this next line is going to have to be called in our update so then we'd say public void uh get new position uh actually no let's just say update position this isn't going to need anything but what we can say now is this dot position dot y is equal to uh except body that position uh what do we set here body death okay so uh body def dot position dot x and then this dot position dot y equals body def dot position dot y okay so i believe that's all we need to do to start creating this physical object so let's give that a try and so uh back in our demo game we're going to actually want to um so where do we spawn up fire here okay it's been a while since i've opened this so we should be able to say player dot uh what do they call it create dynamic that'll create our that will basically convert our sprite 2d into a physical object and where we update position for now we don't want to do this because we want the physics to take control so what we're going to say is we're also going to just um comment this out so then we'll just say something like player dot update position all right so i'm hoping and you know what we might do um just make certain this world space is large enough we're just gonna make it very big and this isn't really going to affect performance um for small things like this but at some point i would recommend that you just have this world space as big as a screen just note though i've had some issues where if any physical object falls out of this world um this world space it will error and crash the program so that there is some error handling involved so let's just hope something okay uh object reference not set to an instance of an object uh [Music] this is a ah let's just go back to this example here let me see buddy def equals new death yes um we might just set it again before all of this just in case and we'll see what that does really uh object references but he was now body was now uh we might be [Music] okay so let's see here so demo game and i think what might be happening is so on load we create the dynamic when is on load called it wouldn't matter because we set set this at the very beginning so regardless oh actually uh where is it am i blind unload oh it's right there okay uh yeah that should be working so body equals create oh wait am i ah okay i didn't do this uh so we won body equals there we go that's what we needed we shouldn't get an error now and a player does not update that's not what we want to see but we're not getting errors so we create the dynamic object we then set it when the player spawns create dynamic so the player should not be null and we just update position what we might do here is let's just say uh log dot uh warning and we'll just pass in body daf dot position um dot two string and we'll see if body position is actually updating and it's just our own position code that's been funky let me see oh my goodness no okay um dot x okay also very close to implementing it it's just the last few little hiccups here um just trying to figure out what everything is doing okay so it's not actually moving okay yeah it's not moving so that makes me think that potentially let's just have a quick look at this demo so define the dynamic body we set its position and call the body factory uh define shape and we create a picture in our case let's create a create shape we set the mass down here what do we do body dot get position body dot get angle right now we don't really have any rotation in our engine so we're gonna ignore get angle but this should be working uh unless hold on here so it's not uh it's body dot get position [Music] dot x and body dot body.getposition.y and we are just going to do that in our debug up here fantastic let's see what that gives us hey there we go it is we're falling upwards but we we have physics and and you can see here it didn't actually crash usually crashes um but yeah we've now exited our physical space and it's stopped working so this just makes me think that in our gravity setting that we had up here um our own world space that we traded the game in might actually be flipped the wrong way so let's just make that a positive 10 for gravity and we should now fall down yeah there we go excellent we are getting some physics into the game now my only problem being is since we don't have rotation do we no we don't um all right well for now we'll ignore that um so we'll do the same thing now but we're going to copy the [Music] uh this here wait um okay it's only this line here and instead of create dynamic we're gonna say void create uh collider actually that doesn't quite make sense we'll say create static put that in there and ground body is just going to be our body this is going to be x uh express engine.world.createbody this is going to be body def all this is fine except this is going to be body dot create fixture uh in our case we either have an updated version or a old version i'm not too sure but create shape okay and heck i think that's it so for everything that's not our player now at the end here we can just say dot create did we make it a public no we did not so make sure we make this a public and we can just say uh in our demo here dot create static and dot create static and that is it we should now actually have collision no we don't uh so what's going on here uh assertion failed abort quit retry [Music] ignore doesn't really give me much of a helping hand here um [Music] oh wait let's ignore that uh do we set the position anywhere set box okay so let's just give it the same shape as this this is also going to have to be of scale at some point but for now let's just do this and this will need to be that um hold on a second so we have shape def ah did i miss a bit here yes yes i did miss a bit all right so let's do all this again um yeah it's kind of thinking that's a little bit too short uh so body def this should just be wait yeah body death so body death and um should be able to just say body depth.position and this will be the new vector 2 using box 2d and we're just going to say this.position.x and this.position.y and body will just be body and this will be expressed engine.world and we want our body def grand that's all good that's all good the only thing is set box we'll just do that and um i should just copy the name here keep everything the same okay that should be good and now we should be able to just say body dot create fixture and this is going right there's a body to create fixture yeah and then we just want to pass in the shape definition and it's not correct fixture it's create shape all right so now that we actually set the position it shouldn't be out of bounds i hope and we should now hey it works okay um so the collision for our i don't think this will work because we're gonna have to do some sort of math to scale our scale correctly because i'm not quite sure what box 2d uses for its uh units but we'll just try scale first and see what happens okay yeah you can see it's it's way too large [Music] so we might just give it a fixed scale for now something smallish uh maybe a little bit larger we've got 20 and 20. okay it still needs to be larger uh that or we can just scale our player bounding box we'll try 15 by 15 all right but that's better than nothing now we should actually be able to apply physics to this i can't quite remember how it might be body not add uh apply force there it is yeah so now we have all these things that are similar to unity um that we can use so just to try this out we can just create a new public void and we can just say um add force we're going to want a vector 2 because we want to keep everything in our actual game consistent we don't want to use vector 2 from box 2d and vector 2 from our own game so we can keep everything consistent but then just convert it in this function here so this is going to be our our force and this next one i believe is our direction so um what can what we can say is body dot uh apply force yeah so we can say new vector force dot x and force dot force dot y and the next one is a point which i believe is the direction so we'll just say back to uh new vector runner and this will be our direction.x and direction.y and that looks good so let's go ahead in our demo game instead of moving our player with just setting our position and also this uh this code here can now be treated as like a like a trigger instead of unity so we don't actually need this because this just detects if we're on a block or not so we can actually remove that entire collision code now and we still want to keep it for things like coins um but we should now be able to say player dot add force and we want to go vector and we want to say zero and negative one and then a direction i think or point it said point didn't it let me just quickly see point so okay okay so force can be uh the direction and point is where the force is being applied from so in our case so let's actually just call this point it makes more sense post point point and point great so back in our demo game this is our direction and our point is just going to be vec uh we can actually just save 2.0 here and that should be fine uh like this okay so then going down should be positive one [Music] left on the x should be negative one and then you should just do the opposite which is positive and i think that's it we should if i press w oh is it eric isn't it okay uh might be too low let's go for like a 55 just to make absolutely sure this is working actually move the window to the side a bit so we can see this no it's not working um what could the problem be uh the update position could it be that we need to update the bus the position after we apply force maybe let's try that um [Music] no that doesn't make sense doesn't hm i forget what the order is uh it is let's just try changing the point here um as well let's see what happens very odd very odd indeed um unless i'm using the wrong thing to add force because this is apply force uh dot is there a velocity it might be velocity that i'm thinking of uh let's see so we have apply force which i believe is the right one that we should be using [Music] um is bullet set bullet that's interesting what does this do i feel like this bullet is familiar i'm not sure why but let's see what happens if we set his bullet no i don't know why is bullets sounded familiar um okay so velocity we want to set the linear velocity and this only requires a vector 2 so let's try that out and i can ignore the point for now oh okay so we are setting our velocity now so that does work which makes me wonder that we might not have been adding enough force before and all these stars and things they actually have their own collision which we don't want so back in our demo game here and for coin we don't actually want to create a static object for that we just want it to be a normal sprite and we're just gonna handle collision with triggers so now we should be able to still collect them yeah and okay you can see that the collision needs to be you know properly configured um i'm just thinking because i can't quite remember all right let's let's try it again let's at this point it's all implemented right so you guys could go and mess mess around with this yourselves so that's just what i'm going to do at the moment kind of mess around with this um and kind of because apply force should be the one we want i'm just going to set these values to some very extreme number to see if it is our gravity setting that's just too heavy um so we'll try that oh here we go oh okay it's doing some funky stuff it's doing some funky stuff update position so this shouldn't work at all yeah um so passing it down he gave us the error no what broke before then okay well there i think we just had it in the wrong and now that our actual world space is flipped around the other way um so this is actually going to be positive and this negative and this here we should be able to just make it vector 2 dot 0 and now instead of this game being top down we can make it a platformer so we're just gonna say something like this okay and i forgot what i had for empty spaces oh yeah okay um okay so we just put our period for the empty spaces here fantastic now we will copy this don't do that just a couple of times [Music] why are you not aligning properly okay and we have g for ground so let's just replace that okay and we will kind of just make a step up here and with the last part being a coin and we can't figure out a player we'll just put him up here and we'll see there are builders uh cannot convert method vector2 what from where oh uh forget our brackets um okay it's uh it works it's it's just one of those things you're gonna have to kind of mess around with the physics yourself with what is still opposite i'm still doing some weird stuff here um hold on let me say something this is on update i think i might know why it's acting up um so on update we call it on render so i think that's our issue we want to call this above update and i think our stepping might be a lot smoother now yeah there we go feels so much better um so velocity iterations we can just bump this up position into iterations i might just bump this up as well because that will just smooth it out yeah there we go feeling a lot better and so at this point it's implemented right so um so we have one for add force let's now do add velocity or rather set velocity and we are just going to remove point we're going to set this to val and then we'll say body dot set linear velocity and we're going to say new vector val dot x and vel dot y okay and so now we go to demo game here and let's just comment all this out and now just change everything using a velocity so let's say player dot set velocity and then we just want this here we'll do the same for the rest so now you have two main physic methods that you can use this is going to be negative this is zero this and actually uh it being 1 000 is going to be way too high now so i might actually set this to five and then this one will be five as well okay okay and we're getting the same thing where this is should be a positive i might change it to 15 so it's faster did i just make that the wrong way around yes i did yes i did okay this is the final iteration you guys can have a play with it so you will need to do some maths so you can you know actually jump and all that but we now have physics which is fantastic um maybe we'll implement one more um so what else do we have uh that's shape we want sprite you can also do the same thing for shapes as well but we're not going to do that because i'm probably going to remove the shape 2d um so just checking what else do we have that we can set um i still don't know what said bullet is it has a flag apparently we didn't said that before did we so true so let me just quickly see if that affected anything all part of the programming process experimentation see what works and see what doesn't work okay uh things are the opposite way i don't get why physics is when a pla applying force is the opposite but in the center setting [Music] okay it's doing the same thing now that's very interesting um i just feel like add force isn't acting the way it's been acting when i've used it in the past let's try setting the point to the exact same thing as what the uh is what the force is so i'm losing losing track of what i'm saying here all right so that does okay still a little bit funky um but i mean it's all there so just experiment around and see see what works best right now setting the velocity seems to be the best thing um there's not really anything else that we can really set here um your local position center and change the angles maybe apply impulse that might be what i'm thinking of let's try that okay um we'll just undo everything so it's back to zero we'll see if impulse is the one i'm thinking of oh yeah i think impulse is the one i'm thinking of uh so this might actually just have to be a lower value again fantastic yeah i think it was impulse this this is yeah this is actually doing what i wanted to do and see collision is still awkward and bouncy and whatnot um that is just something we you're gonna have to uh mess around with with your own scaling so where where these boxes are and stuff so um you could do scale of x i don't know divided by some number i you're gonna have to research exactly what the uh what the units of measurement they use um because at the moment we built the game engine without really thinking about any of that stuff right so we don't actually have a proper unit of measurement oh divided by two that's not too bad and what you can do right to jump is uh when we press the that's the wrong script when we press the up key instead of adding force we set velocity to something i don't know high so walking along tap the jump key okay that's that's too high walking along tap the jump key and yeah kind of a little bit messy but that's it there physics implemented happy with that um pretty simple implementation i i think box 2d is pretty outdated at least not the not the normal box 2d because this seems like it gets updated pretty regularly but the actual wrapper for c sharp seems like it's i mean if you if you look at it uh it hasn't been updated for years four years and in two years yeah it's kind of an old one um but yeah i'm pretty happy with that it should get you guys going in kind of implementing some sort of physics into the game um and yeah hopefully you guys enjoyed this it is another long one but um most of it was just me ranting and figuring out stuff but yeah it's mostly copy and paste if you just look at that uh that example um but yeah thanks for watching i hope you guys enjoyed it uh if you need any help go ahead join our discord server and i'd be happy to help along with a few others um subscribe like if you want to see more comment down below what you reckon and what you want to see next um to be honest i don't think i will be continuing the game engine uh using the gdi uh stuff and we may switch over to something like opengl um which is very complicated uh in regards to what we've done here right because what we do here is uh very basic stuff uh with the windows api like we just do like uh g dot draw image or g dot fill rectangle yeah we don't have that stuff in opengl we actually need to create all our vertices and triangles and stuff even for a 2d game um so there's there's a lot more code involved um but yeah that's that's something i'll consider uh if i get enough people wanting a tutorial on opengl or some sort of other drawing library it doesn't have to be opengl um yeah i hope you guys enjoyed the video and i'll see you guys in the next one [Music] bye [Music] you
Info
Channel: Laurence Creates
Views: 3,639
Rating: undefined out of 5
Keywords: c# game engine, making a game engine from scratch, coding your own game engine, 2d game engine, adding physics, box2d, c# box2d
Id: Xa0948wTByw
Channel Id: undefined
Length: 58min 18sec (3498 seconds)
Published: Sat Sep 12 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.