"Java Programming Tutorial" | "Super Mario Game" #12 | Collision Detection

///"Java Programming Tutorial" | "Super Mario Game" #12 | Collision Detection

"Java Programming Tutorial" | "Super Mario Game" #12 | Collision Detection

FavoriteLoadingAdd to favorites

Welcome to episode 12 of our mario game in java tutorial. This episode, we create collision boxes, and implement collision detection into our game.

Java468x60

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

By |2020-05-24T03:38:19+00:00May 24th, 2020|Java Video Tutorials|20 Comments

20 Comments

  1. Eva B. May 24, 2020 at 3:38 am - Reply

    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.

  2. aniruddho ray May 24, 2020 at 3:38 am - Reply

    my right collision is not working. the rest is fine.

  3. Joseph Joestar May 24, 2020 at 3:38 am - Reply

    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 :/

  4. Ryan Walsh May 24, 2020 at 3:38 am - Reply

    You know that while loops are less buggy for collision, right?

  5. Yekta Anıl Aksoy May 24, 2020 at 3:38 am - Reply

    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;
    }

  6. Shirin Siddiqui May 24, 2020 at 3:38 am - Reply

    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 !

  7. Roman Lebedev May 24, 2020 at 3:38 am - Reply

    I can't use "intersects", just intersection, please show your code

  8. Johannes Elseberg May 24, 2020 at 3:38 am - Reply

    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);
    }

  9. John Doe May 24, 2020 at 3:38 am - Reply

    😀 I LEArNED BASIC COLLISION WOOOT

  10. John Doe May 24, 2020 at 3:38 am - Reply

    Mooore gaming videos Please~~

  11. ARJAV GARG May 24, 2020 at 3:38 am - Reply

    Wow man, you don't even monetize your videos

  12. ARJAV GARG May 24, 2020 at 3:38 am - Reply

    And why getX() + 10 and width – 20??

  13. ARJAV GARG May 24, 2020 at 3:38 am - Reply

    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

  14. Daniel Draper May 24, 2020 at 3:38 am - Reply

    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

  15. brandon nguyen May 24, 2020 at 3:38 am - Reply

    why did you change from regular "x" to "getX()"

  16. IraqiHerobrine103 May 24, 2020 at 3:38 am - Reply

    plz help getting errors and cant even move

  17. Emily Ling May 24, 2020 at 3:38 am - Reply

    Hi, good job on the tutorials. I notice that your frames are a lot less than mine what did you do to decrease them??

  18. Jamie Marcum May 24, 2020 at 3:38 am - Reply

    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!

  19. mrstanlez May 24, 2020 at 3:38 am - Reply

    I want to see how you create a gravity, with your latest video example code

  20. mrstanlez May 24, 2020 at 3:38 am - Reply

    Nice and good to understand. I am waiting for next version. +1

Leave A Comment

*