1

I have a set, in the World class, for an object called collidable:

Set<Collidable> collidables = new HashSet<Collidable>();

While trying to develop a collision detection system (for a ball), I made two for loops, for X and Y.

cboxX = (int) Math.floor(position.x - RADIUS);
cboxY = (int) Math.floor(position.y - RADIUS);
cboxW = Math.abs((int) Math.ceil(nextPosition.x + RADIUS) - (int) Math.floor(position.x - RADIUS));
cboxH = Math.abs((int) Math.ceil(nextPosition.y + RADIUS) - (int) Math.floor(position.y - RADIUS));

for (int x = cboxX; x <= cboxW + cboxX - 1; x++)
{
    for (int y = cboxY; y <= cboxH + cboxY; y++)
    {

    }
}

Everything is good here. However, inside the for loop, I am trying to check for collidables with x and y parameters, but due to the fact that I am creating a new instance of a collidable (albeit with the exact same parameters as one that was previously generated), it will always turn up false:

world.collidables.add(new Block(new Vector2(x, y)));
System.out.println(world.collidables.contains(new Block(new Vector2(x, y)))); //returns false

However, if I use the same instance of block, it will turn up true:

Block b = new Block(new Vector2(x, y))
world.collidables.add(b);
System.out.println(world.collidables.contains(b)); //returns true

This is unacceptable however, as the entire reason for having two for loops was to not have to iterate over every collidable, every update.

What I'm asking is, does anyone know of a way to get whether a collidable is at the location I am specifying, without having to iterate over the entire set?

LulzCop
  • 131
  • 1
  • 9
  • Again, that would require iterating, which I can't do for performance reasons. If I could, I would just iterate and check if that object's vector has the same values as the position I am at. – LulzCop Jan 09 '13 at 03:32

1 Answers1

5

You need to provide you own implementation of two methods:

int hashCode()
boolean equals(Object o)

These methods should be implemented such that c1.hashCode() == c2.hashCode() if and only if the attributes (vectors) of the two instances are equal. At the same time hashCode() must be consistent with equals(Object o) in the sense stated by documentation:

  • Whenever it is invoked on the same object more than once during an execution of a Java application, the hashCode method must consistently return the same integer, provided no information used in equals comparisons on the object is modified. This integer need not remain consistent from one execution of an application to another execution of the same application.
  • If two objects are equal according to the equals(Object) method, then calling the hashCode method on each of the two objects must produce the same integer result.
  • It is not required that if two objects are unequal according to the equals(java.lang.Object) method, then calling the hashCode method on each of the two objects must produce distinct integer results. However, the programmer should be aware that producing distinct integer results for unequal objects may improve the performance of hashtables.
Jack
  • 131,802
  • 30
  • 241
  • 343