3D Game Programming - Episode 34 - Entities

The Cherno · Beginner ·🛠️ AI Tools & Apps ·13y ago

Key Takeaways

The video demonstrates 3D game programming using Java, focusing on entities, game state updates, and collision detection, utilizing tools like Java, Nano time, and ArrayList.

Full Transcript

hey what's up guys my name is Theo and welcome to Episode 34 of 3D game programming in Java oh it feel so good to say that cuz I haven't said that in over a month okay so we're back um hope you guys have been enjoying the morning coffee series by the way uh cuz that is taking a ridiculous amount of time to make I don't know how I'm going to be able to keep it daily but I'm going to try my best especially over this next week um to try and get an episode out every day so you guys should see an episode out every day except for Sunday because Sunday is 3D game programming in Java day um or actually I've sort of renamed it to 3D game programming day um all right so today um I've decided to do a few things today today uh I want to I want to spend it more on um I want to sort of Orient this series more towards actually talking about what I'm doing than getting a bunch of code down and the thing is it's really hard to do that because when you're making a 3D game um there's a lot of processes you have both you know in your mind while you're doing it and also you know um in terms of code so it's it's it's kind of hard to try and make a bunch of you know 20 minute 30 minute whatever 50 minute 1 hour long episodes where I write I don't know maybe about a 100 lines of code each um because at that rate you know it would take a few years to make a 3D game like this not even like this though cuz this is um I'm I'm might talk a bit about this kind of 3D game because you guys might have realized you know this isn't exactly this isn't this isn't really a 3D game right this is just uh it's just a it's essentially a 2d game with a 3D looking perspective because technically technically um if you check out you know the the definition of 3D in computer Graphics this is not really it because this is just a raycasting engine right um so there's a few things there's rate tracing don't don't get confused with r tracing rate tracing is a is more of a 3D rendering technique that uh is kind of slow so it's not really used in games but raycasting is the name of this engine um and I don't know I guess I should have probably um explained that earlier but you know this is sort of orientated at beginners I guess and you know a begin is going to be like oh cool raycasting engine yeah great now I know what it's called but I I don't know anything about it um but yeah so this is the same type of engine that was used for uh Dungeon Crawlers I guess like Wolfenstein 3D and also Doom the first Doom game that's um it's pretty much this engine so yeah I've sort of expanded it a bit by um by by making the ceiling two blocks high instead of one I just thought that I'd play around with that for you know for experimental purposes um this time is really d actually I want to do a few things today the main thing I want to do today is entities though you might have seen it by the title um I I so as I said you know I wanted I want to start taking it slow I want to start um I'm you know I guess my year 12 my final exams for school are going to come up in November and I'll probably spend the entire October October is before November right y um yes so I'll probably spend the entire October studying for them because you know these are my final exams basically my my future will depend on what Mark I get in those exams you know as well as throughout this year but um about you know 60 to 70% of the Mark is going to come from those exams so it's very important for me to to to do that which is why I do want to take it slow I don't want to get into too much we've done we've basically done I guess you'd call it boilerplate code and boiler plate code is just um it's code that get basically gets repeated for whatever the hell game you're making or whatever whatever you know application you're making so something like you know setting up with high setting up setting up a screen class or you know in this case a render class um um you know setting up the timer um and uh yeah I think I might redo the timer actually I don't know if I'll do that right now I just I don't like the way that it's currently handled so yeah okay we're going to do entities today but I guess as a little bonus I'm going to redo the timer first so let's get rid of ticked um a lot of you guys have been complaining that I've been using ticked te is essentially update right so we could rename it to update it's the same thing um it's it's a bit of a convention to use tick in game programming it's um I don't know you you'll see it a lot um so be be mindful tick is the same as update right I I some I I write both ways I've actually sort of leaned into running update more now but um yeah so let's let's order this let's tick count is basically updates so let's just make a new integer we'll call it updates and we'll set it equal to zero all right and instead of having a bullying for this let's get rid of this let's render we should be rendering as fast as we can um so let's actually okay so the purpose of this code obviously is to basically say that yeah whatever whatever's here let's have it happen 60 times right um and I didn't say that right I meant let's have that let's I guess module it to 60 so in other words once every 60 ticks we're going to do whatever's in in here um once every 60 updates uh which essentially results into a second because we're we're supposed to be updating the game at 60 times a second right there's a big difference between frames and updates um huge difference because you want to always you want to you want to basically keep the updates static and not not Java static not um not not Java static you want to keep the update static as in you know that they don't change so I guess to illustrate this in a in a different point of view excuse me for my voice as well I'm a bit sick right now uh it's winter in Australia and it's cold anyway um yeah so to illustrate this in a different way I think I think of this right my computer right now is it's it's still considered faster than the average computer even though it's 2 and a half years old um cuz I built it 2 and a half years ago but um it's it's a very fast computer right you might be sitting on a laptop um the difference is I might be able to you know I guess render this game um let's just not let's not talk about this game this game is a bad example cuz it's not very efficient but um and I might spend an episode making it more efficient but we'll see about that I don't think I don't think there's a huge need to to be honest um you might be running a 2d game say I might be running a 2d game at 500 frames per second right but because your computer's not as good you might be running it at 100 frames per second now the problem with that is that if if we ignore updates and just say that okay the FPS is going to be the same as the updates then essentially my game is going to change its state it's going to update 500 times a second and yours is going to do it at 100 times a second now the obvious problem there is my game will simply run faster it'll actually physically everything will move faster because the computer's updating you know I guess updating the positions of AI characters updating my updating the players position 500 times a second whereas yours is only doing it 100 times a second so in 1 second you know if I'm moving one pixel or whatever one unit once every cycle then I'm moving 100 units per second you're moving I mean I'm moving 500 units per second you're moving 100 units per second so in other words the purpose of this timer is to make sure that we stick and by convention it's 60 frames per second now why 60 frames and by frames I mean updates I'm actually confusing you guys aren't I um vsync for example limits your frame rate 2 60 per second because uh you know that that'll basically avoid um you know updating too much but it's not a problem with this because we're not running too fast um if we were running above 500 FPS I'd probably enforce something like 60 FPS or make the thread sleep for 2 milliseconds or something every now and then but the point of having a timer like this is so that we can update uh the game State we can update the logic of the game at a at a set amount per second so if your computer's very fast and my computer's very slow or or vice versa or whatever then you know our game will look exactly the same okay that's the case um so by convention um and this isn't really by convention because if we go to let me just duck down screen resolution here um if we go to advanced settings you'll see that the refresh rate is mine's actually at 59 Herz probably should be at 60 but 60 a 60 HZ monitor I'm not I'm not going to apply it right now just in case it screws up my screen recording it should be 60 though um that's it's pretty popular that means that we can basically maximum we can see 60 frames per second so our monitor will update 60 times a second right uh there are a bunch of 120 HZ monitors which apparently really smooth um you would be able to you know in in case of that I'd probably make sure make that like 12 or so but um because our monitor is not capable of I guess updating more than 60 times a second you know you might as well limit the frame rate to 60 but um I guess I guess it's fun to see how fast how efficient your engine is so people don't really do that that much but anyway by by convention and by point it's good to limit your updates per second to 60 FPS leave that usually unlimited again if you're running you want to if I'm not going to go into the whole I don't know I don't know if you guys like this new format that I'm trying out where I just spend uh time explaining stuff like this but uh I think it will this is kind of turning into more of a podcast is I should probably stop okay so let's just wipe out this code um don't need that don't need that don't need this and don't need this and current time past time um okay so um we're dealing with Nano time here which is nice but on the other hand it means that we have to convert Nan seconds to milliseconds for the purpose of timer so 1 2 3 4 5 6 7 8 9 um which is 1 billion um and we want to divide that by 60 all right because we want our fps to be limited by we want our updates to be limited at 60 if we wanted our updates at 120 we'd put that to 120 um and this 0 Z at the end few of you might be wondering why you need 0 Z and why not just this uh one of the reasons is that if we put a point Z we're basically telling Java to handle this as a double else we're just telling it to handle it as an integer which means that one one 1 billion divid by 60 you know I don't know what that equals but I can find out that's going to be this if we um if we divided it if we if we don't have the0 Z here like we do now it's not going to give you that it's going to cut this off and because Java rounds down I'm pretty pretty sure it's going to give you that number without this precision and we do want the Precision because we want to be as precise as possible else we else hence rather hence we need to put the point Z at the end all right anything else we need uh probably Delta so that equal to zero okay so essentially um I'm just going to in fact I'm just going to let's put let's push up request focus into here W was it really in there shouldn't have been in while in the running Loop Okay so so what we want to do is we got the current time what we want to do now is we want to add to Delta the difference between the current time and the last time the previous time I think that's right yeah yeah and then we want to divide it by NCS so divide by our NS um conversion into milliseconds okay and we want to to set current time rather we want to set previous time equal to current time okay so now we want to say that if Delta is greater than or equal to 1 uh which should happen 60 um times a second uh let's just first of all subtract from Delta um and then let's create our updates and let's tick okay there we go and in between that we should render so let's grab render and frames that should be here and then while um while system. current time milliseconds minus previous time is greater than 1,000 which will be once a second um we want to do all this stuff so yeah and we also want to make sure that updates equals zero so that we reset that okay we haven't used this it's all right it's the same as Delta unprocess seconds is identical to Delta okay um yeah okay so that's a new timer that should work let's just run it and we'll see what we get all right well we're chilling at zero frames per right now but our game is running smoothly and it's not updating that so something is wrong um oh whoops my bad okay previous time let's make a long yeah let's make a long hold timer we'll set that equal to system. current time milliseconds and we'll make sure that we're doing this and we need to add 1 second to timer just like that so let's see if this worked yeah okay I did so now it's telling us that we're running at 40 FPS which is pretty accurate actually I think we are running at 40 FPS which isn't good um let's doing anything to updates either so updates plus oops updates or UPS updates per second plus frames Okay so so they should be identical in this case which they are okay so this is this is always going to try to be 60s bad example because where so let's just lower the resolution just for the purpose of this to 80x 60 it's tiny um oh we've got the whole configuration thing going on don't we so long since we made this 80 by 60 I didn't even finish this did I okay let's play okay so you can see that we're running at 60 frames per second and it's actually doing it twice I don't know why but um you can see that we're running at 60 updates per second so that's limited to 60 however our FPS is much higher so yeah there's no point at this existing is let's just make sure that we're at 800 by 600 okay so um excuse me ah okay we're back why is it doing that twice did I no clue I don't know what's going on um yeah that's fine it seems to work um okay so now let's move on to the main aspect of this uh episode 17 minutes in I'll give you guys a nice chilled relaxed long episode because it hasn't been one for like a month Al so actually when was the last episode of 3D game programming in Java um let's see June 10th wow really it's been like two months yeah it's been wow it's been like two months since the last episode I'm sorry about that okay uh um huh yeah I I I privated the live stream by the way from yesterday because it's completely screwed up but this guy just sent me a comment saying that he's still watching it oh and he's got the troll face as well no I'm going to have to show this is kind of funny hang on so this why guy over here is still watching the live stream and he's got a little troll face so yeah that's pretty it's pretty accurate anyway um let's get back into this so entities yeah that's right okay so at the moment let's just I hate this I ha this view I hate this little view the package Explorer seems to show like all the folders not as subfolders so in other words this should be a subfolder of this but it's not it's just showing all of them there's another perspective called Navigator which shows you everything in folders like it should be so like this and I I just find it a lot easier to navigate so let's go into we don't even have yeah sure okay let's make a new file or class rather we we could make a new file doesn't actually matter call it entity dot Java just makes gives you a nice clean I don't usually make them as files because um it's uh there's no point so mine front and let's put it into entity so let's make a new folder called entity that's not spelled right is it rename and and oh it is spell right damn I'm paranoid today um moving there okay sweet so now we've got an entity class now a lot of people um are always confused by the aspect of uh I guess you know subclasses or in other words classes that extend other classes and um I I think that a good way to explain it is that it's a template right so we've got an entity class that we've made right here right it will never be instantiated okay it will never in fact we should probably make a constru it would be it would be good programming practice to make a private Constructor just like that um because that would just ensure that if we were to try and make you know entity E equals new entity um then it's not going to let us because um because it's not visible so that would be good programming practice let's just get rid of that import and it's gone control shift o organizes your inputs by the way um so yeah I guess that's a good way to do that quick simple way to make sure that we never actually instantiate entity again um with the morning coffee episode one on um on uh uh visibility modifiers um it's it's more useful to use visibility modifiers like that if you're working in a group or in like a company or whatever and you're like you know you're constantly like sharing your code with each other you're constantly sending you know the other guy your code and being like okay do this for me um or you know you've got to hang on I'm I'm noticing a pattern here this always stays a 22 22 22 22 I think that there's um I'm not going to spend my time doing that now though actually getting distracted um anyway so I guess that's more in a thing but you know if you send someone your code and they're like okay how is instructed the The Entity class when he sees this he'll be like all right well there's no way that this guy wants me to actually make a new entity okay if if I need to make a new entity I'm not going to use the entity class to do that I'm going to use a subass of entity to do that um so yeah I guess think think if this is a template right so what what do you want to do you want to um in fact you could you could call this protected actually as well um but I'm just going to call Private until I need to call protected if I need to call protected but um again template okay what do I need to have in the entity class right every every entity that I make whether it be the player a mob other players if it's multiplayer whatever they all have a um how do I call this I guess a set of properties that is applicable to every single entity in the game right and those entity those properties instead of having to copy and paste them for every single entity I can just make like a template or like you know or a super class in this case but um I can just make like a a reference of those variables so that whenever I make new entities whether it be you know the player or whatever they're going to have these these variables already assigned to them so um let's make uh a few projected variables so X and Y first of all um they're going to be the X and Y location on the map um I'm going to call it X and Zed just because why Y is vertical so I I don't don't really want to mess with that um what what other things are we I'm just doing this on the Fly by the way I don't even have I don't even have any notes in front of me so I might be coming back to this class and adding more of this stuff because I don't know what I'm doing um okay what else do we need um I like to have um a protected bullying um called removed so in other words um if we run the public method the public in fact yeah the public method um public void remove then it will set removed equal to true and basically what what this will do is when we talk about array lists in the future we can be able to see that if removed equals true then remove the entity from the array list so in other words destroy it stop updating it and stop rendering it um and you will see that in the future okay that's going to be it for now because I can't think of anything else um I don't want to put Sprite into here how how are we even managing Sprites I'm Sorry by the way it's been two months since I've opened up this project so I have no idea what the structure is I've forgotten everything so yeah just bear with me through that but um uh we're just hand we're just handling a single uh a single um Sprite shade aren't we and we're yeah okay and we're setting hang on ah okay we're statically setting oh why are we doing it that way yeah I I I probably wouldn't do that now if if I was to rewrite this game I'd probably do it completely differently to be honest I'm not sure why um yeah that works too I guess anyway so let's make a few more things let's just make a mob class so let's create a class make a new package for it and we'll call it mob and MOB will extend entity okay so let's import entity make sure you put the com dot the M front one not the the Java the Java X the Java X um whatever the hell was there um yeah we're going to have to have a constru for this um let's just call this protected so that we don't we get rid of that error okay um let's see okay so a mob is going to basically because we're we're extending entity right X and Y and removed are already in here so that's all fine um but you know basically now we're saying what do we want all mobs to have in common that that all entities don't so in other words if we have like a I don't know a rock we don't want the rock to be able to move exactly do we but we do want entities to be able to move so what we need is a move method right A Move method um so the amount is going to move in X and the amount is going to move in y um and this method is going to basically move the player or whatever in fact let's just make a player class at the moment we've called a controller I believe yeah okay let's just move the controller class to mob and let's uh change the package declaration and let's rename this so control what am I playing alt shift R to refactor it so that we don't need to copy and paste the name that's all right and import our our stuff okay let's just check that this still works and we got an error somewhere oh crap we've got a lot of Errors what's the problem with this oh it's in a different package damn this is going to take a while oh maybe not seven rors zero errors yay okay let's check that we can still move yeah okay we can okay so um at the moment we're using an interesting way to move let's just make sure that player extends mob and now what we want to do is we want to fix up a bunch of this stuff so first thing we don't need is these two variables and also I don't want to be able to immediately um okay so Z isn't public let's make this public okay um I don't want to be able to I don't want to manipulate I guess the X values immediately but rather I want to um I I I I want to call a move method and I guess modify them separately so you'll see what I mean in a second so what I want to do is in here um first of all okay a few things that cross my mind when I think of oh this is interesting look at this 43 updates 4 oh no don't swear I saw something different there anyway first thing first thing that crosses my mind when I think of this is Collision detection immediately crosses my mind when I think of moving and the reason is that um if if we if we handle Collision detection in a fairly efficient way um it will completely crash if we try and slide against a wall so in other words when we crash into a wall we we don't want to you know move backwards being the only option we sort of want to be able to slide across it this is sort of more applicable for 2D games but I think it'll work for 3D as well and basically what we want to do is say that if if we're um if and the way we can fix this is if we're moving so in other words if Z okay this is either going to be negative 1 or positive one right and in fact it's going to be everything in between cuz it's just 3D but the point is zero means that we're not moving at all so in other words if both of them are not zero so in other words let's just make sure everything's called Z so if we're moving in both directions if if we've got like w and D press down if we're moving both left and forward then let's handle them individually by just moving this and then just moving that and then make sure we return so what that's going to do is it's going to return out of the method and then it's going to process it twice but in a much better um way so um basically what we want to do is just X Plus = X A and Z Plus = Za a okay and we'll have to uh figure out rotation and everything in a second again so let's get rid of this okay so if we move forward we want to uh xaal 0 and Za AAL zero so every time we go through this Tech method this update method we're going to set them back to zero again so if we move forward we want Za to be Plus+ so in other words Za will become one and remember it won't become any anything greater than one because it will be reset to zero before it does it again so if we go back we want to minus minus we might have to flip these controls as well I'm just doing this on the Fly again um XA plusus minus and right will be X A++ okay so rotation left and right um that's cool but this could be a problem actually let's hope it it's not going to be there so after rotation yeah that's actually really interesting because I don't know how this is going to pan out okay let's just see let's just see how this works let's just move like that and we will actually need to know the rotation value keep putting y a instead of z a okay let's get rid of this let just the Mouse stuff um so let's go into move and let's make sure that we rot for rotation okay so what we need to do basically is set I guess a new X I call them NX and basically we need to use vectors here to make sure that we um just a bit of water um we need to use vectors here to make sure that we're going the right way so I want to bust open paint on that so that I can show you guys what I mean okay so if we're going this way um for example we're not we're not going straight or whatever we're going at that angle and this angle let's just say it's 40° that is crap I shouldn't even try I should just use this shouldn't I 40 40° um essentially what we what we want to do is we want to figure out um how much we need to move up and how much we need to move right so if we're facing this direction if we're facing this direction we need to figure out the up value and the right value that we need to move so the the uh the up value will technically be Zed because this is 3D and this will be X we need to figure out the Z and the x value the the amount of the Z and the x value that we need to move um every every update basically so in other words this might be something like 0.8 this might be something like I don't know 1.0 because obviously we're going actually technically if it's 40° it beat the other way around but my point is well what is what's this supposed to be is this really me writing 0.8 holy crap anyway um I should just use like a tablet or something a um one of those Graphics tablets the drawing things with the pen anyway um so anyway so to work this out what we need to do is use a very complex algorithm that we've already used before to figure out uh this so in other words what I'm going to do is we need to X plus equals NX so NX being you know this value or this value depending that's for Zed though so we need to add uh it to that okay so double NX is going to equal it's something to do with rotation so let's H okay hang on NX is going to be uh cosine because cosine is to do with X and S is to do with y of the rotation except we'll need to figure something out here as well because that'll probably be too fast let's do the same thing for whoops for y okay now we're also going to have to multiply this by a let's call this Zed by z a so in other words if z a equals z it's going to give you zero and the Z is not going to move at all which is actually kind of wrong but we'll we'll figure something out in a second because um the other thing is we need to add sorry this should be sign we need to add um oops we need to add this to the equation and subtract it from this I think that's right okay so that should give us us the right thing and then we can multiply but by whatever modifier we want if it's going too slow or fast um let's make these double hope that nothing crashes good um and it's going to really be interesting if it works okay so let's get rid of this um yeah so you can see that basically it's the same code that I just wrote um except I think I did it wrong because where are we mob because this should be sign and this should be cosine is that right I guess we'll see yeah okay okay um let's check it out okay so it works and not exactly so you can see how that does not exactly work for rotation but main movement does work and we are very fast so we might slow that down okay so now let's figure out the problem with with this I feel so weird doing a tutorial by the way cuz I haven't done one in two months so just give me a break just for this episode um so something's wrong something's not adding up with the rotation I'm not sure what it was probably shouldn't have been so quick to delete maybe for rotation a no definitely not thing is it's thing that's bugging me is that it is sort of working isn't it Forward 45° like see it's not it's not completely stupid although I can't get it to move that way yeah that's odd okay let's come back here and see if we can maybe I was right in the first place nope clearly not okay huh make sure that everything's right here and next said whoops what did I just type in placeholder um I want to see the render 3D class um make sure everything is right here and that does in fact go to oh yeah let's see how rotation set let screw everything up I think huh okay let's um let's let's deconstruct this and make sure that we're doing everything right first of all um we want rotation to plus equal rotation a that's for sure so that when we rotate oh yeah maybe not that makes it nice nice and okay so it's working I think until 90° yeah okay let's figure this out um let's go into the move method and let's check out um what the value of rotation is right now so it's 25 is 26 oh that's weird yeah that's clearly not working because oh okay that's a bit of a problem isn't it huh so we just keep turning basically I think it's in radians though it should be in radians shouldn't it the whole thing should be in radians not degrees so 1.5 this should be 1.5 this should be 0.5 hang on if it's in radians so it's three radians for oh that doesn't work here it doesn't but um it's three ran for an entire for a half a circle that's wrong that can't be right it's supposed to be oh no that's right that is right it is in radians and it is right cuz Pi is 3.14 and that's what this is basically 3.16 okay that that is right that's good so it is giving us the correct rotation value that's for sure um but oh first of all it's it's moving us every second let's make sure that it only moves us if XA doesn't equals zero or make sure you don't put and or z a oops z a doesn't equal zero let's um actually let's quickly go into window preferences let's go into um Java I think yeah Java code style formatter edit this edit this and go into control statements and keep Simple if on one line so that we when we hit control shift f it formats it nicely like that cuz it is just a simple if statement with just one line so we might as well keep it like that okay so now we shouldn't be yeah now we should own the update as you can see when we move okay so then something's wrong with this math then let's not print the thing cuz we know it works um so x a time equals so in other words 1 * equals that plus how is this wrong I don't get it this is negative right that's not going to work nope ah this is bugling my mind right now cuz I don't know what I've done wrong hang on M why is this card here that's interfering with oh my god did you just say that oh God I'm going to cut the last bit of May yeah this is stupid see this is what I'm talking about this is why you should add comments into your code that just cost me like 20 minutes okay now it's freaking Works beautifully like should have from the very beginning let's get rid of this crap unbelievable yeah so the reason that didn't work if you didn't see it very clearly now we're freaking sweet rotation speed's a bit fast but um apart from that it's a bit slow3 might be nice oops okay let's just leave it as leave let's just leave it at that all right so um you might have noticed that the problem there was that we were also adding uh an incorrect value to um the thing we were also doing this um as well with which was interfering with everything because it was causing this to happen um or in other words you basically just ruined everything so and I didn't realize that code was there because I forgotten I'd forgotten how I'd made moving work in this game so that um that sucks all right so now we've got entity entities right we've got our entities working out correctly we've got got um got a move method for our lovely lovely mobs and we've also got a base entity method which contains a few different values what I want to do now quickly before I go um is set up uh a few things in level okay so at the moment level has an array of blocks um which add this stuff right but what I want also to happen is um public void render or more like update so I want this code right here oh that's the Constructor isn't it huge let's um let's quickly get this crap out of the way generate level that into there okay update um let's basically what we want to do is we want to update our entities so in the beginning of this whole thing in our display class we've got a player um where's our player variable now again I could be wrong because I haven't actually don't remember how this works oh it's in our game class isn't it yeah so we got a we basically got this going on here so we're updating the player we're updating everything um throughout throughout here um which is cool but we we want to be able to update if we have like 100 players in the game or whatever 100 mobs in the game just running around we don't want to have to create new instances of each of them we don't want to have to say all right let's make another player called player two and now what we're going to have to do is we're going to actually have to create the new player and we're going to have to also you know tick the new player right and you can imagine that if we've got like like a thousand of them or whatever then we'll have this huge thing here that is just crap and we don't need so the solution to that is to use an array list now what is an array list we've never used an array list before in this series um and a bunch of you are probably wondering what an array list is um and I will answer that in a minute uh maybe I should save it for next episode actually nah no we'll do it this episode okay so an array list is basically like an array hence the name right so what we could do you guys might say is let's just make an array list of players right called players and then what we'll do is We'll add as many as we need to that now that's cool but what if we want a specific what what if the amount of players changes right once the player is dead once we kill another mob on our screen we want to remove it from that array to save memory usage we want to get it out there cuz we don't we don't we don't want to be updating it we don't want to be rendering it it's gone it's dead let's get rid of it let's call the removed method and remove it from the array now you can't do that with entity you can't do that with an array okay um because it's not Dynamic the size of the array the index of all the items in the array that's fixed once you set that it's done what what we need to do so like with blocks right we can't change which block is where while we're running the game because it's an array and we've set it up once now an array list is like a dynamic array okay there's no such thing as array index out of bounds because the the bounds of the array are whatever as as much as we need basically if we we can keep adding I don't know a million mobs to the map and it'll still be fine so apart from you know all the other uh C like memory usage and mostly memory usage actually and probably processing processing time as well so what what we want to do here is we want to set up a new array list which will hold all all our entities so right all we need to do is go private list entity now first thing you're going to ask is what are these fancy less than and greater than signs um enclosing the word entity now this is basically all this is doing is it's saying that this list is going to contain a type of object it's going to contain entity objects and that's it we can't add for example a level object to this right we we can't just decide to add a level to it okay it needs to be if we set entity as basically the contents of this list it has to it can only contain entities this list can only contain entities we'll call it entities since I've been saying that word for the past hour and it's the name of the episode and we'll say that equal to new array list and again entity right and then you'll have your things as usual now let's import it so oh make sure you import the one that is in util not um or not awt util and obviously entity is our entity and that's it okay sweet so now we've got an array now we've got an an array list of entities what what we want to do now is um public void add entity and entity a so in other words we want a method to add a new entity into the level so all we need to do here is say that entities do add e right so it's going to add whatever entities in the parameters to our the list now we need to update every single entity now the reason I'm not making a render method is because we're not actually rendering the play the play is like just a camera it's not actually being rendered at the moment we'll probably get onto that later but anyway so in other words for and I equals z we need to make a for Loop to render all of the entities in the list um I is less than entities do size it's not length in in arrays it's length in um list it's it's sorry let me just grab some water in lists its size and it's also it's also a method it's got these things i++ and all we want to do is we want to e uh sorry entities. getet now the index is going to be I right and we want to update so tick or whatever now um okay so to do that we need to make the actual update method okay so yeah okay so you can see that I've made like a method here that doesn't do anything that's cool but remember if we have the exact same method in player it will do something because we're rendering a we're actually doing a player thing we'll see in a in a minute what I mean tech let's come into player and get rid of this crap let's just rewrite the entire tick method so come with me cut oops don't want the last line oh there we go paste okay so a bunch of Errors that's no problem um public player and um input Handler input and we'll make a private input Handler here called input and we'll just set this do input equals input so all we're doing is we're saying that when we create the player initially we'll put an instance of the input Handler object there and then we'll just set this input equal to this input so that we can use the input Handler objects through throughout this class so um let's go into the input Handler thing and oh there's a lot of crap here uh let's make a bunch of yeah okay let's make a bunch of um go into game and just grab this and pop it into here and we need to make an update or a tick method um and we just need to basically set this okay let's cut this and put it here and then buing forward back Left Right rotate left rotate right jump Crouch and run okay and let's come into here and I'm pretty sure that we've got an yeah we do um in game. tick just on the game. tick input dot tick and we don't need this let's get back into our display class real quick and game class let's move this up well whole screen just flashed it's odd um where's game class where's my game class okay so new player and it'll be input and looks like we'll have to add this into here and over here we'll just add input and make sure that it is that this thing is before it otherwise we'll get a n we'll get a null pointer exception so we'll set input equal to that and this right away we'll just add it into into the game um get rid of that cool so few errors that's all right we're fixing everything everything um okay sweet what's run what a run used to be I've got no clue make it public H run that's shift isn't it yeah it would be okay so we fix everything up let's see if it works and it works perfectly okay sweet so now we' got this in here now this is going to be the longest episode ever isn't it no I am going to cut like 20 minutes out of it so so maybe it won't be H okay oh I got to get back to watching Dexter it's like 4:15 I just finished um season one of Dexter yesterday for the first time oh it's so good so good if you haven't checked out Dexter check it out it's actually such a good show anyway um let me just turn the backl on for my keboard cuz I can't see the KE and I like seeing the KE cuz it's cool um and I can actually see what I'm doing okay uh so what we want to do is we want to level. add player okay so that way we don't need to take the player we only need to take to take the level um did I call it update yeah I did not really consistent but it doesn't matter level. update um let's call everything tick rename call everything tick for this project that should work so let's just see crashed wi crash with a null poter exception so level equals null yeah see you want to do that after you set level equal to no not Java e no okay so this should work yeah it does okay so you can see that we're updating the player perfectly right now um even though we are we're not actually technically calling the update method of the player so in other words that is mission accomplished and uh yeah so let's make the map a lot smaller and next next week uh on Sunday we could do one of two things guys so I'm going to have to actually get you to to post um oh so many walls let's make walls a bit rarer right now so let's just say uh 28 and maybe this 25 there we go nice okay so two possibilities for episode 35 we could either do Collision detection and I know a lot of you guys are going to like jump up off your seats and be like finally um yes so we could either do Collision detection next uh Sunday or level design and by level design what I mean is that we're going to be able to go into a program like paint.net and basically you know draw whatever level we want and each separate color will be a different type of block okay so we could do that so either level design where we'll read the level file as being an image as a PNG image and each different color is going to be a different block at that particular location so we can draw we can basically draw a 2d a top facing map of the level and that will actually be what the level looks like or we could do Collision detection one of the two options okay so I actually well I I'm thinking about this right now and I'm like I don't know what's going to win I know you guys want Collision detection but you also want level design so that will'll do next week guaranteed one of the two um unless something very bad happens and I won't have time or something so uh yeah thanks for watching this episode of 3D game programming in Chava give it a like because it's very long and because there hasn't been an episode in so long so yeah and um I'll see you I'll see you guys next week bye oh [Music]

Original Description

Like for the longest episode yet! Episode 33: http://www.youtube.com/watch?v=OTGxX_mRV-o Welcome to a series of videos where we will create a 3D game (probably a first person shooter) from scratch, using just the included libraries in Java 1.6. If you have any questions or problems, leave a comment or send me a message, or tweet at me. In this tutorial: - How to create a timer - Why you should add comments into your code - How to create an entity system / framework Twitter: http://twitter.com/#!/thecherno Facebook: http://facebook.com/thecherno
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from The Cherno · The Cherno · 39 of 60

1 3D Game Programming - Episode 1 - Window
3D Game Programming - Episode 1 - Window
The Cherno
2 3D Game Programming - Episode 2 - Game Loop
3D Game Programming - Episode 2 - Game Loop
The Cherno
3 3D Game Programming - Episode 3 - Arrays
3D Game Programming - Episode 3 - Arrays
The Cherno
4 3D Game Programming - Episode 4 - Drawing Pixels!
3D Game Programming - Episode 4 - Drawing Pixels!
The Cherno
5 3D Game Programming - Episode 4.5 - How Rendering Works
3D Game Programming - Episode 4.5 - How Rendering Works
The Cherno
6 3D Game Programming - Episode 5 - Playing with Pixels!
3D Game Programming - Episode 5 - Playing with Pixels!
The Cherno
7 3D Game Programming - Episode 6 - Performance Boosting
3D Game Programming - Episode 6 - Performance Boosting
The Cherno
8 3D Game Programming - Episode 7 - FPS Counter
3D Game Programming - Episode 7 - FPS Counter
The Cherno
9 3D Game Programming - Episode 8 - Alpha Support and More
3D Game Programming - Episode 8 - Alpha Support and More
The Cherno
10 3D Game Programming - Episode 9 - Beginning 3D
3D Game Programming - Episode 9 - Beginning 3D
The Cherno
11 3D Game Programming - Episode 10 - Floors and Animation
3D Game Programming - Episode 10 - Floors and Animation
The Cherno
12 3D Game Programming - Episode 11 - Rotation
3D Game Programming - Episode 11 - Rotation
The Cherno
13 3D Game Programming - Episode 12 - User Input
3D Game Programming - Episode 12 - User Input
The Cherno
14 3D Game Programming - Episode 13 - Render Distance Limiter!
3D Game Programming - Episode 13 - Render Distance Limiter!
The Cherno
15 3D Game Programming - Episode 14 - Basic Mouse Movement
3D Game Programming - Episode 14 - Basic Mouse Movement
The Cherno
16 3D Game Programming - Episode 15 - Textures + More!
3D Game Programming - Episode 15 - Textures + More!
The Cherno
17 3D Game Programming - Episode 16 - Walking, Crouching, Sprinting + More
3D Game Programming - Episode 16 - Walking, Crouching, Sprinting + More
The Cherno
18 3D Game Programming - Episode 16.5 - Exporting Runnable Jars
3D Game Programming - Episode 16.5 - Exporting Runnable Jars
The Cherno
19 3D Game Programming - Episode 17 - Small Adjustments + Birthday!
3D Game Programming - Episode 17 - Small Adjustments + Birthday!
The Cherno
20 3D Game Programming - Episode 17.5 - Creating an Applet
3D Game Programming - Episode 17.5 - Creating an Applet
The Cherno
21 3D Game Programming - Episode 18 - The Beginning of Walls
3D Game Programming - Episode 18 - The Beginning of Walls
The Cherno
22 3D Game Programming - Episode 18.1 - A Few More Things
3D Game Programming - Episode 18.1 - A Few More Things
The Cherno
23 Episode 18.5 - Creating an EXE File in Java
Episode 18.5 - Creating an EXE File in Java
The Cherno
24 3D Game Programming - Episode 19 - Rendering Walls
3D Game Programming - Episode 19 - Rendering Walls
The Cherno
25 3D Game Programming - Episode 20 - Continuing Walls, Fixing Bugs, and Managing Crashes
3D Game Programming - Episode 20 - Continuing Walls, Fixing Bugs, and Managing Crashes
The Cherno
26 3D Game Programming - Episode 21 - Texturing Walls, Fixing Clipping, and Fixing the Mouse
3D Game Programming - Episode 21 - Texturing Walls, Fixing Clipping, and Fixing the Mouse
The Cherno
27 3D Game Programming - Episode 22 - Random Level Generator + Properly Fixing Clipping
3D Game Programming - Episode 22 - Random Level Generator + Properly Fixing Clipping
The Cherno
28 3D Game Programming - Episode 23 - Graphical User Interface (GUI) Launcher
3D Game Programming - Episode 23 - Graphical User Interface (GUI) Launcher
The Cherno
29 3D Game Programming - Episode 24 - Making Our Launcher Work
3D Game Programming - Episode 24 - Making Our Launcher Work
The Cherno
30 3D Game Programming - Episode 25 - Writing and Reading Files
3D Game Programming - Episode 25 - Writing and Reading Files
The Cherno
31 3D Game Programming - Episode 26 - Custom Resolutions
3D Game Programming - Episode 26 - Custom Resolutions
The Cherno
32 3D Game Programming - Episode 27 - Decorating the Launcher
3D Game Programming - Episode 27 - Decorating the Launcher
The Cherno
33 3D Game Programming - Episode 28 - Continuing our Custom Launcher!
3D Game Programming - Episode 28 - Continuing our Custom Launcher!
The Cherno
34 3D Game Programming - Episode 29 - Launching The Game
3D Game Programming - Episode 29 - Launching The Game
The Cherno
35 3D Game Programming - Episode 30 - Colour Processing In-Depth
3D Game Programming - Episode 30 - Colour Processing In-Depth
The Cherno
36 3D Game Programming - Episode 31 - Sprites!
3D Game Programming - Episode 31 - Sprites!
The Cherno
37 3D Game Programming - Episode 32 - Sprite Mapping
3D Game Programming - Episode 32 - Sprite Mapping
The Cherno
38 3D Game Programming - Episode 33 - High Resolution Rendering
3D Game Programming - Episode 33 - High Resolution Rendering
The Cherno
3D Game Programming - Episode 34 - Entities
3D Game Programming - Episode 34 - Entities
The Cherno
40 Genesis - My Game for Ludum Dare 24
Genesis - My Game for Ludum Dare 24
The Cherno
41 Vlog + Ludum Dare Results
Vlog + Ludum Dare Results
The Cherno
42 Game Programming - Episode 1 - Resolution
Game Programming - Episode 1 - Resolution
The Cherno
43 Game Programming - Episode 2 - Threads
Game Programming - Episode 2 - Threads
The Cherno
44 Game Programming - Episode 3 - Game Loop
Game Programming - Episode 3 - Game Loop
The Cherno
45 Game Programming - Episode 4 - Window
Game Programming - Episode 4 - Window
The Cherno
46 Episode 5 - Buffer Strategy
Episode 5 - Buffer Strategy
The Cherno
47 Game Programming - Episode 6 - Graphics Initialized
Game Programming - Episode 6 - Graphics Initialized
The Cherno
48 Game Programming - Episode 7 - Buffered Image and Rasters
Game Programming - Episode 7 - Buffered Image and Rasters
The Cherno
49 Game Programming - Episode 8 - The Screen Class
Game Programming - Episode 8 - The Screen Class
The Cherno
50 Game Programming - Episode 9 - Rendering Pixels
Game Programming - Episode 9 - Rendering Pixels
The Cherno
51 Game Programming - Episode 10 - Clearing the Screen
Game Programming - Episode 10 - Clearing the Screen
The Cherno
52 Game Programming - Episode 11 - "Out of Bounds, Baby!"
Game Programming - Episode 11 - "Out of Bounds, Baby!"
The Cherno
53 Game Programming - Episode 12 - Negative Bounds
Game Programming - Episode 12 - Negative Bounds
The Cherno
54 Game Programming - Episode 13 - Timer
Game Programming - Episode 13 - Timer
The Cherno
55 Game Programming - Episode 14 - FPS Counter
Game Programming - Episode 14 - FPS Counter
The Cherno
56 Episode 15 - Tiles
Episode 15 - Tiles
The Cherno
57 Game Programming - Episode 16 - The Map
Game Programming - Episode 16 - The Map
The Cherno
58 The Walls 2 - Minecraft PvP Survival Map
The Walls 2 - Minecraft PvP Survival Map
The Cherno
59 Game Programming - Episode 17 - Key Input
Game Programming - Episode 17 - Key Input
The Cherno
60 Game Programming - Episode 18 - Controlling The Map
Game Programming - Episode 18 - Controlling The Map
The Cherno

This video teaches the basics of 3D game programming using Java, covering entities, game state updates, and collision detection. It provides a comprehensive understanding of how to create and manage entities in a 3D game environment.

Key Takeaways
  1. Create an Entity class with properties like X and Y location and removed status
  2. Use inheritance to avoid code duplication
  3. Implement movement and rotation using vectors
  4. Use ArrayList to hold and update entities
  5. Set up input handling for player movement
💡 Using an ArrayList to hold and update entities is an efficient way to manage game state in a 3D game environment.

Related AI Lessons

How to Create a Second Version of Yourself Inside Obsidian Using AI (Step-by-Step Guide)
Learn to create a second version of yourself inside Obsidian using AI with a step-by-step guide
Medium · ChatGPT
How to prepare for Spain civil service TIC exam using AI in 2026
Learn how to prepare for the Spain civil service TIC exam using AI in 2026, boosting your chances of success with technology-driven study techniques
Dev.to · David García
Going Viral! How I Created AI Kissing Videos Step by Step Easily Using AIAI.com
Create viral AI kissing videos using AIAI.com in a step-by-step process, leveraging AI technology for creative content creation
Medium · AI
How to prepare TIC teacher exams in Spain with AI (oposiciones 2026)
Prepare for TIC teacher exams in Spain using AI with these actionable steps
Dev.to AI
Up next
Low-Tech, High-Impact: Replacing Your Receptionist With a $15 AI Phone System
Maximum Lawyer
Watch →