Improving 2D Games
Hello, I started writing java about 1 year ago with my primary interest being game development, I have written about 5 games or so in that time, 4 animated and 1 just using swing components. In writing these games I found that I must be doing something fundamentaly incorrect since I have seen similar games on the snes perform much better. So my question is mostly about how I can improve performance/image quality of the games I am writing. I will explain a couple of the algorithms I am using which I know there must be better solutions to:
1. Movement: When moving a character (or any other object) in one of my games, I generaly create an x destination variable and a y destination (there character is moving based on mouse clicks). Each moving character (or other object) is on its own thread, it iterates through a loop while x != xDest and y != yDest. First problem with this is that i think there is a more efficent way of doing this, second is that I can only move in 45 degree angles and straight lines.
2.Drawing Images: I am just using plain old Image objects, updating their coordinates, and repainting. Is there a way I could improve my performance/quality by using a VolitileImage or BufferedImage? or maybe by performing some other technique like double buffering (don't JPanels do that for you?).
There are probably a couple more that I have questions about, but I can't think of them right now. Any help would be greatly appreciated, thanks in advance.
first off, search these forums for methods of keeping the frame rate constant, that's probably what'll help you most. Second, perhaps you just need a more advanced way of moving things.
In your mouse clicks and key pressed, just set flags that will be checked in your game loop. Then inside your game loop, you can manage all of the movement in one place. Then instead of moving your character's destX 5 pixels to the right for pressing right or whatever, make his velocityX += runAccel.
Then when you do something like jumping, manage in this fashion:if (up && characterIsOnGround()) {
velocityY += jumpStrength;
}
Where the "up" variable has been set to true because your keyPressed set the flag when you pressed the UP key.
Then, in your game loop, you have a spot to manage gravity or water effects or air resistance and all that, and in it you say, for gravity, velocityY -= gravity; Then after all of your key press checks and environment checks and so on, you check for collisions. Such as... If (characterIsOnGround()) {
velocityY = 0;
y = characterHeight + whateverBlockYouAreOn.getY();
}
Hope that helps get you thinking in the right direction!
Don't create a separate thread for every character. This is very slow, especially if you have many characters, and it can lead to a lot of issues with synchronization. Instead, what games do is have one single thread which repeatedly loops through each character and updates its position, then repaints the screen, then sleeps for a short while.
Unfortunately, there doesn't seem to be any general-purpose Java game programming tutorial anywhere on the Internet (at least none that I could find), but you can find some very good articles about other languages at http://www.gamedev.net.
Also, if you're really interested in this topic, you should look for a game programming book. There are many more of these being written these days, and I've seen some great ones even 4-5 years ago, so hopefully most of them are high-quality.
Matei at 2007-7-2 0:39:23 >

Thanks, I think that will actualy help me quite a bit. I guess I hadn't really been thinking about it in terms of real life physics, but I see how it would make sense to do it that way. Creating a game loop might be a good first step for me (anything else I should know about that would be nice), I think I would know how to use that, but I have never done it that way in the past. Instead of setting flags in a game loop, I was creating an ActorMover object, passing it an Actor object as well as a movement type; I then called the ActorMover's start() method wich would move the Actor on its own Thread. I'm not sure how I will use this game loop to handle different types of movement for npcs, but I'll figure it out, thanks for gettin me started. Anyone else have any tidbits that could help me out?
To resolve your problem of only moving in straight lines, I recommend you use floating point coordinates. Java 2D directly supports using floating point coordinates, so you wouldn't even have to do any special translations yourself. But, you would have to change your test case from seeing if you're exactly where you want to be, to instead seeing if you're close enough to where you want to be. "Close enough" would be be dependent on how fast you move each turn.
For your image drawing question, I would say that moving to VolatileImages, which can be kept in video card memory, will most likely run faster, but would require more code to support. You should probably leave graphics improvements until after you have implemented your engine improvements, like the other people's suggestions of using a single thread for moving characters, as opposed to all characters having their own thread.
Thanks for the suggestions, I actualy didn't know java2D supported floating point coordinates, but I have actualy ported my game to jogl and I am using floating point coords with it.