Java Game Programming #9 – Finishing Collision Bounds

Java

Video is ready, Click Here to View ×


Make Video Games 2018 ► https://www.codingmadesimple.com/courses/

In this video we finish our collision bounds for 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.

I’m the founder of codingmadesimple.com that hosts high quality, fun to learn and straight to the point course material for game developers. I’d really appreciate it if you’d check out my…

32 thoughts on “Java Game Programming #9 – Finishing Collision Bounds

  1. Im disappearing when im fall down

    package com.fl.objects;

    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.Rectangle;
    import java.util.LinkedList;

    import com.fl.framework.GameObject;
    import com.fl.framework.ObjectId;
    import com.fl.window.Handler;

    public class Player extends GameObject
    {

    private float width = 64, height = 128;
    private float gravity = 0.5f;
    private final float MAX_SPEED = 10;

    private Handler handler;

    public Player(float x, float y,Handler handler, ObjectId id)
    {
    super(x, y, id);
    this.handler = handler;
    }

    public void tick(LinkedList<GameObject> object)
    {

    x += velX;
    y += velY;

    if(falling || jumping)
    {
    velY += gravity;

    if(velY > MAX_SPEED)
    velY = MAX_SPEED;

    //gravity = gravity + 0.005f;
    }

    Collision(object);
    }

    private void Collision(LinkedList<GameObject> object)
    {
    for(int i = 0; i < handler.object.size(); i++)
    {
    GameObject tempObject = handler.object.get(i);

    if(tempObject.getId() == ObjectId.Block)
    {
    if(getBoundsTop().intersects(tempObject.getBounds())){
    y = tempObject.getY() + 40;
    }
    if(getBounds().intersects(tempObject.getBounds()))
    {
    y = tempObject.getY() – height;
    velY = 0;
    falling = false;
    jumping = false;
    }else
    falling = true;

    }
    }
    }

    public void render(Graphics g)
    {
    g.setColor(Color.yellow);
    g.fillRect((int)x, (int)y, (int)width, (int)height);

    Graphics2D g2d = (Graphics2D) g;
    g2d.setColor(Color.RED);
    g2d.draw(getBounds());
    g2d.draw(getBoundsRight());
    g2d.draw(getBoundsLeft());
    g2d.draw(getBoundsTop());
    }

    public Rectangle getBounds()
    {
    return new Rectangle((int) ((int)x+(width/2)-((width/2)/2)), (int) ((int)y+(height/2)), (int)width/2, (int)height/2);
    }

    public Rectangle getBoundsRight()
    {
    return new Rectangle((int) ((int)x+(width/2)-((width/2)/2)), (int)y, (int)width/2, (int)height/2);
    }

    public Rectangle getBoundsLeft()
    {
    return new Rectangle((int) ((int)x+width-5), (int)y+5, (int)5, (int)height-10);
    }

    public Rectangle getBoundsTop()
    {
    return new Rectangle((int)x, (int)y+5, (int)5, (int)height-10);
    }

    }

  2. Hello! I know it's way after this video uploaded, but I have a huge problem. For some reason, after using the keys for a little bit, it just stops. I put a debug message in the key event and it gets sent for the first few times, but after a few times, it randomly stops. The player won't move and the message won't get sent.

  3. Hi Just watching your video, have been looking at this for a few days and am getting these errors

    Exception in thread "Thread-2" java.lang.NullPointerException
    at develop.Player.Collision(Player.java:39)
    at develop.Player.tick(Player.java:34)
    at develop.Handler.tick(Handler.java:15)
    at develop.Game.tick(Game.java:70)
    at develop.Game.run(Game.java:53)
    at java.lang.Thread.run(Unknown Source)

    Any ideas, I have been copying the code as you have written but it seems to not like the Collision method I have made?

  4. If any one is having problems with "teleportation" and can't get the bounds to work right make sure your getBoundsLeft() method isn't swapped with your getBoundsTop() method, or something similar to that. Just comment out the code for one bound method and replace it with the code for another and see which rectangles disappear and which don't.

  5. Don't get me wrong. I like these videos. But the way this series is going, you have to know how to do exactly what your trying to learn if that makes any sense. It's almost like a tutorial to pick up a few things on how you would program this game and we want to make any changes. well then you shit out of luck. The people watching this are trying to learn it. This style of programming is not very add on friendly. wysiwyg. school flashback.

  6. I think a lot of people might leave the tutorial at this point. because of the code added off cam. and the hard coded collision. I doubt if people are here to learn to program a game even with basic java experience that they want to do something totally different than what the tutorial is showing. also in my opinion it's better to use doubles instead of floats. floats suck. I get more control with doubles. Looks like within these last two videos most of the people that came here to learn are just stuck with problems everywhere.

  7. I know this video is incredibly old, but I'm having an issue where the Player teleports randomly. In other words, all the collision boxes are buggy except for the bottom one. For example, when I hit the underside of the platform, the player moves several pixels to the left. Here's my code for the collisions:

    for (int i = 0; i < handler.object.size(); i++){
    GameObject tempObject = handler.object.get(i);
    if(tempObject.getId() == ObjectId.Block){

    //bottom collision
    if(getBounds().intersects(tempObject.getBounds())){
    y = tempObject.getY() – height;
    dy = 0;
    falling = false;
    jumping = false;
    }else{
    falling = true;
    }

    //top collision
    if(getBoundsTop().intersects(tempObject.getBounds())){
    y = (tempObject.getY() + 64);
    dy = 0;
    }

    //right collision
    if(getBoundsRight().intersects(tempObject.getBounds())){
    x = (tempObject.getX() – width);
    }

    //left collision
    if(getBoundsLeft().intersects(tempObject.getBounds())){
    x = (tempObject.getX() + 64);
    }

    }
    }

  8. Thanks dear for your great tutorial .. I am really learning something here ..
    You Promised To Answer 😀 .. 
    There is one bug I was trying to remove which is .. The player can jump while falling .. which is something that we don't want .. so I was trying to find a solution for that .. and while I was checking there is something really interesting I found .. which in this method (this.getBounds().intersects(tempObject.getBounds())) .. while the loop is runnung it sometimes false and other times true  .. EVEN if the player is in the idle status .. and I don't know why exactly that behavior happened .. can you explain that ?? !! 

  9. That won't work for the future that collision code…

    It would be x = (float) (tempObject.getX() + tempObject.getBounds().getWidth()); for the left.

    Because adding on something like 40 to the x will mean we will be 40 pixels from the x of that block that block size is 32. Meaning if the blocks x was like 0 we would be set to x 72 and that I guess would be fine but if the block size was 64 we would be inside of it.

  10. Super tutorial! Great follow up on the first tutorial! Learned loads from it!
    About the collision detection:
    The number you have to use to reset the player's position after collision is not just a number, i think it's the size of the object you bump up against. For instance:
    if(getBoundsTop().intersects(tempObject.getBounds())){
             y = tempObject.getY() + tempObject.getHeight();   // I made height a parameter of GameObject…
              velY = 0;
    }

    Thanks again! Great stuff your making! Keep it up!

  11. I was planning to use the falling variable to make sure you cannot jump midair (you can now if you walk of an edge, and then jump). So my plan was to do something like this

    if(key == KeyEvent.VK_SPACE && !tempObject.isJumping && !tempObject.isFalling()) { 
    tempObject.setJumping(true);
    tempObject.setVelY(-10);

    }

    Turns out though, the falling variable is always true. Even though it should be set to false when you are colliding.

    if(this.getBounds().intersects(tempObject.getBounds())) {
    y = tempObject.getY() – height;
    velY = 0;
    setFalling(false);
    setJumping(false);
    } else {
    setFalling(true);
    }

    It seems like the problem is that the collision method is looping through all of the blocks so it will find also find the other blocks that it's not colliding with, triggering the "else" and setting falling to true constantly. Does anyone else have this problem? Does anyone have a solution for this? It's making me crazy… xD

  12. Hey, could someone tell me how to repair that sticky movement… It's bothering me since couple days. I'd be very grateful if anyone could copy their code. It's so much pain in the ass…

  13. When you run the game and the block or what to call it is falling down to the ground and it is supposed to stay there but my block is just going through the ground instead and disappears.

    What to do? Help!

  14. the movement is sometimes unresponsive when making multiple movements. example; going left and right or jumping sometimes doesn't calculate and I assume that is because of the game loop.  would setting the ticks per second to 120 fix that?

  15. Why would you hardcode the collision widths and heights in like that? If you decide to change your character size later on, you gotta go back and tweak everything.

Leave a Reply

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