Saturday, April 18, 2009

Starfighter: Part 1

Introduction

I am eventually hoping to start up a commercial website dedicated to entertainment. I have decided to use Java due to its relative platform independence and the fact that I have spent a lot of time on it as a hobby.

Towards this effort, I have been working on a number of games. I am currently focusing on a game I have titled: Starfighter.

When I first tried to write games, I spent plenty of spare time trying to develop a Java based scene graph, or else find an effective open source solution. However, I found both options to be difficult and time consuming.

Which is why I became excited when I first learned about JavaFX, a scripting language dedicated to graphics.

Handicaps


One of the largest handicaps I have had, is JavaFX's current incompatibility with Linux. After finally getting tired of viruses getting past my firewall and ruining my computer, I ended up switching to Linux. My computer has not demonstrated any virus problems since, but this is problematic when programming in JavaFX, because it is officially unavailable for Linux and Solaris. According to this article, it is because of difficulty in implementing graphics acceleration and video.

I have managed to get around this using a popular hack, but I have thus far been unable to get video to work and I have had to develop a large number of typing habits to compensate for the buggy code editor.

Curiously, all of the samples on the JavaFX website seem to work on my computer, even video. The only setback is that they are slower on Linux than they are on a Windows computer (and in one case unmanageably so).

It is rather nice not having to write my own scene graph, but JavaFX still seems to lack a major component for graphics design: Collision Detection.

Collision Detection


Collision detection refers to when a program checks to see if any of the sprites overlap. Then of course, it presumably calls a function to respond to the stimuli appropriately.

I received major headaches when trying to implement this in Java. Fortunately, I have a better idea of what I am doing this time.

The simplest form of collision detection would be rectangle based and use the brute force method. Basically, a rectangle would bound each sprite, with its minimum and maximum x and y points corresponding to those of the sprite. Then, the program would mathematically calculate one sprite against every other sprite to see if any of their rectangles overlap.

The problem with the rectangular bounds method is that the rectangles cannot accurately indicate whether or not various shapes, such as a circle or a line intersects with another object. I will be unable to work around this problem for now.

The problem with the brute force detection method is that for every sprite I add to the scene graph, the amount of collision checks increases exponentially, as every sprite must be checked against every other sprite.

There are two methods I am aware of that can take care of this problem: Quadtrees and Cells.

Both are ways of dividing the space the sprites exist in into sections. That way, each sprite is only matched against sprites in the same section.

Since I do not know very much about Quadtrees, and I am hoping that the JavaFX team will eventually resolve this, I am implementing a variation of the Cell method.

0 comments:

Post a Comment