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

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

RyiSnow

2 года назад

88,368 Просмотров

Ссылки и html тэги не поддерживаются


Комментарии:

@sonnystoyanov1884
@sonnystoyanov1884 - 12.01.2024 14:17

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 ?

Ответить
@itzvelos7507
@itzvelos7507 - 31.12.2023 21:21

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.

Ответить
@drec_is_here5229
@drec_is_here5229 - 27.12.2023 17:12

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

Ответить
@youreawesome718
@youreawesome718 - 18.12.2023 20:21

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.

Ответить
@omerfaruk4579
@omerfaruk4579 - 15.12.2023 20:47

Nice brA, you are deserved to be BIG THREE ecole

Ответить
@rosecollins8850
@rosecollins8850 - 27.11.2023 10:36

I m in Tokyo, please let me code with you!!!!!!

Ответить
@BrunoHenrique-oe5hb
@BrunoHenrique-oe5hb - 20.11.2023 05:30

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

Ответить
@xxwraithxx8827
@xxwraithxx8827 - 25.09.2023 18:17

my Player gets stuck when it detects collision on top. can someone help?

Ответить
@nanoic2964
@nanoic2964 - 11.09.2023 20:31

Will you release the source code for this project? I'm having errors with the collision detection part of the tutorial and comparing the code would really help me.

Ответить
@ExceptionCat
@ExceptionCat - 04.09.2023 17:32

public void check() { // If you wish for an easy way

entity.solidArea = new Rectangle(entity.x+8,entity.y+16,32,32);

Rectangle solidArea = entity.solidArea;

Rectangle nextArea = solidArea;

switch(entity.direction) {
case "up": nextArea.y = solidArea.y-entity.speed;
nextArea.x = solidArea.x; break;
case "down": nextArea.y = solidArea.y+entity.speed;
nextArea.x = solidArea.x; break;
case "left": nextArea.x = solidArea.x-entity.speed;
nextArea.y = solidArea.y; break;
case "right": nextArea.x = solidArea.x+entity.speed;
nextArea.y = solidArea.y; break;
}
for(int row = 0; row<tileM.maxRow ; row++) {
for(int col = 0; col<tileM.maxCol; col++) {

if(tileM.tile[tileM.map[col][row]].collision) {

if(nextArea.intersects(new Rectangle(col*48,row*48,48,48))) {
entity.collisionON = true;
}
}
}
}
}

Ответить
@AnimalFacts444
@AnimalFacts444 - 04.09.2023 09:34

Code is harder and harder to read. Need to split it up a bit to a different methods

Ответить
@Petronz
@Petronz - 29.08.2023 11:51

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?

Ответить
@laz3664
@laz3664 - 28.08.2023 20:41

I can't thank you enough for these!!

Ответить
@OneLatteComingRightUp
@OneLatteComingRightUp - 13.08.2023 10:48

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.

Ответить
@Nex701
@Nex701 - 31.07.2023 05:18

I found a nice way to to get the image and set collision if you use a numbering format for your tile png files, for example I named my tiles Tile_000, Tile_001 etc.

it avoids having to to write multiple lines of code to call each image and set its collision

I set an int array for my collisionTiles in my TileManager class like this: private int[ ] collisionTiles = {1, 5, 7, etc};

then changed my getTileImage to look like this

public void getTileImage() {

try {
for (int i = 0; i < 650; i++) {
tile[i] = new Tile();
String imagePath = String.format("/genesis_tiles/Tile_ID_%03d.png", i);
tile[i].image = ImageIO.read(getClass().getResourceAsStream(imagePath));

for (int collisionTile : collisionTiles) {
if (i == collisionTile) {
tile[i].collision = true;
break; // No need to continue the loop if the collision is set
}
}
}
}
catch(IOException e) {
e.printStackTrace();
}



There is no need to change the CollisionChecker :)

Ответить
@micheledalmonte740
@micheledalmonte740 - 24.07.2023 16:29

how can i solve the problem of sticky wall?

Ответить
@jwfinn100
@jwfinn100 - 18.06.2023 16:10

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.

Ответить
@benp9452
@benp9452 - 29.05.2023 15:12

i skipped the whole last part5 about worldmap and camera because i dont need whole world. is ist still possible to continue this series without everything you made in that part5

Ответить
@trickbuster8260
@trickbuster8260 - 17.05.2023 19:15

Ryisnow my player is stuck at one place please help

Ответить
@cristofferostberg1297
@cristofferostberg1297 - 15.05.2023 13:21

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?

Ответить
@katy8823
@katy8823 - 14.05.2023 20:19

If you wrote the code correctly and it won’t work make sure u didn’t accidentally added collision boolean in the wrong place 😅!

Ответить
@XerifeCaitlyn
@XerifeCaitlyn - 30.04.2023 16:28

I thought i could understand it well even if i got dificulties in the worldXY screenXY thing. but i think ill have to go back a little

Ответить
@3bood_kr
@3bood_kr - 26.04.2023 02:32

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.

Ответить
@hijo-0191
@hijo-0191 - 21.04.2023 23:45

Hello, I have a problem where if I try to move, my character gets teleported to the left side of the map

Ответить
@cedricsahaghian1605
@cedricsahaghian1605 - 12.04.2023 07:26

Thanks a lot for these videos!

Ответить
@VasiliyNikitin
@VasiliyNikitin - 04.04.2023 03:04

Cool video tutorial! The result of this video makes me happy

Ответить
@user-rd7bq4hv5g
@user-rd7bq4hv5g - 30.03.2023 14:20

God bless you your videos helped me so much thank you thank you

Ответить
@user-bm9hr1gy1i
@user-bm9hr1gy1i - 23.03.2023 22:17

can anyone tell what is tileM here?

Ответить
@dcwavie
@dcwavie - 10.03.2023 03:07

wo else legit did the rest on their own after making the if and switch statement for letting the player move

Ответить
@ucquangle7496
@ucquangle7496 - 06.03.2023 15:30

how can the player move diagonally, thanks so much!!

Ответить
@ritankarbose9782
@ritankarbose9782 - 03.03.2023 13:36

Ok so for some reason my player starts automatically moving in the direction it is facing.

Ответить
@Spluli
@Spluli - 25.02.2023 16:32

CODE in this answers.

Ответить
@tybrassington1614
@tybrassington1614 - 16.02.2023 11:34

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.

Ответить
@samjesf
@samjesf - 08.02.2023 08:11

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.

Ответить
@lvdv4j
@lvdv4j - 08.02.2023 01:28

Hey Ryi, as always your videos are awesome! Because of school I had to set this down for a while but I'm back at it again! This time I want to finish it 😄

I have a question while I'm here on this video. I'm not sure if you're still taking questions, but let's say I have a 32x32 tile, and on this tile I have drawn a treasure chest that only takes up 16x32 of the tile. Is there a way that instead of using the entire TileSize as the collision I could set it in a way that each tile has it's own "temporary collision size" so to speak? I've tried implementing this myself but I can't seem to get it right.

Ответить
@natzos6372
@natzos6372 - 06.02.2023 15:39

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

Ответить
@8NLD8
@8NLD8 - 17.01.2023 10:57

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

Ответить
@marcell114
@marcell114 - 13.01.2023 15:48

first at all thanks for this video.
i got a problem somewhere. collision works great but not the character moving non stop, only changing direction when pressing bottoms but keep moving. cant find the problem.

Ответить
@hakerananasek
@hakerananasek - 06.01.2023 16:44

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?

Ответить
@liergi6483
@liergi6483 - 19.12.2022 18:44

Thank you very much for the RyiSnow videos. I'm learning a lot!
I have a problem with my code. My character goes through the wall instead of standing. Also when going through the wall his speed is reduced.If you could help me I would be very grateful.

edit: I found the mistake! What a relieve

Ответить
@saladstik3158
@saladstik3158 - 16.12.2022 14:53

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

Ответить
@noell.855
@noell.855 - 02.12.2022 17:55

My character wont move when I walk to a wall and then try to go down

Ответить
@Tefrax
@Tefrax - 28.11.2022 15:21

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.

Ответить
@beautifulmama7851
@beautifulmama7851 - 02.11.2022 11:28

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

Ответить