I am trying to determine collision in a 2D Side Scroller environment using Rectangles. However, the way most people do it is to get the bounds of the Tile. I do not use Tile's in this case, I am just drawing Simple Full Colored Rectangles using Graphics2D.

g.drawRect(int x, int y, int width, int height)

I would obviously need a GameObject Class, which I have below, and I do check for an intersect, but I don't think that would actually check for collision because I dont know where it should be called. Also, how would you know which parameters to pass? I also do not believe that I need everything in the class, and maybe I am missing something. It would be greatly appreciated if someone could give me tips to improve upon the class aswell.

package com.github.geodox.echo.game.entity;

import java.awt.Rectangle;
import java.awt.image.BufferedImage;

import com.github.geodox.echo.game.GamePanel;

public abstract class GameObject
    protected BufferedImage objImage;
    protected double xmap;
    protected double ymap;

    //Position and Vector
    protected double x;
    protected double y;
    protected double mx;
    protected double my;

    protected int width;
    protected int height;

    protected Animation animation;
    protected int currentAction;
    protected int previousAction;

    protected boolean forward;
    protected boolean jumping;
    protected boolean falling;

    protected double moveSpeed;
    protected double maxSpeed;
    protected double stopSpeed;
    protected double fallSpeed;
    protected double maxFallSpeed;
    protected double jumpStart;
    protected double stopJumpSpeed;

    public GameObject()


    public boolean intersects(GameObject o)
        Rectangle r1 = getRectangle();
        Rectangle r2 = o.getRectangle();
        return r1.intersects(r2);

    public Rectangle getRectangle()
        return new Rectangle((int)x - width, (int)y - height, width, height);

    public int getX()
        return (int) x;

    public int getY()
        return (int) y;

    public int getWidth()
        return width;

    public int getHeight()
        return height;

    public void setPosition(double x, double y)
        this.x = x;
        this.y = y;

    public void setVector(double mx, double my)
        this.mx = mx;
        this.my = my;

    public void setDirection(boolean forward)
        this.forward = forward;

    public void setJumping(boolean jumping)
        this.jumping = jumping;

    public void setFalling(boolean falling)
        this.falling = falling;

    public boolean notOnScreen()
        return x + width < 0 || x - width > GamePanel.getWindowWidth() || y + height < 0 || y - height > GamePanel.getWindowHeight();


Recommended Answers

All 4 Replies

Each rectangle is a tile, or can be thought of as living inside a tile if it's rotated.

What you need is to determine the bounding boxes around your objects, and then determine whether those bounding boxes overlap.
If your objects are simple rectangles that aren't rotated, the rectangle itself is its bounding box (though you might want to add a few pixels if you want to detect a collision before it actually happens).

So you have for example 2 boxes of 10x10 pixels, existing at <12, 23> and <21, 19>.
It's now a simple matter of geometry to determine whether those two boxes overlap (these two do).

Makes sense, however how would you check that with a player? The player would be moving, so how would you determine which object is the closest since you wouldnt just want to check if the player is colliding with all other objects?

For clarification, you would just use the x and y positions of the rectangle and the width and height to determine the top right, bottom right, and bottom left? Would you even need to determine those points? or could you just use the x, y, width, and height to determine the bounding box?

The objects are not rotated by the way.

The player would be moving, so how would you determine which object is the closest since you wouldnt just want to check if the player is colliding with all other objects?

Checking for rectangles overlapping is so quick & easy that you may as well just do it for all the possible objects - it's only going to cost you a few microseconds. Deciding which ones to check will probably cost more than just doing the check!
Checking for overlaps is probably ueasier using top, botton, left , right coords

yes, and if your area is very large, maintain a larger box around the player, check all objects if their centers are in that box, then if they are check if there's a collision.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.