Game Programming - Episode 63 - Collision Detection

The Cherno · Intermediate ·🧒 Coding for Kids ·13y ago

Key Takeaways

The Cherno implements collision detection in a game using Java, covering topics such as solid and non-solid tiles, collision response, and tile-based collision detection. The video series takes an in-depth look at game programming from scratch.

Full Transcript

hey what's up guys My Name Is AO welcome to episode 63 of game programming all right so today we're going to talk about Collision detection yep that's right Collision detection um probably the single most heavily requested topic um for me to cover ever has been Collision detection and I can kind of get why like I'm not going to be like oh I have no idea why it's so easy but um I I do kind of get why you guys you know would want would want to you know I guess learn how to do it obviously it's very important to making games but also in my opinion it's probably one of the most hardest easier things to do in game programming because it's kind of hard to to visualize like if you were to go out and be like all right I'm not going to look at any resources I'm just going to I'm just going to make Collision detection from scratch logically in my head and I'm going to write it down on a piece of paper and then implement it into my code and the answer is it'll probably work right unless you're you know um kind of I don't know how to put this in a polite way unless you're not educated let's just say it'll probably work right but it's probably not going to be the most efficient way to um actually handle Collision I remember the first time that I actually made Collision detection I didn't look at any resources I just said logically in my head all right well you know if this part of this Square collides with this Square you know if this part of this Square exceeds the X boundaries of another Square you know and do the same for every other Corner pretty much um if that happens then there is collision and then but now then we've got the other issue of we've detected Collision but there has to be Collision response right we've detected Collision but we have to we have to kind of react to it you know what I mean so detecting Collision is one thing it's pretty easy to do actually it's just you know if if this is inside this other object then we've got a problem you know that's that's a collision but then there's also the the aspect of it that okay we can't move in that direction now how do we make the player not be able to move in a certain direction based on if there's Collision there or not and again the answer is extremely simple and it's literally one line of code I'm not even going to lie it's one line of code that is the most like that is that is pretty much that's basic Collision detection right there um but we're not actually going to do that we're going to do a bit of a more advanced way and I'll show you guys the simple way first by the way so that you guys can wrap your head around how it roughly works and then I'll lead into the more advanced way but um it's just more accurate that's why we're going to do it in a more advanced way not just because I want to but um it's yeah it's it's actually not that hard it it probably sounds hard and feels hard but it's not that hard okay so right now um what we've got in our game is we've got these things here right and let me quickly okay good I was worried I would run out of disc bace because I always sometimes do that when I'm recording uh but but it's all right I got 3.65 gig left on my uh my on my main Drive okay so um what we've got right now is we're kind of just prancing all over this um all over these tiles and remember we've actually set these tiles to this uh but I don't know what this is this wall I guess these walls we've actually set them to be solid so if we just get out of this we expand our little um level thing our tile folder and we've got a bunch of tiles here like um in in in spawn level we've got wall tile and in wall tile you know public bullying solid it returns true so in other words that is true if we look at something like grass tile that actually doesn't have a solid um method I guess doesn't have a solid method um because by default if it doesn't have a solid method it looks like it looks at the um the super class and you can see here that it returns false right um same with Flor tile uh but wall tile and hedge tile they actually return true and solid and that's actually breakable but I don't know we we haven't imple implemented that yet but um yeah so they're solid right now hedge and wall tiles are solid um so that's that's the first step right the first step in Collision detection is actually to identify what tiles are solid and what are not solid why does my mouse drop out of I don't know my mouse kind of stops working sometimes that's weird haven't seen that before anyway um yeah right so the first step is actually um defining which tiles you know you can't pass through and which tiles you can pass through so this tile obviously we can't pass through right we've set it to to return true so in other words it is solid we've set it to be solid now let's go into our mob class right and if you guys remember the mob class is the class that actually handles the move method so in other words if we want to move a mob if we want to move an object well like if we want to move a mob then we have to you know that's that's where that move method comes from if we want to move a mob we go through the mob class this is this is how it happens and we've even set up a temporary Collision thing here right we've pretty much just laid down all the groundwork for this we've said that if there is no Collision so if Collision equals false right this is a it's referring to this bulling here by the way it's currently returning false but if it equals false then yeah let's advance in those directions but if there is a collision then you cannot move so that's kind of collision response already done right and that's obviously a very simple way of doing it but that's kind of how it works all right so how do we actually you know stop ourselves from moving through those collidable tiles um yeah so how do we actually you know stop our play from being able to pass through these tiles um the way we actually do that is obviously we need to run an algorithm here in um the Collision method right because basically if we return true for this Collision then we're just not going to be able to move and you can see that here that we just we can't move anywhere right cuz Collision is returning true um but what we want to be able to do is only return true in certain cases so another wayse we want to control this this bullying value here right and I'm just going to call it solid equals false for now and let's return solid all right so what I've done here is I've just you know created a variable out of it but um we want be able to control this bulling we want to be able to say that based on where we are moving to you know if that tile is solid and don't let us get into that tile because it's solid right so what we kind of want to say is that if I'm moving this way hypothetically well I am actually moving this way but if I'm moving this way and I get to here then let's look ahead of where I'm moving which is this tile but wait that tile is solid so we can't move further return false and that's the idea honestly that is the idea so we want to be like if we move that way then you know don't don't let us go there um and the other way that we can actually um picture this as well a lot of people make a mistake of saying if the tile that I'm currently on is a solid tile don't let me move and that doesn't really work when you think about it because it's like well if you're already on a solid Tire like you shouldn't even be there in the first place if if you're already there it's kind of too late like you're already on it so we I'm not going to let you out of it now it's a solid tile you can't move now so we want to kind of look ahead and how do we so how do we look how do we look ahead right that's that's another question how do we look ahead and if you see over here actually the direction in which we move is defined by XA and ya a right they are either - 1 0 or 1 and negative 1 means we're moving you know in X's case we're moving left zero means we're not moving anywhere and y means we're moving right so if we come over here right we've already got a way of actually looking forward XA and ya it tells us which direction we're going in so in other words over here if we just simply add XA to X we're going to get the tile that we're about to be in right right so one thing I'm going to do is actually feed in XA and ya as parameters into this Collision method because I want to be able to use those variables to figure out where we're going and the tile that we're future that will be in in the future so that we can run and check through it and say if it's you know if it's solid then return false um or rather return true so over here the way that we kind of check that is and I'm trying to break it down like because this is one line of code and I want to explain it as much as I can but basically if level. get tile right and what level. get tile is is it returns to us all of the properties of a particular tile so if level. get tile right and which tile are we getting here we're getting tile x + x a and y + y a so in other words x is the current position of where we are in terms of X and XA is where we're going to move in terms of X so our future location in terms of X right um what same with Y right Y is our current location in Y and Y is where we're going to move in terms of Y so up or down and that's obviously left and right left or right so if level. get tile and we're getting this particular tile here if it is solid right then solid equals true oops and that ladies and gentlemen is Collision detection honestly so let's run this and see what we get okay so we get a crash that's okay the reason we get a crash and I actually forgot to mention this was that we don't actually have level set up right if we actually control click on level it leads to this and obviously level doesn't equal anything so we get a null pointer exception what we need to do is actually initialize the the level inside the entity class so what I like to do is make a um a method called in it stands for initialize right and all we'll do here is it will actually take a parameter a level object parameter so this do level equals level and that's it right so in other words the parameter that we that we will feed in here will be set to this and this of course we use here so let's go back to our game. Java class that's our main class um and over here you see that we actually do have a player right here and player is obviously a mob so what we can do here is we can say player. init and we can actually set level equal to this level and of course that level is actually equal to level do spawn so in other words it is equal to something and if we run this now we don't get a crash all right so let's check this out ready ready ready oh we passed right through it now why did that happen I wonder now the reason that that happen is actually pretty simple I love how I just kind of mess with you guys you're probably expecting it to stop um if we actually check out what this value equals so if we just quickly print out X Plus x a and just for good measure we'll also print out y + y okay um we print out those variables when we move we actually get oh wow like look how big they are 10,000 and 200 why are they so big and the reason they're so big is they're returning it in terms of pixels not tiles and if we go to get tile method you know we're actually looking at a particular tile right so tiles obviously is in terms of um ah tiles is actually in terms of T what what do we even hang on minute where is tile set exactly um guess it would be in spawn level yeah so here tiles are set to width times height which at the moment is 64x 64 so of course that freaking exceeds it was like not actually 64x 64 but anyway um it's set to tile level Precision right but we're talking about um pixel level Precision here clearly so what we you need to do is convert it back into tile level precision and the way that we can do that is pretty simple we can um we can simply grab X Plus XA put in Brackets and divide it by you know how big our tiles are which 16 that's it that is how simple it is so now take two right um now if we do this okay first of all let's get rid of this printing thing but actually just to prove a point if I do quickly copy that and put it here all right then you can see that look at that we're now intire level Precision so if I go here look at that I cannot go there if I go down look at that I can't can't go down um if we actually come up here as well so I can prove to you how it works if we go here oh look at that we cannot go past here right if we go that we can't can't can't go there can't go there look at that it's working brilliantly now one thing that is there's two kind of problems with this collision and why I wouldn't just leave it like this this is basic so you guys can kind of see how it works right but um one thing that we can't do is if we're are colliding here we can't actually move up and down while we're holding that key right it stops us from doing that and the reason it stops us from doing that is because well we've already collided so as long as we're trying to go that way we can't move but what we want to kind of do is say that okay the Collision is horizontal so let us still move vertically that's one thing we need to do the other thing we need to do is actually make this a bit more precise so you can see that we've don't really have control here in fact that didn't even work because we weren't in the right place as you can see here um yeah so there's a few kind of quirks with this thing obviously the basis is it works fine but I want to I I like to make it just a bit more accurate um and we can actually do that next time and I promise that the next episode will be released in like a few days it won't be like a few weeks couple weeks it'll be a couple days um but yeah that's basically how Collision Works so I suggest that you guys um kind of go through that and see if you can understand it fully if you guys do come up with with a solution by the way I um I actually have a a subreddit called The Cho big surprise but um it's over here right and I do go through this occasionally but people post stuff here like I don't know like Alternatives actually I haven't read that obviously but you can see most of these links are purple so hey I do read it in fact I've even uploaded this link um probably haven't helped them with it though but anyway the point is um this is this is a subreddit and what you guys can do is you can obviously submit um posts and if you submit a post you can share your way of collision detection um or if you've got a problem you can also submit that obviously and people will help you out cuz people here are really nice unlike me so yeah anyway the point is if you guys do come up with a um with a with a with what you think is a more accurate way of actually dealing with Collision post it here you know I'll check it out and maybe other people can check it out as well and maybe they'll share their ways and maybe we can get an get an even better way of handling Collision than what I've got right now um obviously next time regardless of if any posts actually occur I'll actually share with you guys the way that I obviously make this more accurate and that's probably the way I'll do it anyway cuz it works 100% of the time and it's really really nice but um that's rough Collision detection just cuz I wanted to bring it down bring it down a notch for you guys so that you could really understand Collision in one L card um because it is kind of confusing to wrap your head around but that's kind of how it works anyway guys I'll see you guys next time on episode 64 again couple days um cuz I've actually got a wake off uni now because of EA yay um anyway if you guys enjoyed the video please hit the like button and I'll see you guys next time later guys

Original Description

Welcome to Game Programming, a series in which we take an in depth look at how to make a game from scratch, in Java. ---------------------------------------------------------------------------------------------------------------- Website: http://www.thecherno.com Twitter: http://www.twitter.com/thecherno Facebook: http://www.facebook.com/thecherno Subreddit: http://www.reddit.com/r/thecherno/ Steam Group: http://www.steamcommunity.com/groups/thecherno ---------------------------------------------------------------------------------------------------------------- Outro music is by Approaching Nirvana: http://www.youtube.com/approachingnirvana
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from The Cherno · The Cherno · 0 of 60

← Previous Next →
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
39 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 collision detection in game programming using Java, covering topics such as solid and non-solid tiles, collision response, and tile-based collision detection. The host shares their approach to collision detection and plans to share more in future episodes.

Key Takeaways
  1. Implement collision detection in a game
  2. Define solid tiles by returning true for solid method
  3. Handle move method in mob class for collision detection
  4. Run algorithm in Collision method to stop movement through solid tiles
  5. Control solid value based on movement direction and tile type
  6. Look ahead to check for solid tiles before moving
  7. Feed XA and YA as parameters into the Collision method
  8. Check the solidity of the next tile using Level.getTile
  9. Initialize the level inside the entity class
  10. Set the level parameter in the game class
💡 Collision detection can be a complex topic, but breaking it down into smaller steps such as checking for solid tiles and handling collision response can make it more manageable.

Related Reads

📰
How Creative Characters Like Sprunki Inspire Kids to Learn Coding
Learn how creative characters like Sprunki inspire kids to learn coding through interactive games and animations
Medium · Programming
📰
Computer Applications for Primary School Children: A Fun + Safe Guide for Ages 6-12
Learn how to introduce primary school children to computer applications in a fun and safe way, teaching them essential skills for the future.
Dev.to · Ogunkola Adeola
📰
From 0 to 20 Chapters: My Story‑Driven Rust Book for Kids Now Has a Bilingual Interactive Demo
Create an interactive coding book for kids using Rust, with a focus on storytelling and bilingual support, to teach programming concepts in an engaging way
Dev.to · born1987-ir
📰
How Kids Can Build Fighting Games Stickman in Scratch
Kids can learn coding by building a stickman fighting game in Scratch, developing skills like movement control and collision detection.
Medium · Programming
Up next
Man Builds a Backyard Tiny House and Turns It into a Home Office | Start to Finish by @Elseweyr
AKLA GELEN
Watch →