# Handle Entities & Collision in large numbers [closed]

I am creating a simple 2D maze game and I am a little confused about entities & collision detection.

At the moment, my entity class is an abstract class containing position, velocity, sprite and a booolean named shouldRender, as well as a tick and render method. Should I go with this or lean towards a component based entity system? If so, how would I implement that in java?

Also, when I update and render my entities I have a class named Level, which holds an arraylist with all entities. When updating and rendering my entities, I iterate through this list and render the entities that are visible in the camera, via the shouldRender boolean. However, all entities are updated, which makes the FPS drop.

Also, when checking for collisions: should I use a Rectangleand loop through all entities in the list and check if the current entity is colliding with any other entity? Or should I lean more towards stuff like quadtrees etc? (And how would I implement those?)

Because when every entity has to check for collisions with all other entities on the map the game runs at 10-20FPS, which is not too good.

So, if anyone could explain this to me, I would be very thankful!

## closed as too broad by Josh♦Dec 22 '14 at 18:52

Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

• These are somewhat open-ended opinion questions... But I can offer opinions. Your basic class with position and stuff is probably fine for your "simple maze" game. The alternative to updating all the entities is to not update all of them. Maybe the ones that are offscreen don't need to move, or move as often... that would be fudging with game's universe's laws, but that's part of the Art. As for collisions, it's called "broad phase" where quicker checks eliminate possibilities. Quadtrees, yes. Even just sorting, say, left-to-right and only looking up/down the list til X is too far can help. – david van brink Dec 22 '14 at 16:36
• But say, in like stealth games where guards etc. walks off-screen, how do they handle the updating there in an effecient way? – Caithrine Dec 22 '14 at 17:03
• You've got two or three very broad questions here, each of which are pretty different. You should check out the help center and consider re-wording your questions to make them less open-ended. "Which is better" or "is this a good approach" sorts of questions are better suited for a dicussion forum, not SE. Also in the future please post a single question in each topic. – Josh Dec 22 '14 at 18:53
• ...As for offscreen guards. Either their AI needs to be updated each frame, or it doesn't. Generally this should be quite fast and probably isn't the cause of slowness... the N^2 collision checks are more likely. But like user55564 says below, Profile, profile, profile. – david van brink Dec 22 '14 at 20:24

Should I go with abstract class or lean towards a component based entity system?

What you are doing is fine. If its working and its fast enough, dont change it unless it doesnt meet all your requirements.

All entities are updated, which makes the FPS drop.

The first solution to this problem should also be: do I need to update all the entities? Is some of my entities stationary? Eg walls? If so, why bother calling the update method? Is it possible to create an list that contains ObjectsRequiringUpdates and only update those?

should I lean more towards stuff like quadtrees etc?

Did you have unnecessary collision checks? Did you check wall to wall collision? If so, remove all of these! The more collisions checks you can knock off the better. If you are working in a 2D maze game my guess is that quadtrees are not necessary and your problem is probably doing too much unnecessary work.

And finally, profile first to determine which area is the bottleneck first before applying optimizations.