I have been using Quadtrees since the brute force is pretty slow in a platform game I was making. I’m checking in this loop.
for (Entity entity : EntityList)
{
entity.update();
entity.move();
for (Entity entity2 : EntityList)
{
if (isCollision(entity, entity2))
{
entity.processCollision(entity2);
}
}
}
It almost got 400000 checks and is somewhat slow. Then I had kept a flag collisionListener in the entity and checked for collisions only for the entities having that flag set to true. This reduced the collision checks to 238 since I’m only checking the collisions on the player and enemies. This raised my fps about 10 but my CPU usage is 68%. To minimize this, I changed my game loop to this.
long now = getCurrentTime();
long gameTime = getCurrentTime();
long updateTime = 1000/30; // Cheat to run at 30 ups
long maxFrameSkip = 5;
int frame = 0;
while(game.running)
{
frame = 0;
while(now + updateTime > gameTime && frame <= maxFrameSkip)
{
updateGame();
gameTime += updateTime;
frame++;
}
displayGame();
}
But the CPU consumption never decreased. So implemented a QuadTree from Quick Tip: Use Quadtrees to Detect Likely Collisions in 2D Space at TutsPlus. The collision checks are now 19 but the CPU consumption increased by one percent and the fps dropped by a bit.
The fps is ranging from 120-141. I know it’s great but it’s just at 38 on my old laptop running XP. What are the changes I need?
To test, I’ve made a prototype of it using GameMaker and it’s using just 13% of my CPU and at the same time is doubly fast. I am trying to meet it.
Thanks in advance for help.