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

  Рет қаралды 106,645

RyiSnow

RyiSnow

Күн бұрын

Пікірлер: 343
@DaniloMotaSoares
@DaniloMotaSoares 2 жыл бұрын
what the hell, dude being haunted in 1:09 and kept recording, absolute legend.
@envar1
@envar1 6 ай бұрын
The sound is Japanese police driving by his building
@jumpsneak
@jumpsneak 2 жыл бұрын
best Programming series on youtube
@osternosousa
@osternosousa Ай бұрын
The Best!
@Tefrax
@Tefrax 2 жыл бұрын
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.
@hakerananasek
@hakerananasek 2 жыл бұрын
Yess glad someone pointed it out. Those fixed numbers looked out of place to me lol
@savkorlev
@savkorlev 3 жыл бұрын
Man, you're a legend, thank you for your tutorials! 👍
@RyiSnow
@RyiSnow 3 жыл бұрын
Glad you like them! Thank you for the comment.
@hanno-oz3ip
@hanno-oz3ip 2 жыл бұрын
hope this tutorial series never ends
@samjesf
@samjesf Жыл бұрын
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.
@happyburger23
@happyburger23 Жыл бұрын
When I do that, I'm getting an error reading "Not a statement". How do I fix that?
@antoinebguitar2869
@antoinebguitar2869 10 ай бұрын
Its makes huge difference. In programming == typically means to check if a certain value is equal to another, typically used in if statement, while a single = just assign the value so yeah it literally changes meaning so of course it will not work. It's not what i would call small detail haha
@natzos6372
@natzos6372 2 жыл бұрын
If your character get stuck on collisions sometimes, check if changed the - to a + in case "down" and case "right" in the CollisionChecker.
@xcuze
@xcuze Жыл бұрын
you're the goat thanks :P
@Julson_PL
@Julson_PL Жыл бұрын
Thank you too.
@thiccboiigavin5341
@thiccboiigavin5341 Жыл бұрын
tysm
@Mypremiumacct
@Mypremiumacct 7 ай бұрын
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!
@TheCountOfAstora
@TheCountOfAstora 3 жыл бұрын
Thank you so much, I really enjoy these videos they're so helpful, you deserve so much more views!
@droidlycodes
@droidlycodes 2 жыл бұрын
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!
@BracamonteL
@BracamonteL 4 ай бұрын
I was stuck for a couple of days and then the solution came to me in a dream source: a dream
@AlexanderHL1919
@AlexanderHL1919 9 ай бұрын
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.
@blubliblubl6213
@blubliblubl6213 Ай бұрын
Thanks again for the tutorial! I wanted to implement diagonal movement which complecated thing for the collision, but i managed to implement it correctly :)
@bluethemcguy4430
@bluethemcguy4430 3 жыл бұрын
Also thanks for making this series, i needed it so much!
@kandiman9111
@kandiman9111 3 жыл бұрын
Another great video, these are so helpful and really appreciated, thanks so much!
@RyiSnow
@RyiSnow 3 жыл бұрын
You're welcome! Glad I could help :)
@GuildOfCalamity
@GuildOfCalamity 2 ай бұрын
This guy is a wizard - great tutorial so far!
@jwfinn100
@jwfinn100 Жыл бұрын
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.
@bluethemcguy4430
@bluethemcguy4430 3 жыл бұрын
Loved the vid! I was wondering if you would incorporate random terrain generation in your game?
@RyiSnow
@RyiSnow 3 жыл бұрын
Not in this one. This is for 2D game beginners and I'm trying to keep it simple so the code is not overwhelmingly complicated (You'll need to construct an algorithm to do random generation). Maybe I will do in the future though.
@bluethemcguy4430
@bluethemcguy4430 3 жыл бұрын
@@RyiSnow Ok, thank you for replying!
@droidlycodes
@droidlycodes 2 жыл бұрын
I have an idea for this based off my knowledge and how games like Minecraft are loaded. I'll see what I can come up with!
@elginbeloy9066
@elginbeloy9066 2 жыл бұрын
For something simple could use a markov process where tile probability is based on tiles neighbors. This would generate clusters like forests, oceans (w/ sand) or other things depending on those probability values. Tweak and have fun:)
@六工業のカービィ
@六工業のカービィ 2 жыл бұрын
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 🙂😌
@jojo.-tv
@jojo.-tv 9 ай бұрын
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.
@_chaozgt_
@_chaozgt_ 3 жыл бұрын
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?
@RyiSnow
@RyiSnow 3 жыл бұрын
May I see your Player class? You can paste it here.
@ciTatic
@ciTatic 3 жыл бұрын
Looking at the code again, I think I see the issue: all of the movement code is running under an if statement that checks if a key is pressed. I don't have this for some reason and found my own workaround, but you could probably just add that if statement under update() in player. Look for it on line 71 in the video, or you can see it at 22:22 Without this if statement, the only guard on applying speed to position is whether you're colliding or not, and as you usually aren't colliding at this point in the video you'll just move forever.
@_chaozgt_
@_chaozgt_ 3 жыл бұрын
@@RyiSnow package tWoD.src.Entity; import java.awt.image.BufferedImage; import java.awt.*; import java.io.*; import javax.imageio.ImageIO; import javax.swing.*; import tWoD.*; import tWoD.src.Entity.*; import tWoD.src.Main.TwoDGamePanel; import tWoD.src.Main.TwoDKeyHandler; public class Plr extends Entity{ TwoDGamePanel gp; TwoDKeyHandler keyH; public final int screenX; public final int screenY; public Plr(TwoDGamePanel gp, TwoDKeyHandler keyH){ this.gp = gp; this.keyH = keyH; screenX = gp.screenWidth / 2 - (gp.tileSize / 2); screenY = gp.screenHeight / 2 - (gp.tileSize / 2); solidArea = new Rectangle(12, 18, 24, 30);//works by x,y,width,height setDefaultValues(); getPlayerImage(); } public void setDefaultValues(){ worldX = gp.tileSize * 25; worldY = gp.tileSize * 25; speed = 4; direction = "down"; } public void getPlayerImage(){ try{ left1 = ImageIO.read(getClass().getResourceAsStream("..\ sc\\Player\ obotPlayerAWalk_1.png")); left2 = ImageIO.read(getClass().getResourceAsStream("..\ sc\\Player\ obotPlayerAWalk_2.png")); right1 = ImageIO.read(getClass().getResourceAsStream("..\ sc\\Player\ obotPlayerDWalk_1.png")); right2 = ImageIO.read(getClass().getResourceAsStream("..\ sc\\Player\ obotPlayerDWalk_2.png")); down1 = ImageIO.read(getClass().getResourceAsStream("..\ sc\\Player\ obotPlayerSWalk_1.png")); down2 = ImageIO.read(getClass().getResourceAsStream("..\ sc\\Player\ obotPlayerSWalk_2.png")); up1 = ImageIO.read(getClass().getResourceAsStream("..\ sc\\Player\ obotPlayerWWalk_1.png")); up2 = ImageIO.read(getClass().getResourceAsStream("..\ sc\\Player\ obotPlayerWWalk_2.png")); }catch(IOException e){ e.printStackTrace(); } } public void update(){ if(keyH.upPressed == true){ direction = "up"; } else if(keyH.downPressed == true){ direction = "down"; } else if(keyH.leftPressed == true){ direction = "left"; } else if(keyH.rightPressed == true){ direction = "right"; } // CHECK TILE COLLISION collisionOn = false; gp.cChecker.checkTile(this); // IF COLLISION IS FALSE, PLAYER CAN MOVE if(collisionOn == false){ switch(direction){ case "up": worldY -= speed; break; case "down": worldY += speed; break; case "left": worldX -= speed; break; case "right": worldX += speed; break; } } spriteCounter++; if(spriteCounter > 15){ if(spriteNum == 1){ spriteNum = 2; } else if(spriteNum == 2){ spriteNum = 1; } spriteCounter = 0; } } public void draw(Graphics2D g2){ //g2.setColor(Color.white); // colors g2 - ismael notes //g2.fillRect(x, y, gp.tileSize, gp.tileSize); // placeholder for player character thus making it the size of tileSize - ismael notes BufferedImage image = null; switch(direction){ case "up": if(spriteNum == 1){ image = up1; } if(spriteNum == 2){ image = up2; } break; case "down": if(spriteNum == 1){ image = down1; } if(spriteNum == 2){ image = down2; } break; case "left": if(spriteNum == 1){ image = left1; } if(spriteNum == 2){ image = left2; } break; case "right": if(spriteNum == 1){ image = right1; } if(spriteNum == 2){ image = right2; } break; } g2.drawImage(image, screenX, screenY, gp.tileSize, gp.tileSize, null); } }
@RyiSnow
@RyiSnow 3 жыл бұрын
@@_chaozgt_ Just like ciTatic says, you haven't added the if statement to check key input. So the program keeps adding the speed value to player's worldX/Y. I guess you skipped a part in Part 3 video.
@_chaozgt_
@_chaozgt_ 3 жыл бұрын
@@RyiSnow Oh, i see, thank you!
@yuvtajvirsingh3513
@yuvtajvirsingh3513 3 жыл бұрын
Thanks for the video, I was confused on how to implement a Collision Detection
@TNHoWalfas
@TNHoWalfas 2 жыл бұрын
It seems like I have some kind of issue with the collision. My CollisionChecker class is identical to yours but when I run the game, the character is stuck, frozen in place and no animation being played. When I comment out the code to check tile collision in my player class, my character moves again, but can't stop. Terminal output also mentions "java.lang.ArrayIndexOutOfBoundsException: Index 1056 out of bounds for length 50". I have no idea what causing these problems as I followed the collision detection tutorial exactly. EDIT: I figured out at the 20:17 mark I unknowingly put the parenthesis in the wrong place (place the ")" behind ".speed" instead of "gp.tileSize", how I did that I don't know), but the character is constantly moving in one direction unless it collides with a tile...
@happyburger23
@happyburger23 Жыл бұрын
@CrippinDoge it should be entity.speed) / gp.tileSize; rather than entity.speed / gp.tileSize);
@elguardallavesdejaal
@elguardallavesdejaal 10 ай бұрын
In this comment I've found salvation, thanks.
@matthewwheeler469
@matthewwheeler469 4 ай бұрын
Bro, thank you so much. I've been stuck on this problem for hours.
@user-js1vo6ht8e
@user-js1vo6ht8e 3 ай бұрын
Thank you😭
@monfil899
@monfil899 2 ай бұрын
you are the hero we didnt know we needed🙏🙏🙏
@alphabeta3528
@alphabeta3528 2 ай бұрын
PLEASE NOTE ON 25:44 IT IS VERY IMPORTANT GUYS (I missed it and wondering why the collision is weird for right only and it'll be annoying when an npc collide with you)
@Mohamed-lx3lp
@Mohamed-lx3lp Ай бұрын
My colision still weird helppp
@vasilisavasya4245
@vasilisavasya4245 3 жыл бұрын
your videos are indeed very clear and helpful
@vistasinconnection9678
@vistasinconnection9678 7 ай бұрын
Yay! got it working - this is really good. Teaching me so much. Thank you
@tranminhquang7368
@tranminhquang7368 3 жыл бұрын
I've been waiting for your new video for 1 week Hope you make more videos in the future
@RyiSnow
@RyiSnow 3 жыл бұрын
I have a full time job so can't promise but hopefully I can spend more time for making videos this month.
@wizco4443
@wizco4443 2 жыл бұрын
Soo I have a problem, whenever I colode with an tree up down or left I can use the opposite direction to move away. If I collide with something on my right side the only way I can get out of colliding is if I go up or down , when I press left the animation plays but I don't go anywhere. PLEASE HELP! all help/ advice is welcome.......
@sgsnake2x
@sgsnake2x 2 жыл бұрын
Could I make method such as this? public void createTile( int tileIndex, String filePath, boolean collision) throws IOException { tile [ tileIndex ] = new Tile(); tile[ tileIndex ] . image = ImageIO . read (getClass() . getResourceAsStream ( filePath ) ) ; tile [ tileIndex ] .collision = collision ; }
@Ty-vm5hv
@Ty-vm5hv 2 жыл бұрын
really nice tutorial, thanks a lot ! you deserve way more views !
@Neon-sise
@Neon-sise 3 ай бұрын
im in 23:17 and if i press the up button i'm not able to move.does someone know why
@DravenFNM
@DravenFNM 3 жыл бұрын
Was waiting eagerly for this video Thanks :)
@norhasanrajahmuda8509
@norhasanrajahmuda8509 8 ай бұрын
why my player continues moving down even if i did not control it and when i switch to right or left if also continue moving and when up is stop
@VasiliyNikitin
@VasiliyNikitin Жыл бұрын
Cool video tutorial! The result of this video makes me happy
@youreawesome718
@youreawesome718 Жыл бұрын
So I've been having an issue with the collision. The map is loaded fine, however I will run into seemingly random collision areas on the map, and sometimes where there should be collision there isn't. I have redone the collision from this video, I have looked at the line string input for the map, made sure the correct tiles have had collision on. I am unsure where to go from here.
@youreawesome718
@youreawesome718 Жыл бұрын
Also what is the counter2 initializing in the player subclass?
@youreawesome718
@youreawesome718 Жыл бұрын
Well, the issue(which I haven't resolved) is the world01.txt is only loading half the map but making that half take up the 50x50 allocated tile slots. The bottom half will load but not the top. if I change maxWorldX and maxWorldY to 100, the map does not load with any of the textured tiles other than the grass tiles.
@princea08999
@princea08999 Жыл бұрын
@@youreawesome718 seems like problem is caused by the use of incorrect variables.
@doxuantruong04
@doxuantruong04 7 ай бұрын
Hey, I cannot create the collisionChecker function and receive the GamePanel gp.
@MvpMartell
@MvpMartell 3 жыл бұрын
Hello Ryi this is Martell. How are you? Question: This question is a bit off topic so I apologize, but I want to know....how can I make JText field transparent? I tried to set the main text area to Opaque and that doesn't work. I even tried to set the background color to 0,0,0,0 and that didn't work either. I also tried to set the panel itself to OPAQUE and that didn't work. Can it be done?
@RyiSnow
@RyiSnow 3 жыл бұрын
JTextField tf = new JTextField(); tf.setOpaque(false); tf.setBorder(null); This makes it transparent.
@CharllothMoreira
@CharllothMoreira 28 күн бұрын
Without a doubt, the best content for learning Java for games. However, when it comes to collision, some things were a bit unclear-maybe because I'm a beginner. Could you explain clearly how the player doesn't get stuck on the collision tile? I've tried everything, but they still get stuck. Does it have something to do with the execution order?
@TerminallyUnique95
@TerminallyUnique95 3 жыл бұрын
How can I troubleshoot by drawing the collision rectangles on screen. My character is able to pass through top left and bottom left of solid objects.
@RyiSnow
@RyiSnow 3 жыл бұрын
You can display the rectangle by adding a line like this: g2.drawImage(image, screenX, screenY, gp.tileSize, gp.tileSize, null); // Draw player g2.fillRect(screenX + solidArea.x, screenY + solidArea.y, solidArea.width, solidArea.height); // Draw collision rectangle
@TerminallyUnique95
@TerminallyUnique95 3 жыл бұрын
@@RyiSnow Thanks. I finally figured out.
@mw6092
@mw6092 3 жыл бұрын
Thanks, this was very helpful. I had a similar problem and apparently the solidArea causes trouble if x,y, width and height are not set to numbers but to variables/calculations. I wanted to make it dependent on the tileSize and if, for example, I write solidArea.width = (int) (Math.round(32/48*gp.tileSize)); the collission does not work properly and the collision rectangle is not painted at all. However, with solidArea.width = 42; (for tilesize 64) all works fine.. A bit strange but at least now I know what's the problem :)
@geoartop3363
@geoartop3363 3 жыл бұрын
@@TerminallyUnique95 hey i am having a similar problem can you tell me how you solved it ?
@TerminallyUnique95
@TerminallyUnique95 3 жыл бұрын
@@geoartop3363 I had to recheck the code in checkTile until I found what I typed wrong.
@rexroyulada6267
@rexroyulada6267 9 ай бұрын
Also, make sure that you let the entity check the direction it is colliding, example: I don't know what this: entityRightCol = (entityRightWorldX + entity.spd) / (gp.tileSize); was used for and even accidentally did this instead: entityRightWorldX += entity.spd / gp.tileSize; This part of the code actually detects the collision before the entity gets too close to the tile and detect multiple collisions. I learned this the hard way.
@hijo-0191
@hijo-0191 Жыл бұрын
Hello, I have a problem where if I try to move, my character gets teleported to the left side of the map
@bluestreak711
@bluestreak711 2 жыл бұрын
7:22 @RyiSnow, Why didn't you use a calculation of gp.tileSize here? It may seem like unnecessary math, but if somebody decides to change the scaling of the game, then this part of the game would not function correctly.
@chitra___
@chitra___ Ай бұрын
If you change the scale, don't all the sprites and tiles need to be redone?
@md.towhidulislamrahat2603
@md.towhidulislamrahat2603 2 жыл бұрын
Man Loved This tutorial. You're legend
@BrunoHenrique-oe5hb
@BrunoHenrique-oe5hb Жыл бұрын
When i change the player.update() to implement the collision system i had a problem: when the game starts my character starts move down automaticaly. This happened because of two things: 1 - we change the way we increment the player position in the world. Before was in the if statment: if (keyH.upPressed == true) { direction = "up"; worldY -= speed; } then it was in the switch case: switch (direction) { case "up": worldY -= speed; break; 2 - i did no include t[he if statment: if(keyH.upPressed || keyH.downPressed || keyH.leftPressed || keyH.rightPressed) { // code } because i liked the player moving still, its like a breathing animation. But i couldn figure it out how to fix this without adding the conditional, sadly. But its ok, let's keep going. Great video, as always
@coldbacon冷気
@coldbacon冷気 11 ай бұрын
thank you very much, i had the same issue
@Jeganis.
@Jeganis. 4 ай бұрын
So, I was actually having the same issue, and if you want to keep the breathing animation, I found that adding the conditional around just the switch statement fixes the issue, after the check for 'collisionOn == false'
@drec_is_here5229
@drec_is_here5229 Жыл бұрын
Update: in checkTile function I’ve found an issue regarding collision detection make sure to add spacing between the front slash “/“ or else its considered a division (example: entityTopRow - entity.speed / gp.tileSize) this one was very difficult to find since its the exact same thing I see on the video. Hope this helps
@Gm_hshahahs
@Gm_hshahahs Жыл бұрын
I think you forgot to put entity entityTopWorldY - entity.speed in bracket Like this (entityTopWorldY - entity.speed)/gp.tileSize;
@tybrassington1614
@tybrassington1614 Жыл бұрын
Hi Ryi, I got my collision working for the most part, but I've run into an issue when the bottom of the players hitbox is aligned with the bottom of the collision tiles, it will phase through it. I don't run into this issue when the left and the right of the hitbox are aligned with the left or right of the collision tile. Do you happen to know where I went wrong as yours doesn't seem to have that issue.
@tybrassington1614
@tybrassington1614 Жыл бұрын
I'd be happy to supply the code through Github if you need context
@alialden1258
@alialden1258 3 жыл бұрын
Thank you so much for these tutorials
@diegofiguesevi8355
@diegofiguesevi8355 Жыл бұрын
Hello! Thank you for all these videos, you helped me improve the foundations of my knowledge and in some cases (like in threads) you helped me understand much better everything that at the time I couldn’t understand. But I have a question, in some cases you divide some values by the tileSize and I don’t understand why, could you explain this better to me? The case where I have the most doubt is in the formulas you used at minute 17:00. Why did you do it like that and what does it return? THANK YOU!
@RyiSnow
@RyiSnow Жыл бұрын
We divide them by the tile size because we need to get col and row numbers instead of x and y!
@vyzymz
@vyzymz Жыл бұрын
@@RyiSnow for a moment, I also wondered why. but i think, to explain it better, it is because you are looking for a tile in that position, so you divided it by the tileSize. the four (4) numbers you made are like (4) pin points, or simply (4) corners of the collision hitbox, where these pins checks whatever tile they're standing on, whenever you told them to (every frame update, in this project), so that these (4) pins can tell you if it's a solid tile or not. if it's solid, we won't let the player move further to where they want to, otherwise, we will let it happen. to make it more clear, these (4) pins can actually detect 4 different tiles, with the help of tileSize variable, if we let it.
@pasinducharuka9846
@pasinducharuka9846 3 жыл бұрын
This is a great great great work... big salute to you bro...
@Petronz
@Petronz Жыл бұрын
I have a question. After i copied that, the collisions are not activied but in walls, water and trees, my character keeps walking but it goes slower and I don't understand why, since we didn't deal with speed in the methods, has anyone come across a similar stuff?
@silenceradieux3590
@silenceradieux3590 Жыл бұрын
Same here! Checked out comments section for a solution. I hope someone will help us. 🥶 Update : found this kinda working : case "up": entityTopRow = (entityTopWorldY - entity.speed)/ GamePanel.tileSize; tileNum1 = gp.tileM.mapTileNum[entityLeftCol][entityTopRow]; tileNum2 = gp.tileM.mapTileNum[entityRightCol][entityTopRow]; if (gp.tileM.tile[tileNum1].collision || gp.tileM.tile[tileNum2].collision) { entity.collisionOn = true; entity.speed = 0; } else { entity.collisionOn = false; entity.speed = 4; } break; Do this for each direction.
@davidmako2696
@davidmako2696 Жыл бұрын
@@silenceradieux3590 lifesaver.
@marvindietermann9597
@marvindietermann9597 3 жыл бұрын
Hey every1, I have a problem: I know someone actually had this problem in the comments before, but the answer didn't help me that much due to me being quite new to coding, but my character keeps getting stuck on collision boxes, with only up being the way out, which then completely stucks the pCharacter in the collision box up top of the pCharacter. Can someone help?
@adrianrazvan17
@adrianrazvan17 3 жыл бұрын
same problem, have you found the solution?
@marvindietermann9597
@marvindietermann9597 3 жыл бұрын
@@adrianrazvan17 actually yes, i've made seperate boolians for collisionOnUp/Down/Left/Right and coded them in the right places, switch(direction) { case "up": if(collisionOnUp == false) { worldY -= speed; } break; case "down": if(collisionOnDown == false) { worldY += speed; } break; case "left": if(collisionOnLeft == false) { worldX -= speed; } break; case "right": if(collisionOnRight == false) { worldX += speed; } break; } now it works like a charme
@antsvietnam9933
@antsvietnam9933 16 күн бұрын
well, ran into some problems, if i hold D key and press W key, the character travel right will go up, but if i hold W key and press D key, the character travel up and won't go to right side, it's all about the order of the code, i thinhk you will get it, can somebody give me solution
@deadsuch3930
@deadsuch3930 4 ай бұрын
what if i just stuck on invisible tile i just go up and hey invisible wall how can i fix this i dont get it or i just go through collision tile why the method doesnt work though that code is correct.
@markoantonijevic9683
@markoantonijevic9683 2 ай бұрын
Hi RyiSnow, I wanted to make the player be able to movie diagonally as well, so I made some adjustments to the code in that department. However, whenever the player reaches the bottom wall purely by pressing down, he can go purely left or right. Pressing down and right or down and left leads to the player(solid area) going a bit inside the bottom wall tile, so he gets stuck and can't get out of it without pressing only up. Do you know what might be the issue?
@kaitakahashi3167
@kaitakahashi3167 2 жыл бұрын
Great vid and it really is helping me, but i have found several places where due to collision n the rectangular size make the player stuck in the same position Its like u cant move away from that place. Forever Stuck, was wondering, is there any solution to that?
@FreeStyleCrhymeR
@FreeStyleCrhymeR 2 жыл бұрын
check your collisionChecker. if you put the "break;" statements of switch's in the if statements, it happens. put it outside of if statements.
@milkman6240
@milkman6240 8 ай бұрын
Around 11:41 I'm a little confused about what you're doing with those variables. I understand that the actual collision box is a smaller box inside the player box, but what are you trying to with these points? I've watched this video three times and I haven't managed to wrap my head around it.
@jimmyjakes1823
@jimmyjakes1823 8 ай бұрын
Let's start from 11:41. After you input direction, the player's position is updated. Suppose you are on 23, 22 in the world map and press the up direction which updates the player position to 23, 21. You multiply these tile coordinates for the new position by the tile size 48 to get the pixel coordinates (1104,1008). You then use the offsets in the hitbox to determine the 4 corners that make up the coordinates of the player's collision box on the map. I'm using the actual values here for this situation but don't get hung up on that because they change everytime the player moves: top of the box: 1008+16 bottom of the box: 1008 +48 left of box: 1104+8 right of box: 1104+40 The end result is a smaller box within the 48x48 tile that we use to draw the player sprite, which makes it easier not to bump into impassable tiles. Here's the part I think you're asking about: Since you're hypothetically trying to move up in this example, we check the tiles on the upper left and upper right corner of the player's hitbox which are (1112, 1024) and (1144, 1024). We then divide these coordinates by the tile size to find out what their position is on the 50 x 50 game map in the text file and what type of tiles they are. It's possible for the left upper hitbox to fall in one tile and the right upper side of the hitbox to fall in a different tile so we have to check what the tile is at both coordinates to see whether they're passable, but in this case the entire hitbox falls inside one tile which is 23, 21 and that's a sand tile. If the tiles are impassible the player isn't allowed to move there and you stay at position 23 ,22 or else it is passable and the player moves up to position 23, 21. Is that sort of what you were asking about?
@milkman6240
@milkman6240 8 ай бұрын
@@jimmyjakes1823 Thank you for the explanation! I think I'm really beginning to understand things now!
@tidstids
@tidstids 3 жыл бұрын
In checkTile is an error when I write Entity entity, so I need to write Player, it’s that normal?
@RyiSnow
@RyiSnow 3 жыл бұрын
If player is the only entity that you check collision, writing Player is fine but if you use the method to check other entities such as npcs or monsters (which we will do in later videos), it will cause problems.
@tidstids
@tidstids 3 жыл бұрын
@@RyiSnow I don’t know what I do, but now it’s working!
@tidstids
@tidstids 3 жыл бұрын
In tutorial 14 it start working
@zhichgaming1466
@zhichgaming1466 2 жыл бұрын
For some reason, when I go down, it goes halfway through the tree. I even drawed a black box on the screen as the collision zone and I see that it goes right through the box. This only happens when I go down, not with any other directions. Could you tell me any common mistakes people make so I can check if I made them?
@Nicolaspatti19
@Nicolaspatti19 2 жыл бұрын
i have the same problem, did you solve it ?
@Nicolaspatti19
@Nicolaspatti19 2 жыл бұрын
I find the solution : in the CollisionChecker for case : "down" he made the same mistake than for right so you have to replace the " - " by a " + " on the first line of case down.
@3bood_kr
@3bood_kr Жыл бұрын
I'm trying to make pacman game using your code but the collision detection isn't working. So when i loaded the map i created an arraylist of rectangils of all blocks on the map that holdes each block x and y. And inside CollisionChecker i have a method that takes an entity as a parameter and loops over all the rectangles arraylist and checks if the player intersects with any of them using intersects method. So, is what i'm doing right? Cause it's not working. It's not giving any errors but the player can still walkthrough blocks. Edit: I fixed it by updating player's solidArea's cordinates in player's update method and also figured that i didn"t calculate the speed inside collision checker and now it's working. Now i'm trying to implement pacman movement by storing the next direction in a string and checking if the next direction's casues collision or not to decide when to change the current direction. Edit1: Finally i've succeeded to implement pacman's movement. It took me hours because he was getting stuck when he tried to enter a tight turn but somehow it worked at the end. I also added the food and implemented food interaction and now he can eat food and store score hos score. The only thing left is adding ghosts and sounds.
@hakerananasek
@hakerananasek 2 жыл бұрын
Hi Ryi, not sure if you're still reading the comments on your old videos, but there's a problem with collision rectangle object size not being dependent on scale. If you change the scale variable, the collision rectangle will still have the x, y, width and height as if the tileSize was 48. It can be fixed with multiplying rectangle arguments by scale. However, when I tested that, I also noticed that player moves faster on lower resolutions, and slower at higher. Is it intended, or addressed in later videos?
@toasty6224
@toasty6224 Жыл бұрын
What do you mean by that? I’m currently building the game but I’m having issues with the collision. My characters width and height that I want for collision is 13 x 13. It is 9 pixels from the left border and 19 pixels away from the top border. However the issue is I’m still colliding with the actual tile size of 48 x 48 if I get to close to a tree for example.
@OneLatteComingRightUp
@OneLatteComingRightUp Жыл бұрын
Hi RyiSnow, I'm loving your content! I am currently up to episode 14 of the series, as I just found this amazing channel, except I have ignored a problem on how if I switch keys really fast next to an object, I can noclip through it, the NPC also knows how to do it! Can you please help me? Also, if I am not on the center of an object, but a bit lower, I can move through the object, as if the tree is a pixel small.
@saladstik3158
@saladstik3158 2 жыл бұрын
do you know if there would be a way to do this but keep the ability to move the player diagonal like you could before there was any actual collision detection
@IamDalwine
@IamDalwine 4 ай бұрын
hey just a question what pixel editing softwear do you use?
@mastergamer4335
@mastergamer4335 3 жыл бұрын
I tried to optimize it but it dont worked and now i dunno whats wrong but if i hit a wall or somthing solid i cant move at any direction at all.
@mastergamer4335
@mastergamer4335 2 жыл бұрын
Found why.
@wizco4443
@wizco4443 2 жыл бұрын
whatHapend(?) { iHaveTheSameProblem; }
@kuronomasaro3192
@kuronomasaro3192 Ай бұрын
Why is the collision box at the player's coordinates without seeing any part of the script doing this? Why in your logic does the player collide with a tile and not get stuck in it? I just want to understand the logic
@redwings1847
@redwings1847 4 ай бұрын
I'm always writing comments for what each block of code does so when running into some problem I can quickly review that specific block of code to fix any issues. For comments, explanations for what the code does explained in this video works good for comments. This series is pretty long and getting to the end without comments would make all the code really difficult to understand later I think.
@kevenketchum1525
@kevenketchum1525 3 жыл бұрын
I've been following along really well and everything was running the exact way until we ran the game again at 22:57. My characters position is stuck in the middle of the screen and when I move the 4 directions they are animated to move that way but doesn't actually move. Can anyone help?
@kevenketchum1525
@kevenketchum1525 3 жыл бұрын
even after watching the rest of the video and adding the collision detection for all directions my character is still stuck in the middle of the screen please help.
@RyiSnow
@RyiSnow 3 жыл бұрын
@@kevenketchum1525 If your character cannot move, that means collision is on for some reason. Track down how and when your program chenage the collisionOn status. You probably forgot to set it false or set it true at a wrong place.
@RyiSnow
@RyiSnow 3 жыл бұрын
Change: if(collisionOn = false) To: if(collisionOn == false)
@jarrydoneill7834
@jarrydoneill7834 2 жыл бұрын
@@RyiSnow mines set to false but still not working
@jay_codes
@jay_codes 3 жыл бұрын
Great Tutorial as always
@randominternet8553
@randominternet8553 2 жыл бұрын
what was that in the background? 1:13
@DieterKlingberg
@DieterKlingberg 11 ай бұрын
When I collide, my player can't get out of the collision, even though he's no longer colliding, his movements somehow remain on false. I understand how this works, but I can't find a solution
@beautifulmama7851
@beautifulmama7851 2 жыл бұрын
Do you know what would cause my player to continue moving in the direction with out me pressing any keys? the collision works and it stops but if i move another direction the player will just keep walking in the direction i push
@RyiSnow
@RyiSnow 2 жыл бұрын
The player keeps walking most likely means one of the keyPressed boolean stays in true. So check around the keyPressed conditions.
@beautifulmama7851
@beautifulmama7851 2 жыл бұрын
@@RyiSnow thanks for the reply
@beautifulmama7851
@beautifulmama7851 2 жыл бұрын
public void update() { if (keyH.upPressed == true|| keyH.downPressed == true || keyH.leftPressed == true || keyH.rightPressed == true) { } if (keyH.upPressed == true) { direction = "up"; } else if (keyH.downPressed == true) { direction = "down"; } else if (keyH.leftPressed == true) { direction = "left"; } else if (keyH.rightPressed == true) { direction = "right"; } collisionOn = false; gp.cChecker.checkTile(this); if(collisionOn == false){ switch(direction) { case "up": worldY -= speed; break; case "down" : worldY += speed; break; case "left" : worldX -= speed; break; case "right": worldX += speed; break; } } this is what i have current..
@beautifulmama7851
@beautifulmama7851 2 жыл бұрын
yeah still no luck =/ think i will have to watch a few times to find the fix. im able to control where it moves but i dont press anything its moving on its own
@beautifulmama7851
@beautifulmama7851 2 жыл бұрын
here is my key handler class as well package main; import java.awt.event.KeyEvent; import java.awt.event.KeyListener; public class KeyHandler implements KeyListener { public boolean upPressed, downPressed, leftPressed, rightPressed; p @Override public void keyTyped(KeyEvent e) { // TODO Auto-generated method stub } @Override public void keyPressed(KeyEvent e) { int code = e.getKeyCode(); if (code == KeyEvent.VK_W) { upPressed = true; } if (code == KeyEvent.VK_S) { downPressed = true; } if (code == KeyEvent.VK_A) { leftPressed = true; } if (code == KeyEvent.VK_D) { rightPressed = true; } } @Override public void keyReleased(KeyEvent e) { int code = e.getKeyCode(); if (code == KeyEvent.VK_W) { upPressed = false; } if (code == KeyEvent.VK_S) { downPressed = false; } if (code == KeyEvent.VK_A) { leftPressed = false; } if (code == KeyEvent.VK_D) { rightPressed = false; } } }
@hoancute
@hoancute 2 жыл бұрын
Please turn on subtitles for this video! Thank you so much!
@RyiSnow
@RyiSnow 2 жыл бұрын
Hmm those subtitles are auto-generated by KZbin so I'm not sure why this video doesn't have one. Maybe a glitch?
@Melvin-14
@Melvin-14 2 жыл бұрын
@@RyiSnow you have to enable it in your video settings
@RyiSnow
@RyiSnow 2 жыл бұрын
@@Melvin-14 As far as I know there's no option to enable auto-generated subtitles. You can only manually add subtitles. If you think I'm missing something, let me know how to do it then I'll check it again.
@Melvin-14
@Melvin-14 2 жыл бұрын
@@RyiSnow yes but can u check ur latest video to see my comment i need help
@hk-p1027
@hk-p1027 5 ай бұрын
what should i do if theres space between the character and the tile please? Im really stuck on that
@MrSlinky
@MrSlinky 3 жыл бұрын
Great videos, they're helping a ton. I do have a question though. If my character collides with an object going up or down, they suddenly can't move side to side. I can't really seem to figure out what the problem is.
@MrSlinky
@MrSlinky 3 жыл бұрын
Nevermind I fixed it. It was because I was trying to use a double speed and so had to cast speed as an int in the collision detection class, so it was rounding the speed slightly lower than it should have, there for triggering the side to side collisions.
@iplay3542
@iplay3542 8 ай бұрын
I really don’t know what’s wrong with my collision detection, my character is still going through blocks it’s not suppose to and stopping blocks it is suppose to, and my character is a 16x16
@omerfaruk4579
@omerfaruk4579 Жыл бұрын
Nice brA, you are deserved to be BIG THREE ecole
@jestjames88
@jestjames88 Ай бұрын
For anyone having trouble with this: If your PC spawns outside of your map, all you have to do is find the worldX and worldY values on your player class in your code. There, you can edit your Player Character's spawnpoint, letting you spawn inside the map and away from the void. Idk why but the void around the map (for those working with smaller maps) will crash the shit out of my collision atm so heads up!
@1345alen
@1345alen Жыл бұрын
Hi, can you explain the checkTile section with numbers instead with words, or both. Thank you.
@mikeabordo6655
@mikeabordo6655 2 жыл бұрын
I'm having a problem with the movement, when I pressed S and then W my character won't go up same with pressing D and A. I can't pressed 2 keys at the same time. but its working on pressing W first and then S and A first and then D
@uday_khare09
@uday_khare09 9 ай бұрын
My collision is working but the collision rectangle seems to be on the top and a little bit to left
@uday_khare09
@uday_khare09 9 ай бұрын
And I have the same code
@AlexSilva-zs2ul
@AlexSilva-zs2ul 8 ай бұрын
I'm wondering how I can draw the collision rectangle on the screen. Can someone help me?
@shichaoXu
@shichaoXu 11 ай бұрын
Hi, RyiSnow,I'm a Java programmer,I want to make some word notes depend on your this game video, and publish to blog, can I do this?
@eclectic9071
@eclectic9071 3 жыл бұрын
Im trying to make it so the main character and still move diagonally how do I do that?
@eclectic9071
@eclectic9071 3 жыл бұрын
I've figure out how to include collision detection for the positive diagonal movements but am stuck in trying to figure out the downward diagonal movements
@eclectic9071
@eclectic9071 3 жыл бұрын
nvm figured it out
@zacharyobrien9032
@zacharyobrien9032 2 жыл бұрын
@@eclectic9071 can you post how you did it? I wasn't able to figure it out
@gumbo5206
@gumbo5206 2 жыл бұрын
@@zacharyobrien9032 just change the "else if"'s in the update() method of your player class to "if"'s
@Gius_Valentine
@Gius_Valentine Жыл бұрын
taking the else's away doesn't work anymore
@micheledalmonte740
@micheledalmonte740 Жыл бұрын
how can i solve the problem of sticky wall?
@8NLD8
@8NLD8 2 жыл бұрын
is this method of detecting collision using the Separating Axis Theorem (SAT)? I've heard that it is used to detect collision in games but I don't know how to implement it and I've never seen it in plain code :S
@ucquangle7496
@ucquangle7496 Жыл бұрын
how can the player move diagonally, thanks so much!!
@contrasena5243
@contrasena5243 2 жыл бұрын
I've watched this video 3-4 times in the last week and I can't understand a single thing on your checkTile method hahaha... I've seen in google that the Rectangle class has a collision method, would it be possible to use that method to make it simpler? Also I read a comment of a guy asking for randon terrain generation and that would be awesome. Greetings from Spain and thank you so much for these videos Im learning a lot! :)
@laz3664
@laz3664 Жыл бұрын
I can't thank you enough for these!!
@mattb8480
@mattb8480 2 жыл бұрын
Thanks so much, very helpful. Any ideas on how I would change the player and collision checker class if I wanted to implement gravity? I used the same tile method for collision and my sprites are in 2D, however I’m not sure how to add gravity and jumping.
@itzvelos7507
@itzvelos7507 Жыл бұрын
My collisionOn in the update method of player will not allow me to write it without "boolean" in front, it also will not let me do entity.collisionOn in collision checker.
@princea08999
@princea08999 Жыл бұрын
is collisionOn in entity is set to public ? If yes then there must be spelling mistake perhaps.
@cassiew9389
@cassiew9389 3 жыл бұрын
I am further along in the series but when I try to run my program now I am getting this error : * Exception in thread "Thread-0" java.lang.ArrayIndexOutOfBoundsException: Index 1048 out of bounds for length 50 at Main.CollisionChecker.checkTile(CollisionChecker.java:38) at Entity.Player.update(Player.java:116) at Main.GamePanel.update(GamePanel.java:113) at Main.GamePanel.run(GamePanel.java:93) at java.base/java.lang.Thread.run(Thread.java:833) */ and my mapTileNum = new int[maxWorldCol][maxWorldRow] ----- any ideas what could be causing this?
@RyiSnow
@RyiSnow 3 жыл бұрын
The program is trying to read the array with the index number 1048 which is way over the array's length ([50]). So my guess is you forgot to convert worldX/Y to Col/Row.
@cassiew9389
@cassiew9389 3 жыл бұрын
@@RyiSnow sorry to keep bugging you, I just want it to work lol and I really, really appreciated your tutorial. It helped me so much ! this is my public Tile Manager -------
@Melvin-14
@Melvin-14 2 жыл бұрын
@@RyiSnow what?
@NikitaBomba112
@NikitaBomba112 2 жыл бұрын
Thanks you so much for this video!
@sonnystoyanov1884
@sonnystoyanov1884 Жыл бұрын
hey i have a problem with the collisions my code is exacly like yours and when i run the program my character moves only left and when he reaches a tree it dont stop how can i fix it ?
@sonnystoyanov1884
@sonnystoyanov1884 Жыл бұрын
or at least can somebody help me :/
@princea08999
@princea08999 Жыл бұрын
@@sonnystoyanov1884 check if you have used correct variables in the switch case statement in the checkTile method inside the CollisionChecker class. To be precise I also wrote entityTopRow after the case "right": instead of entityRightCol and had similar problem.
@apro8723
@apro8723 Жыл бұрын
WHATS THAT IN THE BACKGROUND???? 1:07
@RyiSnow
@RyiSnow Жыл бұрын
Maybe a fire truck?
@cristofferostberg1297
@cristofferostberg1297 Жыл бұрын
Hi @RyiSnow! I just wanted to ask: when I from video #2 removed the "else" parameters from update method in Player, I was able to make the player move diagonally. But after video #6 with this Collision-function implemented I wasn't able to anymore. Do you have any way/function for implementing diagonal movement for the player, or is it unable to in the particular way these classes and program is constructed?
@RyiSnow
@RyiSnow Жыл бұрын
If you want to keep diagonally movement, here's basically what you need to do: 1. In the update(), store the current worldX and worldY in temp variables before adding/subtracting any speed values. 2. Add or subtract the speed value in the if statements where you set direction (basically keep the former method) 3. Check collision 4. If a collision happens, reset the worldX and worldY by using the stored temp values.
@cristofferostberg1297
@cristofferostberg1297 Жыл бұрын
@@RyiSnow Hi! I must first say thank you again for your awesome videos and dedication of this. I´m learning so alot of this and its really helping me in my Java-school im going on right now, you are gold! Just so its notified and cleared :) Keep rocking on! I just tried your code implemementation, and ended up something like this: public void update() { int tempWorldX = worldX; int tempWorldY = worldY; if(keyH.upPressed || keyH.downPressed || keyH.leftPressed || keyH.rightPressed) { if(keyH.upPressed) {direction = "up"; worldY -= speed; } if(keyH.downPressed) {direction = "down"; worldY += speed; } if(keyH.leftPressed) {direction = "left"; worldX -= speed; } if(keyH.rightPressed) {direction = "right"; worldX += speed; } //CHECK TILE COLLISION collisionOn = false; gp.cChecker.checkTile(this); //IF COLLISION IS FALSE; PLAYER CAN MOVE if(!collisionOn) { worldX = tempWorldX; worldY = tempWorldY; } And it seems like the inputs are now working, but the player just stands on the starting tile = not moving (although animation+keystrokes work) any suggestions?
@RyiSnow
@RyiSnow Жыл бұрын
I think it's a simple mistake. Change: if(!collisionOn) To if(collisionOn) !collisionOn means it resets the character's worldX and worldY when collision is NOT happening so it's only natural that you cannot move
@cristofferostberg1297
@cristofferostberg1297 Жыл бұрын
@@RyiSnow Man, i cannot say thank you enough.. You == legend. And i can say me as a class representative many in my class are starting to take interest in your videos here in Sweden as well, even our teachers! So you are superb. Keep having a good life, and remember to take breaks from the computer as well and allow yourself some treats and something good for yourself. That was, however, the issue. Now that i see it, and you said it as well it became really well logical. Thank you for help!
@thamiryssmbraga3756
@thamiryssmbraga3756 2 жыл бұрын
Hi RyoSnow, I don't know what happened but when I remove the code "worldY -= speed" from update(); and I put it in if(collisionOn == false){ switch(direction){}}, my player is extremely slow, but when I return to the previous location it returns to normal.
@masterdob7600
@masterdob7600 2 жыл бұрын
how do i stop my sprite from moving with out me pressing a key.
@justinabraham5642
@justinabraham5642 2 жыл бұрын
I need help please. So no matter what key I am pressing my character jumps to the most left side of the map and gets stuck in the solid wall object.
@justinabraham5642
@justinabraham5642 2 жыл бұрын
I found the problem! I put an equals where there shouldv'e been a plus hahaha
@sipkan3044
@sipkan3044 8 ай бұрын
@@justinabraham5642 to where
@drstowaters
@drstowaters 2 жыл бұрын
Hi, I hope I could get help. When I start the game I can only move on W and when I press A, S or D the animation works but player wont move. But when I move around sometimes my player won't move and sort of get stuck because of som invicible collider. Btw you're touturials are amazing and I've learnt so much!
@cristiano1296
@cristiano1296 2 жыл бұрын
make sure you put double equal signs in the if statement: if(collisionOn == false)
@drstowaters
@drstowaters 2 жыл бұрын
I got it working, the collider was just offset by two tiles for some reason so I just expanded the game screen so it was 16 x 16 tiles instead of 16 x 12 but I can't find the problem why it was that
@LutherCraft222
@LutherCraft222 Жыл бұрын
@@cristiano1296 spent two days trouble shooting and this was my issue thank u so much
@zeishirou
@zeishirou Жыл бұрын
@@LutherCraft222 i still have the issue i dont know how to fix it please help
@spinberry
@spinberry Жыл бұрын
​@@zeishirou did you fix?
@Rastimil
@Rastimil 2 жыл бұрын
when i hold W i cannot change direction unless i release W,when i hold etc D or A i can change direction by pressing W or S and i dont need to relase any key to change it,what may be the issue?
@Rastimil
@Rastimil 2 жыл бұрын
it is same with S,i cant figure out why A and D works but W and S doesnt
Object Placement - How to Make a 2D Game in Java #7
22:09
RyiSnow
Рет қаралды 68 М.
Quilt Challenge, No Skills, Just Luck#Funnyfamily #Partygames #Funny
00:32
Family Games Media
Рет қаралды 55 МЛН
Tuna 🍣 ​⁠@patrickzeinali ​⁠@ChefRush
00:48
albert_cancook
Рет қаралды 148 МЛН
Collision Detection (An Overview) (UPDATED!)
7:27
MacroPixel
Рет қаралды 45 М.
How to Make a 2D Game in Java #1 - The Mechanism of 2D Games
18:11
I Beat Minecraft From One Grass Block
35:27
Beppo
Рет қаралды 7 МЛН
The Tragic Decline Of Firefox...What Happened?
12:34
Logically Answered
Рет қаралды 106 М.
Drawing Tiles - How to Make a 2D Game in Java #4
27:26
RyiSnow
Рет қаралды 164 М.
1 Year of Learning Game Development In 6 Minutes
6:01
Giedzilla
Рет қаралды 2,6 МЛН
Blazingly Fast Greedy Mesher - Voxel Engine Optimizations
23:35
The First Game Done (UI) - How to Make a 2D Game in Java #10
34:50