This semester, I'm taking a programming class which uses Java. Not being at all familiar with Java, I was pleasantly surprised to discover that it is very similar to my main language, C#.
While investigating Java game programming I came across jMonkeyEngine (a Java 3d game engine), and while going through some tutorials on their site I came across an AStar tutorial.
I decided to gain some Java experience by implementing AStar in Java. I got a basic version working pretty quickly, but it wasn't taking the fastest route - it was taking 9 blocks to do what should only have taken 8.
I kept playing around with it and got it down to finding the fastest route, but it wasn't nearly as efficient as it could be (it kept searching blocks that it didn't need to). I fixed this by recording what iteration the algorithm was on when each block was discovered, then weighting my search to sort first by distance (asc), then by iteration (desc).
So, given the following blocks:
Block | Distance From Target | Iteration |
---|---|---|
1 | 10 | 1 |
3 | 10 | 2 |
2 | 20 | 1 |
the old version would pick either block 1 or block 3 when searching; the new version would always pick block 3. Anyways, the end result is that the path my program finds is identical to the one in the tutorial:
Not really a big accomplishment, but it's exciting for me, so I figured I'd post it.
On a side note, I just changed the font to Arial then went back to the HTML and ugh! It took my nice simple text and added a span on every line with inline styles all over the place. Blechh.
Download Sample (Open the HTML page)
Download Source Code (Netbeans project)
No comments:
Post a Comment