

Welcome to episode 12 of our mario game in java tutorial. This episode, we create collision boxes, and implement collision detection into our game.
If you enjoyed the video, feel free to leave a like, a comment, and subscribe!
Intro Music: Knife Party – Centipede
Outro Music: K-391 – Hang Loose
None of these are owned by me. All rights go to their respective owners.
Stay tuned for more videos! 🙂
source
Hey! Great videos! Learned a lot just from a couple of videos. I know that you are not answering any questions anymore but if anyone at all knows how to solve this problem. I'll be very thankful! I've done the code right and even re-watched the 12 videos twice. My problem is when I press the A and D buttons, the blue square start moving by itself without stopping until it reaches the end of the application box. On the other hand when I press W and S buttons the blue box moves one step at a time (I press the W button once, the square moves one step forward and vice versa). Can someone please help me fix this problem? It's really annoying me and I can't seem to find the bug anywhere in the code.
my right collision is not working. the rest is fine.
I know this tutorial is pretty old and you might not see this, but the collision is not working right for me. I've written the code exactly like yours, but when I collide with the wall from the top or bottom, it teleports me up and if I collide with if from the sides, it teleports me to the right. I don't know why. I managed to fix it by doing this:
if(t.getId() == Id.wall){
if(getBoundsTop().intersects(t.getBounds())){
setVelY(0);
y=t.getVelY()+t.height*4+8;
}
if(getBoundsBottom().intersects(t.getBounds())){
setVelY(0);
y=t.getVelY()+t.height+height+8;
}
if(getBoundsLeft().intersects(t.getBounds())){
setVelX(0);
x=t.getVelX()+t.width*4+8;
}
if(getBoundsRight().intersects(t.getBounds())){
setVelX(0);
x=t.getVelX()+t.height*2+12;
}
}
It's working right, but I don't think it's too efficient. I just want to know why it's not working like in the video if I leave it as you said :/
You know that while loops are less buggy for collision, right?
Hi @OffıcialGamingNetwork
I found a bug at 11:22 while I was watching episode #12 ( I didn't watch episode #13 yet ,so , if you fixed it , ignore me 😀 )
Because of that your player and wall are same width -height(64-64) values, you probably couldn't see that.
if player's and wall's width-height values are not same, the problem is occured.
As a result, right and bottom conditions should be like these:
if(getBoundsBottom().intersects(t.getBounds())){
setVelY(0);
y = t.getY()-height;
}
if(getBoundsRight().intersects(t.getBounds())){
setVelX(0);
x = t.getX()-width;
}
I did everything exactly as you did from the first video so basically my whole code is an exact copy paste as yours but !!!!!! the part where you check if the player collides with the wall in if(getBoundstop.intersect………blahh())) iwasnt getting the same output as yours so i tried nested if and i got it ! :0 so m wondring how the hell did i get it ? cause you only used multiple if statment and for some reason my code was executing all the if's….. so the player wasnt moving… thats y i used else if in place of multiple if's and it worked then !
I can't use "intersects", just intersection, please show your code
If you want to see your collosion borders or bounds use this render method in your Player class instead:
@Override
public void render(Graphics graphics) {
graphics.setColor(new Color(255, 255, 255));
graphics.fillRect(x, y, width, height);
graphics.setColor(Color.BLUE);
graphics.fillRect(getBoundsRight().x, getBoundsRight().y, getBoundsRight().width, getBoundsRight().height);
graphics.fillRect(getBoundsLeft().x, getBoundsLeft().y, getBoundsLeft().width, getBoundsLeft().height);
graphics.fillRect(getBoundsTop().x, getBoundsTop().y, getBoundsTop().width, getBoundsTop().height);
graphics.fillRect(getBoundsBottom().x, getBoundsBottom().y, getBoundsBottom().width, getBoundsBottom().height);
}
😀 I LEArNED BASIC COLLISION WOOOT
Mooore gaming videos Please~~
Wow man, you don't even monetize your videos
And why getX() + 10 and width – 20??
Why don't you use intersects ?
It's like
if(rectangle a(intersects (rectangle b))
// code for not changing x and y coordinates of the player
Mine works fine, however if I change the length of the wall so that it is a long rectangle my square bounces off the left side of the wall when it makes contact, why is this? All other sides work fine
why did you change from regular "x" to "getX()"
plz help getting errors and cant even move
Hi, good job on the tutorials. I notice that your frames are a lot less than mine what did you do to decrease them??
Looks like a good tutorial. I was trying to find something for collision and I think this one is one of the easiest so far. I am going to have to start at the beginning to see how you are coding. I am just learning and have seen 100's of videos so far and still have a bunch of questions. I have come across some good and some not so good. I do appreciate you taking the time to share your knowledge with us. Keep up the good work and thanks again!
I want to see how you create a gravity, with your latest video example code
Nice and good to understand. I am waiting for next version. +1