Checking rectangles for arbitrary intersections?

Hi, I was just wondering if there is a way to check a Rectangle to see if anything is intersecting it. I know I can get the Rectangle intersection if you pass in another Rectangle, but I really want to know because right now with my current logic it checks a method every frame, when it could be checking it only if the bounds come in contact with any other bounds. For instance I have this:

Tools.ModifiedTools.addBehavior(tidus,
				new ID.Action(tidus){
					@Override
					public void onCollision(){
						if(Tools.ModifiedTools.checkForCollision(tidus, yuna)){
							Tools.ModifiedTools.setAudio(tidus, 0);
							Tools.ModifiedTools.playAudio(tidus);
							System.out.println("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa");
							}
						else if(Tools.ModifiedTools.checkForCollision(tidus, lulu)){
							Tools.ModifiedTools.setAudio(tidus, 1);
							Tools.ModifiedTools.playAudio(tidus);
							System.out.println("bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb");
						}
					}
				},
				ID.Behaviour.animate,
				ID.Behaviour.playerControlledWASD,
				ID.Behaviour.updateByVelocity,
				ID.Behaviour.updateColliders,
				ID.Behaviour.detectCollisions
				);

and my collision detection method looks like this:

public static boolean checkForCollision(Entity a, Entity b){
			if(a.check(ID.Tag.boundstag)){
				Rectangle collider = Systems.LogicSystem.colliders.get(a.hashCode());
				Rectangle bCollider = Systems.LogicSystem.colliders.get(b.hashCode());
				if(collider.intersects(bCollider)){
					return true;
				}
				return false;
			}
			return false;
		}

Every frame it runs detectCollisions, which calls the method onCollision(). I want to be able to make it so that during detectCollisions, it checks to see if the Entity’s bounds are intersecting or contains any other Rectangle, and only call onCollision() if that returns true, but so far I have found nothing that makes it seem that can be done. Is there no way?

Just brute force?


Rectangle collider;
for(Rectangle r : Systems.LogicSystem.colliders)
    if(collider.intersects(r) && collider != r) return true; // substitute the return with onCollision(collider, r) if that's what you want
return false;

Well that’s just adding an inefficient extra step though? If I have a walled room and I only want to check for collisions with 2 things then the original steps are more efficient.

I’m finding it difficult to figure out what exactly it is you’re asking.

If it solves the problem, brute force is fine unless you have 100s of entities.
If it’s really a concern, you could have a [icode]Systems.LogicSystem.rooms.get(currentRoom).colliders[/icode] or something that contains just the collision bodies relevant to that room.

I mean being able to call something like

if(collider.isIntersected){
              //call onCollision()
        }

but I suppose such a method would be impossible

More or less, yes. When it comes down to it, all collision detection algorithms are brute force, it’s just that the clever ones figure out how to shorten the list of potential collisions to check. (Such as the list of colliders per room model I mentioned)

There’s no way to know if something is colliding without checking if it is.