Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Check for 2D Collision Without Checking Every Object

Tags:

java

2d

collision

I'm being really ambitious and working on a 2D Shoot 'em Up game that will have, hopefully, hundreds of entities running around.

What I'm having trouble wrapping my brain around, is how the bullet will detect when it makes a collision with an object, without it checking for every object on the map. The reason is that I feel that if I have four dozen bullets on the screen, each checking for collision with every entity on the map, every cycle, I will see some fairly significant performance loss.

So what would be the best way to detect for collisions without checking every single entity?

I can handle the collision algorithm when I have my two objects, I just can't seem to find a way to get those two object to see each other without checking everyone else first.

I'm working in Java and OpenGL with (soon to be textured) QUADS.

like image 667
A Name I Haven't Decided Yet Avatar asked May 01 '12 00:05

A Name I Haven't Decided Yet


People also ask

How do you detect 2D collisions?

The most basic way to approach this type of 2d collision detection is to utilize a concept known as Axis-aligned bounding box. Axis-aligned bounding box, or AABB, detects if two non-rotational polygons are overlapping each other.

How do you test for collision detection?

If both the horizontal and vertical edges overlap we have a collision. We check if the right side of the first object is greater than the left side of the second object and if the second object's right side is greater than the first object's left side; similarly for the vertical axis.

What method do we usually use for collision detection in games?

A hitbox is an invisible shape commonly used in video games for real-time collision detection; it is a type of bounding box. It is often a rectangle (in 2D games) or cuboid (in 3D) that is attached to and follows a point on a visible object (such as a model or a sprite).


1 Answers

You should investigate quadtrees; they're often used for efficient 2D lookup.

like image 181
Oliver Charlesworth Avatar answered Oct 20 '22 00:10

Oliver Charlesworth