Problem with a boolean in a frogger type project

Hello there,

There is Boolean in the Road class (Boolean ok) and I cant figure out what is going on with it.
It always set to false and after a few hours of trying to solve it ,I can’t figure out why.
I think it might have something to do with my hit detection in its base class.
I tried changing my Hit detection but that didn’t really help.
If someone could take a look to see if there is anything strange it would be much appreciated.

( I will add the base class here as well as the class in question)
( I am working on a frogger project)
( If you need a runnable program please let me know)

public class Rectangle
{

  float x;
  float y;
  float w;
  float h;
  float left;
  float right;
  float top;
  float bottom;

  Rectangle(float x, float y, float w, float h)
  {
    this.x = x;
    this.y = y;
    this.w = w;
    this.h = h;
    left = x;
    right = x + w;
    top = y;
    bottom = y + h;
  }

  public  boolean isHit( Rectangle other)
  {
    return !(this.left > other.right||
      this.right < other.left ||
      this.top> other.bottom ||
      this.bottom< other.top);
  }

  //GETTERS

  // SETTERS
  public void setX(float newX)
  {
    x += newX;
  }
}

public class Road extends Rectangle
{
  // this class holds all the objects that 1 road needs 
  // in here the cars and logs will be stored
  Object[] objects;
  float speed;
  float offset;
  boolean isLog;


  Road(float x, float y, float w, float h, float speed, float offset, int numOfObjects, boolean isLog)
  {
    super(x, y, w, h);
    this.speed = speed;
    this.offset = offset;
    this.isLog = isLog;
    objects = new Object[numOfObjects];

    float tile = 50;
    for (int i = 0; i < objects.length; i++)
    {
      objects[i] = new Object(offset * i, y, w, tile, speed);
    }
  }


  void display(float screenWidth)
  {

    for (Object object : objects)
    {
      checkLogs();
      object.rebound(screenWidth);
      object.move();
      object.display();
    }
  }


  void checkLogs()
  {

    if (frogRowNum > 11)
    {
      boolean ok = false;
      for (Object object : objects)
      {
        if (player.isHit(object))
        {
          println("isHit : "+player.isHit(object));
          ok = true;
        }
      }
      if (!ok)
      {
        println("ok : "+ok);
        resetPlayer();
      }
    }
  }
}
1 Like

are we talking mainly about this section?

did you try && instead of || ?

what’s the source please?

I tried && and it still has the same problem.
What do you mean with source?

Okay so I wrote a small program to see if I understand this but I clearly don’t and even after looking up rectangle on rectangle hit detection I still don’t understand why this doesn’t work’
I don’t understand why my hit detection (Boolean isHit) isn’t working and I think that is why my other program isn’t working.

If anyone could help me that’d be great
thanks in advanced

class Rectangle
{
  float x;
  float y;
  float w;
  float h;
  float speed;

  Rectangle(float x, float y, float w, float h, float speed)
  {
    this.x = x;
    this.y = y;
    this.w = w;
    this.h = h;
    this.speed = speed;
  }

  void show()
  {
    fill(200);
    rect(x, y, w, h);
  }

  void update()
  {
    x += speed;
  }

  boolean isHit(Rectangle other)
  {
    if (x + speed >= other.x + other.w + other.speed && 
      x + w + speed <= other.x + other.speed &&
      y >= other.y + other.h && 
      y + h <= other.y)
    {
      return true;
    }
    return false;
  }
}


Rectangle rec1;
Rectangle rec2;

void setup()
{
  size(500, 500);
  rec1 = new Rectangle (50, height/2, 50, 50, 2);
  rec2 = new Rectangle (width-50, height/2, 50, 50, -2);
}

//draw (executes 60 fps)
void draw()
{
  background(0);


  if (rec1.isHit(rec2))
  {
    rec1.speed = 0;
    rec2.speed = 0;
  }
  
  rec1.update();
  rec2.update();
  rec1.show();
  rec2.show();
}


1 Like

That’s what I meant by source

I can’t remember the exact if clause but it’s checking whether we don’t have a collision. So maybe swap true and false? No

1 Like

The error is maybe somewhere else?

Oh the problem was with the hit test that I was doing. I changed it so how they showed it in on the website you sent and it worked! So I probably messed up my values. Thank you for the help!!
(this is what I changed it to)

 public  boolean isHit( Rectangle other)
  {
    if (x + w > other.x &&
      x < other.x + other.w &&
      y + h > other.y &&
      y < other.y + other.h)
    {
      return true;
    }
    return false;
  }
1 Like