I am developing a game named
Business Magnate. This post is part 2 of its development blog (
part 1 is here).
For making the isometric engine more useful, we need to be able to click onto buildings, in order to open dialogs and similar. Detecting if a user clicked on a building isn't actually that difficult: Just check if the mouse coordinates are within the rectangle of the drawn sprite, and if the hit pixel is solid. We also need to check if another building is drawn in front of the current one, and then take that one, of course. For business magnate, I also added a hit-test cache:
The red overlay is the hit cache image, it won't be visible in the final game
What it does is basically speeding up the hit-testing: Every frame, I need to lookup which building is under the mouse cursor, and do a image lookup to do pixel perfect hit-testing. For big images, and a lot of them, this could mean that a lot of data needs to be kept in memory for this, increasing cache misses, or even worse: transferred between GPU and main memory. To prevent this, a size reduced and only 1-bit-per-pixel version of the image is created and held in memory, to do very fast hit-testing.
If for example we have 10 building types with a size of 512x512 pixels, we would need about 10 MB for this, which would have needed to be transferred and unlocked. The hit-testing images only need 0.3 MB instead, which is a very nice improvement for this.
The fact that the image isn't exactly matching at the borders is negligible, the error is too insignificant for users to notice. I tried it out, and it feels very nice. Also, the game runs quite fast that way on slower systems.
Additionally, I implemented a quick way to easily create streets:
A simple algorithm quickly scans for neighbouring tiles after the user placed a new street tile, and adjusts the tiles to connect in order to form streets. No magic there.
I'm not yet sure if it will be necessary in the game to have an efficiently planned street layout, but the company and building setup you can create in the game will definately look a lot nicer with proper streets.
If you like the game, you can
subscribe to the newsletter on the
game's website, or follow me on
twitter.