Java Game Development #16 – Collision Detection

Complete Java Game Development Course now available at my website

In this video we learn how to add collision detection into our game. If you have any questions then be sure to leave a comment or contact me on twitter; realtutsgml. If you learned something then be sure to leave a like, comment, and favorite.

Outro Song:
Name: Spitfire (Kill the Noise Remix)
Artist: Porter Robinson
Youtube channel: /user/PorterRobinsonMusic

Have you ever wanted to create games? Have you ever gotten fed up with it being to difficult? Well now is the time to thank me and possibly subscribe because you have just found the channel for you! Game Maker Tutorials, Java Game Programming, Unity3D Tutorials, Batch, C++ and much much more! This is the channel for you, the one stop shop for an exploration of your hidden talent as a game developer. Unleash your potential and go wild with imagination when you finally figure out how to make any game you want!

Visit CodingMadeSimple for more exclusive tutorials and get the help you need to succeed as your very own indie game developer!

Follow me on twitter for exclusive content and interaction with me!

Game Maker Studio: Programming
Game Maker Studio: Tutorial
Java Programming
Game Programming
Game Tutorial
Programming Tutorial






30 responses to “Java Game Development #16 – Collision Detection”

  1. M.Husain Mns Avatar

    a follow up video plz and thank's alot we're learning so much

  2. Kosac Avatar

    did he make a follow up video explaining about collision cause it was difficult to understand it?

  3. SpeedKunG Avatar

    AWWWW GameObject is weirdddd!!!!
    It not render in right place ._.

  4. Kamal Ahmad Avatar

    hi Zack I knew this old video
    and you star other series but I need your help in collision So i add die() method which remove enemy at first it works but when I add more enemies it don't remove enemy that get shot but remove other enemy I know why this happens because we used Linked list when I use remove () it remove frist enemy that added in linked list So how l can remove spceific enemy that get shot So plz I need your help to Finnish my game

  5. hXcFreethinker Avatar

    Just posting this again out side of the replies so others can see.
    I would recommend not passing the entire game class to your entities. This does not make any sense. You should create a world class and have your entity lists in there. Create a world object in your game class and add the entities to it via setter methods. You can pass the world object to your entities and they will still have access to all the required data but they won't have access to the data that they don't need.

    Also method names like "Collision" should start with a lower case and follow camel casing format for censecutive words. for example "collision detection" would become "collisionDetection". That's just proper java format.

    Lastly as many have mentioned, use some polymorphism! Have entityA and entityB extend entity since they are both entities. Generally speaking when you find yourself copy and pasting whole methods between classes you are probably not using the oop functionality of java correctly.

  6. Gameriffy Avatar

    when i'm shooting, the console said COLLISION DETECTED a LOT of time, and stop when the bullet get out of the screen, help me

  7. Patrick Grady Avatar

    A follow up video explaining this a bit more would definitely be nice, especially explain the for loop. 🙂

  8. ejbytes Avatar

    Reading comments… I don't think this is a Q&A. so maybe my question below would be asked to an onlooker instead?

  9. ejbytes Avatar

    Very cool. I've never used collision detection in Java. I was going to create another game in Flash, but saw this. I haven't been in programming mode in a while… I have a question.

    Thinking aloud, so bare with me. At 16min, I was thinking about your method of Collision(A, LinkedList B) using a for loop on B (enemies). I wonder if you could instantiate Collision detection in each Entity B as a trait/property of B? What I'm getting at is this. You probably have to put Physics in a ticker yeah? So each ms you have to ask, "is A colliding into loop(all B)". Am I okay still? But if you added a Collition trait and ticker to each object as it's instantiated you wouldn't have to tick a loop of objects, but semi-simultaneously tick each object dynamically as they are created because you'd have added Collision to each object upon instantiation. Would that work? Thinking aloud…

  10. Saul Ivan Flores Torres Coto Avatar

    You make the best tutorial bud, keep it up, and thank you. You really made me understand the concepts!

  11. Sethu Kumar Avatar

    Or could we create EntityA and EntityB , both extending Entity ( both ARE entities, right ? ).
    That might make the code any simpler ?

  12. Altamash Khan Avatar

    Also you have no method to remove the bullets once they get off screen…. To do that.. we can go to the tick method in the controller class… and add if(entityA.getY() < 0){ removeEntityA(entityA); }

  13. Altamash Khan Avatar

    You are passing the game object as the parameter into the bullet class by ( public Bullet( double x, double y, Textures tex, Game game )) . This basically allows the bullet class to have access to the variables and methods of the game class.. Isn't this a bad practice? help needed. I thought the whole point of creating the textures class was so that the bullet, enemy, player would interact with the Textures class for getting the image sprites, and hence the game class would not be accessible to the bullet/enemy/player etc… please clarify. Can't we have another class to do the collision detection thing. or couldn't this be done in a different way?

  14. DeepVenganceTV Avatar

    I went through the whole video and every code is written exactly the same, but whenever I ran the game, nothing will load up, bullets, enemies won't show up but the player works. Why is that? Someone please help. Thank you.

  15. space1188 Avatar

    in the collision class the parameters take entityA and a linkedList<entityB> why is one a linked list and one isn't

  16. Nicholas Gorman Avatar

    god i spent the last 30 minutes trying to find why boolean for in physics class could not be set to an integer… ohh its because i forgot a tiny very significant i for my entb…
    anyways keep up the work bud, your tutorials will shape my future programming.

  17. Steamy Steamwork Avatar

    How would I detect collision between the Bullet and an enemy ship which results in a: c.removeEntity() of the bullet that collides and the ship that is hit? Moreover, how do I correctly utilise the c.removeEntity() method to remove specific objects such as the bullet and the enemy ship that collide?

  18. Commodore 64 Brain Avatar

    Nice stuff. Do you still do game development anymore? I've been following a guy called Indie Developer on another channel. This has led to a reenacted game I created in the 90s. It's a platformer, modeled after the original Pitfall 2 Atari series. Do you know anything about Java sound by the way? Seems to be a complex topic. Thanks.

  19. Bartol J. Avatar

    when i run the game at 16:44 my enemys dont move, but i can shoot bullets and move player, and few seconds later my enemys start to move and then error occures 🙁 pls somepne help

  20. Brax1982 Avatar

    Noooo, the damn Game parameter is back! Well, off the top of my head I have no idea how to avoid it.

    And why do you write "Collision" instead of "collision"? Capitalized method names…gah. It's probably like that "block" parameter. Something you got used to doing for some reason. If you still remember why, I would love to know where that habit is from.

  21. Taylor Kohls Avatar

    So now the game freezes whenever I shoot

  22. xCoduster Avatar

    My player starts at x = 0 and y = 0 and the bullet doesn't come out from the player. I have removed all the x, y in player and bullet class. Please somebody help!

  23. Peter Cole Avatar

    I was wondering if you could use the Cross Product as collision detection?  it will check if it intersects by returning 0

  24. digger plus Avatar

    so… my ship starts in the top left corner now and the bullets, when fired, stay up there even if the ship is moving around… did i miss something? watched the video 3 times and my code looks the same as far as i can tell.

  25. Matthew NotReal Avatar

    Hmm. This doesnt work. It always returns true.

  26. Epicnerd73 Avatar

    What about if you had Entity as the superclass and the Entity A and Entity B extend that and then all the objects extend from either Entity A and Entity B? Would that be OK? I haven't done it, just wondering..

  27. Yassin Philip Avatar

    How can I do this integration / load / create / completion / analysis magic in Emacs? (I just have completion and class links)

  28. Yassin Philip Avatar

    Better and better… Your mind is a very nice place 🙂

  29. Java Coding Avatar

    Why am I getting a null pointer exception and my game is freeing when I shoot a bullet?
    I checked my if(Physics.Collision(this, game.eb)) and it is right?
    I checked the Collision variable, and it is returning false when I don't shoot, but when I do shoot, I think it is returning null.
    How do I fix this?

  30. Botato Avatar

    Well I just wrote a big comment but hit my back button on my mouse 🙁 Basically I just said you could change: for(int i = 0; i < entb.size(); i++) into
    for(EntityB variableName : entityBArrayList) to further simplify things. I don't know, it's build into Java so just incase you haven't seen it.

Leave a Reply

Your email address will not be published. Required fields are marked *