Collision Detection - How to Make a 2D Game in Java #6

preview_player
Показать описание
In this video, we will implement collision detection so player character stops when he tries to walk through solid tiles.

Caution: This is a tutorial for Java 2D beginners so it's pretty verbose! (and the pacing is a bit slow...)

If you want to use my assets (images/maps/sound) that are used in this tutorial, here's the link:

Guidelines for using Blue Boy Adventure's code and assets:

If you have any questions, feel free to leave a comment.
Thanks for watching.

Timestamps:
0:00 Set tile collision status
1:54 Set player's solid area
7:28 Set up Collision Checker
10:28 Detect collision
25:40 Final result

#javagamedevelopment #java2D #javagametutorial #javagameprogramming
Рекомендации по теме
Комментарии
Автор

what the hell, dude being haunted in 1:09 and kept recording, absolute legend.

DaniloMotaSoares
Автор

For anyone who may run into the same problem I was, where despite my code being the same as what is in the video, my player character wouldn't move, make sure that you have '==' in your if statements and not a singular '='. I was overlooking this because it's such a small detail, but it was making the body of my if statements unreachable and preventing movement. After changing this, my character was able to move.

samjesf
Автор

Thank you, RyiSnow, this is a wonderful series. Learning a lot!
A small addition: If you still want to be able to change the scale factor one should base the collision rectangle coordinates on the 16x16 image (not the 48x48) and multiply it by the scale factor upon initializing it in the player class.

Tefrax
Автор

Man, you're a legend, thank you for your tutorials! 👍

savkorlev
Автор

If your character get stuck on collisions sometimes, check if changed the - to a + in case "down" and case "right" in the CollisionChecker.

natzos
Автор

I was stuck for a couple of days and then the solution came to me in a dream

source: a dream

BracamonteL
Автор

Great video! I just want to mention since theres a few people in the comments who ran in the same issue, there is an issue when "solidArea.x" is made equal to a ratio of the "tileSize" if you want to maintain the "scale" functionality because it is integer division and the compiler will round it up or down to unpredictable number and the collision detection will work very strangely as the collision box will not be where you expect it. The solution is to use hardcoded numbers like in the video, or make the tileSize a "double" and not "int", but this would require everything else that tileSize interacts with to be recast to double instead of int. I'm guessing this is why you used numbers in the video instead of tileSize.

AlexanderHL
Автор

Thank you so much, I really enjoy these videos they're so helpful, you deserve so much more views!

Kandiman
Автор

For anyone who finds their player character gets stuck, make sure that you have a ‘break’ in each switch statement for the collision checking. Otherwise all 4 collision checks are done for each movement and you’ll get stuck.

jwfinn
Автор

This guy is a wizard - great tutorial so far!

GuildOfCalamity
Автор

Been following along the past few days and have felt so proud where I am thanks to you. I cant wait till I learn more so I can add quests and npcs!

Mypremiumacct
Автор

Also thanks for making this series, i needed it so much!

bluethemcguy
Автор

With great help of your videos I might be able to make a tile based strategy, and then a 3D version of it, and not only it. Thanks for the help 🙂😌

六工業のカービィ
Автор

Another great video, these are so helpful and really appreciated, thanks so much!

kandiman
Автор

Thanks for the video, I was confused on how to implement a Collision Detection

yuvtajvirsingh
Автор

I've actually been having a bit of trouble when it came to player movement after moving the world speed to the if-statement: if(collisionOn == false) for the player class. For me, after making this change, the player just keeps moving in whatever direction that it is facing. The problem started at around 22:50 for reference. Any ideas that could have caused this problem?

_chaozgt_
Автор

I can't wait to jump in on this guide as I've tried to understand how collision was detected for a while. Your guide is the only one I understand!

droidlycodes
Автор

your videos are indeed very clear and helpful

vasilisavasya
Автор

I've been waiting for your new video for 1 week
Hope you make more videos in the future

tranminhquang
Автор

If u have the problem that u ato walk after u included the collision for the "up" statement is here the solution: Import the code (to be seen at 22:44) on top to stop it from looping. It was created a few parts ago to make the player stop moving when hes standing. Hope I can help.

jojo.-tv