I am trying to detect if the player is on an object with a tag "canHoldWeight". But the detecting if the player is on an object isn't working.

falling code called every tick:

    boolean isObjectSolid;
    boolean isOnObject;
    boolean falling = true;

    for (int i = 0; i < InitObject.getSizeOfAll(); i++)
        GObject object = InitObject.getObjectFromAll(i);

        isOnObject = checkCollision(InitObject.player, object);
        isObjectSolid = object.isHoldWeight();

        if (checkCollision(InitObject.player, object) && isObjectSolid)
            falling = false;

GObject code (parent of the player and the "ground")

public int rX, rY, hSpeed, vSpeed;
public int x;
public int y;
public boolean dying;
public double dx;
public double dy;
public boolean canHoldWeight;
private Rectangle bounds;
private boolean visible;
private Image image;

public GObject (Image img, int startX, int startY)
    image = img;
    visible = true;

    rX = startX;
    rY = startY;

    hSpeed = vSpeed = 0;
    bounds = new Rectangle(startX, startY, img.getWidth(null), img.getHeight(null));

public Rectangle getBounds ()
    bounds.x = rX;
    bounds.y = rY;
    return bounds;

public boolean checkCollision (GObject obj1, GObject obj2)
    if (obj1.getBounds().intersects(obj2.getBounds()))
        return true;
    } else
        return false;

if you need any more information please let me know :D

  • \$\begingroup\$ Just so you guys know, I am fairly new to java and am trying to do this almost entirely from scratch so don't roast my code too much lol. \$\endgroup\$ – MrEDog Jan 7 at 15:40
  • \$\begingroup\$ In what way is it not working? Does the player get stuck inside the ground object? Does the player object fall right through the ground object? Tell us more of the actual problem. \$\endgroup\$ – Daniel_1985 Jan 8 at 8:13
  • \$\begingroup\$ I managed to fix it \$\endgroup\$ – MrEDog Jan 8 at 18:20
  • \$\begingroup\$ Consider writing an answer to your question, where you provide information about what caused the problem and what you did to fix it. After that, after a day or two, mark that answer as the correct one. \$\endgroup\$ – Daniel_1985 Jan 8 at 18:29
  • \$\begingroup\$ Ok I will try, I have rewritten 99% of my code multiple times since I asked my question lol \$\endgroup\$ – MrEDog Jan 9 at 19:16

Hi sorry I'd prefer to comment but I don't have enough reputation on this account. Your code actually looks very clean by the way, I wish I could see the rest of your code.

I'm assuming your 'checkCollision' method is returning false. If this is the case, my guess is it has something to do with your call of the intersection function. Usually intersection refers to two shapes that are overlapping - when the interior of one shape is within the interior of another. For a game I am not sure this is something you are looking for.

I'd recommend measuring the distance between both objects - like the surface / bounds.y of the floor and the center or contact point of the player object. That way when the distance between them is zero you know they are on top of one another. The trick is if there are different heights then the bounds.y will have to update with the position.x of the player.

Hopefully this is helpful. If I am wrong then I apologize and you can ignore this.

| improve this answer | |
  • \$\begingroup\$ hmm ok, I will try to get it on github if that will help. I have not had much luck with github in the past lol \$\endgroup\$ – MrEDog Jan 7 at 16:33
  • \$\begingroup\$ yeah ok I couldnt get github to work lol \$\endgroup\$ – MrEDog Jan 7 at 16:48
  • \$\begingroup\$ Here this is a helpful link on collision detection: youtube.com/watch?v=NZHzgXFKfuY \$\endgroup\$ – Nicholas Jan 7 at 16:52
  • \$\begingroup\$ Collisions are tough especially if you are doing it from scratch. \$\endgroup\$ – Nicholas Jan 7 at 16:53
  • \$\begingroup\$ ok thanks for the help \$\endgroup\$ – MrEDog Jan 7 at 17:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.