Collision Detection - How to Make a 2D Game in Java #6
Summary
TLDRIn this tutorial, RyiSnow demonstrates how to implement collision detection in a game development project. The video guides through setting solid tiles in the TileManager, adjusting player collision areas using a Rectangle class, and creating a CollisionChecker class to prevent the player from walking through obstacles. RyiSnow also discusses the importance of precise collision settings for better game mechanics and player movement control, concluding with a preview of upcoming features like object creation and interaction.
Takeaways
- ๐ The video is a tutorial on implementing collision detection in a game world map.
- ๐ ๏ธ The 'Tile' class has a 'collision' boolean that is used to define solid tiles like walls and water.
- ๐ฎ Player movement is restricted by setting the collision property to 'true' for solid tiles to prevent walking through them.
- ๐ Collision settings are applied to the player character by defining a 'solidArea' within the player's tile.
- ๐ The 'Rectangle' class is used to create a collision area for the player character with specified x, y, width, and height.
- ๐พ The 'solidArea' is made smaller than the player's tile to allow for better movement and control in the game.
- ๐ซ Collision detection prevents the player from moving into a solid tile, enhancing game realism and mechanics.
- ๐ A 'CollisionChecker' class is created to handle collision checks for the player, monsters, and NPCs.
- ๐ The 'GamePanel' class is used to instantiate the 'CollisionChecker' and integrate it with the game's update method.
- ๐งฉ By calculating the player's 'worldX' and 'worldY' based on the 'solidArea', the game checks for collisions in four directions.
- ๐ The tutorial concludes with the successful implementation of collision detection, with plans to create and display game objects in the next video.
Q & A
What is the main topic of the video?
-The main topic of the video is implementing collision detection in a game development program to prevent the player from walking through solid objects like trees and water.
What is the purpose of the 'collision' boolean in the Tile class?
-The 'collision' boolean in the Tile class is used to designate whether a tile is solid or not, thus preventing the player from walking through it.
Why is it important to set only specific parts of the player character as solid?
-Setting only specific parts of the player character as solid helps to avoid issues like getting stuck on walls and improves the control mechanics, allowing for more precise movement through narrow paths.
What is the Rectangle class used for in the context of collision detection?
-The Rectangle class is used to create an invisible or abstract rectangle that defines the solid area of the player character for collision detection purposes.
What is the default value of the 'collisionOn' boolean?
-The default value of the 'collisionOn' boolean is false, meaning that by default, tiles are not considered solid for collision detection.
How does the video script suggest determining the solid area for the player character?
-The script suggests using the Rectangle class to define a specific area of the player character as solid, adjusting the x, y, width, and height parameters to fit the desired solid area.
What is the CollisionChecker class responsible for?
-The CollisionChecker class is responsible for checking collisions between entities, such as the player, monsters, and NPCs, and the game world's tiles.
Why is it necessary to check the worldX and worldY of the player's solidArea for collision detection?
-The worldX and worldY of the player's solidArea need to be checked because collision detection must be based on the coordinates of the solid part of the player character, not the player's overall position.
How does the script handle checking collisions in different directions?
-The script uses a switch statement to handle different directions, calculating the predicted position of the player after movement and checking the tiles that the player's solidArea would collide with in that direction.
What is the next step mentioned in the video script after implementing collision detection?
-The next step mentioned in the video script is to create objects and display them on the screen for the player to interact with, such as picking them up.
Outlines
๐ณ Implementing Collision Detection
In this segment, RyiSnow discusses the process of adding collision detection to a game world. Initially, the player could walk through trees and waters, but now RyiSnow will modify the Tile class to include a collision boolean. This boolean is set to 'true' for solid tiles like walls and water, preventing the player from passing through. The default for non-solid tiles, like grass, is 'false'. RyiSnow also emphasizes the importance of defining the player's solid area to avoid issues with movement and control mechanics, suggesting that not the entire player tile should be solid, but only a specific part, which is done using the Rectangle class to create a solidArea.
๐ Setting Player Collision Area
This paragraph describes how to set the player's collision area within the game. RyiSnow explains that the player's solid area can be defined by creating a smaller rectangle within the player's tile, which is done by adjusting the x, y, width, and height parameters. The goal is to make the player's solid area more precise, allowing for better control and movement through narrow paths. RyiSnow provides an example of setting these parameters and suggests that developers can adjust these values to suit their game's needs.
๐ Creating a Collision Checker Class
RyiSnow introduces the creation of a CollisionChecker class to handle collision detection for the player, as well as for other entities like monsters and NPCs. The class is instantiated within the GamePanel and is used in the Player class's update method to check for collisions. The CollisionChecker receives an Entity and checks if the player's solidArea is hitting a solid tile by calculating the worldX and worldY coordinates of the solidArea and determining the corresponding column and row numbers in the game's tile grid.
โฌ๏ธ Collision Detection for the 'Up' Direction
In this part, RyiSnow focuses on the collision detection logic for when the player moves 'up'. The player's future position is predicted by subtracting the player's speed from the solidArea's worldY coordinate. The script then calculates the row number and checks two tiles in the 'up' direction for collisions. If either of the tiles is solid, the player's movement is blocked. RyiSnow also mentions the need to make certain variables public for the collision detection to function correctly.
โ๏ธ๏ธ Expanding Collision Detection to Other Directions
Having established the collision detection for the 'up' direction, RyiSnow proceeds to extend this logic to the other directions: 'down', 'left', and 'right'. The script involves adjusting the row and column checks based on the player's intended movement direction. For 'down', the bottom row is checked; for 'left', the left column; and for 'right', the right column. The process involves checking the tiles at the player's predicted new position and ensuring that the movement is allowed only if no solid tiles are encountered.
๐ฎ Finalizing Collision Detection and Previewing Future Steps
In the final paragraph, RyiSnow tests the collision detection system, confirming that the player character is now stopped by solid tiles like trees and waters. The script demonstrates that the collision detection works in all directions, enhancing the game's realism and playability. RyiSnow concludes by mentioning the next steps, which involve creating objects in the game that the player can interact with, such as picking them up.
Mindmap
Keywords
๐กCollision Detection
๐กTile Class
๐กTileManager
๐กSolid Tile
๐กPlayer Character
๐กRectangle Class
๐กCollisionOn
๐กGamePanel
๐กEntity
๐กUpdate Method
๐กSwitch Statement
Highlights
Introduction to implementing collision detection in a game development tutorial.
Utilization of the 'collision' boolean in the Tile class to prevent player movement through solid objects.
Setting specific tiles as solid by modifying the TileManager to enhance gameplay realism.
Explanation of the default 'false' collision setting for non-solid tiles.
The importance of defining solid areas within the player character for better control mechanics.
Demonstration of how setting the entire player tile as solid can restrict movement unnecessarily.
Discussion on the challenges of navigating narrow paths with solid collision settings.
Introduction of the Rectangle class to define solid areas within the player character.
Customization of the player's solid area to allow for more precise movement and control.
Instantiation of the solidArea rectangle with specific parameters to fit the player character's design.
Creation of the CollisionChecker class to manage collision detection for various entities in the game.
Integration of the CollisionChecker class within the GamePanel for broader collision detection.
Implementation of collision checks within the Player class's update method.
Calculation of the player's solidArea world coordinates for accurate collision detection.
Use of switch statements to handle different player directions and corresponding collision checks.
Detailed explanation of the 'up' direction collision detection process.
Adjustment of player movement code to account for collision results before allowing movement.
Expansion of collision detection to include other directions: down, left, and right.
Final demonstration of the game's improved collision detection in action.
Teaser for the next tutorial segment focusing on creating and displaying game objects.
Transcripts
Hi guys, this is RyiSnow
so last time, we loaded our world map
and implemented a camera function
so now we can move around the world, like this
but still, we can walk through these trees and waters
so in this video, we will implement collision detection in our program
so the player won't be able to walk through solid tiles
when we created this Tile class, we created this collision boolean
so it's time to use this
first, we go to this TileManager
and look for a tile that we want to change it to a solid tile
for example, this wall tile should be solid
we don't want our player character walk through this tile
we call this collision boolean and set it "true"
also this water tile... this one too
and this one too
and if it's not a solid tile... like this grass tile
then you don't need to add anything because the collision is false as default
so you only need to add this if the tile is solid
ok so tiles' collision setting is done
then we decide which part of the player character is solid and which part is not
the easiest way is setting the whole tile is solid
but sometimes it won't produce the best result
for example, if the whole player tile is soid,
then he hits this wall at this point and cannot move further
but maybe you want to move him a bit further... like this
also, there's another issue
if everything is solid, it's really difficult to get through a narrow path like this
because even if the player is 1 pixel to the left, collision still happens here
so you need to place him very precisely, like this
and that is simply pretty stressful
and bad control mechanics in my opinion
for example, "oh i'm hitting this tile so let's move to the right little bit"
and "oh now i'm hitting this tile and still i cannot go further"
so to avoid this kind of hassle,
we set a specific area of the character as solid, not everything
like, only this part is solid
then the player can get through this kind of narrow path
and to set this kind of collision area,
first, open this Entity
we use this Rectangle class
and i'm gonna name this solidArea
with this class, we can create an invisible or abstract rectangle
and we can store data such as x, y, width, and height
also, we create a boolean called collisionOn, and the default is false
now go to this Player class
and instantiate that rectangle
wait, oh what is this... soidArea... sorry
and when we instantiate this rectangle, we can pass 4 parameters to the constructor
for example, if you want to create a rectangle which is the same size as our tiles,
you type like this:
x = 0, y = 0, widht = 48, and height = 48
or you can also type like this: gp.tileSize
either way is fine
or we can also leave this blank and set these values one by one
either way is fine
and since we want to make this rectangle a bit smaller than the player character
we change these values a little bit
maybe something like this
so this gray area is not solid, and this area is solid
so this slid area starts from x 8
and width is 32
and height is also 32
so the top left corner is x = 8, and y = 16
so in that case, we're gonna change this x to 8, and y to 16
and widht is 32 and height is also 32
you can adjust these numbers and find what works best for your game
so we got this collision area for the player character
now let's create a class to check collision
inside this main package, create a class
and i'm gonna name this CollisionChecker or whatever name you want
and first, constructor
and we receive GamePanel
and create a method
and we receive Entity here
not Player but Entity
because we will use this method to check not only player collision but also monster and NPC collisions as well
now go to the GamePanel
and instantiate this CollisionChecker class
and pass this GamePanel
and now go to this Player class
and we check collision inside of this update method
after this direction if statement,
first, we set this collisionOn to false
then call this...
oh sorry
this needs to be "public"
so we're gonna call this checkTile method from here
and pass this Player class as Entity
since this Player class is a subclass of this Entity class
so the CollisionChecker can receive the Player class as Entity
alright, now the main part of this video
so what we need to do is, to check if the player is hitting a solid tile or not
to do that, first, we want to know the worldX and the worldY of the player's solidArea
not the player's worldX and Y because collision needs to be detected based on these solidArea coordinates
there are 4 points to be checked
and we can find out these 4 numbers like this:
then based on these coordinates, we will find out their column and row numbers
we simply divide it by tileSize
and finally we will create two more integers
because basically, we only need to check two tiles for each direction
if the player is going up,
we only need to check what tiles, so the player's left shoulder and right shoulder are hitting
so in this case, if the player is going up like this
this is hitting maybe this wall tile
and this right shoulder is hitting this grass tile
so now we're gonna create a switch statement
we're gonna check the entity's direction
so let's take care of this "up" first
for example, the player is here right now
and he is trying to go up
what we are doing here is, we kind of predict where the player will be after he moved
so we subtract this player's speed from the player's worldY
or the player's solidArea's worldY, to be precise
now we know this row number so we can find out what tile the player is trying to step in
and there are possibly two tiles
so we're gonna call this mapTileNum
this stores all the map tile information
oh this also needs to be "public"
and the other one is...
we checked this point, this is tile1
now we want to check this point, the right side
with this information, we create an if statement
and call this Tile array... this also needs to be "public"
we're gonna use this tileNum1 as an index
and check if this tile is solid or not
if one of them or both are true, the player is hitting a solid tile
so he cannot move this direction
so if these collisions are false, then we don't do anything
ok so this "up" case is done
let's go back to this Player class
and now we're gonna move these player movement lines from here to here
maybe i like this format better...
yeah because now we're checking collision so the player can move only when the tiles are not solid
so first we only check the direction
and based on this direction, we check collision
and if collision is not happening, then we let the player move
ok let's check this
so now this tree tile is stopping the player character
but it's still only the up direction
you can still go left, down, and right
so let's take care of the other directions too
i think we can copy and paste
we only change some variables
so if the player is going down, we're not gonna check the top row but the bottom row
so change this to bottomRow
we're gonna change this from top to bottom
and if the player is going left then...
we're gonna check the left col
and the left col will be...
so now we're gonna check this point and this point
if going right then...
that's it, i think
ok let's check this
ok hitting the tile
and this water tile too
so collision is happening
yup, looks good
so this is getting more and more like a game
so we took care of the collision detection
next time, we will create objects and display them on the screen
so the player can pick them up
thanks for watching, and until next time
Browse More Related Video
How To Make a Topdown RPG in Godot 4: Adding The Player (Part 1)
How To Make a Topdown RPG in Godot 4: Adding Classes and Projectiles (Part 5)
Lecture: 14 | WATER BOAT GAME IN SCRATCH | GAME DEVELOPMENT | SCRATCH TUTORIAL
Unity 2D CHECKPOINTS Tutorial (Simple and Easy!) | Unity 2D Platformer Tutorial #9
Pygame Movement - Pygame RPG Tutorial #2
DX100 - Collision Detect (Code 4315)
5.0 / 5 (0 votes)