Sri Harsha Chilakapati
Sri Harsha Chilakapati

Reputation: 11940

Java game development. Making a tile-map faster. Advices on optimisations

I'm trying to make a fully-automated game engine which needs to be working based on the events. The problem I'm facing is that I've made a map class and it's initialized in the game class. It's almost static and only one map exists for a game. New maps are loaded by clearing the objects in the current map and adding new maps. You can see the source of the map class here.

http://code.google.com/p/game-engine-for-java/source/browse/src/com/gej/map/Map.java

The main problems comes in the collision detection, where I'm using the brute-force collision detection where I should not. This slows down the game a lot and I wanna check the collisions only for objects which are nearer to the object. I've been using the MapLoader interface to construct the maps. I'm thinking that calling the collision() method of the objects in another thread might help. How-ever it all the map objects are updated in the Game class.

Here's the game class if in case it might help

http://code.google.com/p/game-engine-for-java/source/browse/src/com/gej/core/Game.java

There's another problem that some-times, the objects aren't destroyed. I'm calling the map's removeObject() method but it takes a 1-second delay and some times wont remove at-all.

It gives me 48-64 fps in a platform game with 158 objects in the game. But in a space-invaders style game, it gives me only 20-30 fps. Any advice on optimization is greatly appreciated...

If anybody could get me a tutorial for binary spacing etc., I would be thankful.

Upvotes: 1

Views: 863

Answers (1)

Jack
Jack

Reputation: 133577

Two suggestions by looking at your code: first thing is that you should try to minimize object allocation in collision detection, don't create new Rectangles, work on data you already have by writing directly the collision detection algorithm.

Second an more important thing: a collision detection engine should work by using two levels:

  • a coarse level of collision already excludes objects that are surely too far to collide (you can use many techniques here, like a binary spatial partition algorithm of big colliding blobs, or hierarchical structure of objects)
  • a fine level which will compute specific details for the collision that can occur, with more precise algorithms

Upvotes: 2

Related Questions