Hello, this is my first post here.
So I’ve been trying to program a 2d side scroller platformer on java by myself and it’s going okay so far (though programming slopes and moving platforms were horrific) but I’ve decided that perhaps attempting something like this without any sort of feedback from others may not be the best idea, so I decided tojoin this place to ask questions.
On to my problem. What’s happening right now is that my application occasionally gets sort of choppy. It runs okay, but I’d like the frame rate to be about as consistent as Cave Story, because even though it’s playable as is, I’m a bit of a perfectionist. The choppiness is NOT what’s affecting my collision, I have a special timer system built so that the game will either catch up or not move at all if too much lag builds up, so it’s not like im having problems with characters clipping through platforms or anything.
My main problem is that, in my hunt to remove the occasionally choppiness/random game freezes, i’ve been going through (attempting to) optimize my code (i don’t know much about code optimization unfortunately), and one of the things i got to is the way my collision system for objects works.
What I’ve done is I’ve created two arrays about the size of the room in pixels divided by 16 (so one average screen is about 40x40), one of them being an int array, the other one being a string array. what i’m doing is at the beginning of my collision loop I index the number of objects overlapping with a certain room tile in the integer array, and in the string array, i concatenate on the object id by converting it to two characters (0-65535, which should cover any number of objects that should get on screen). Then, during collision checking, each object reads off the list of objects from within every tile it overlaps and does collision checks with them. In this way, if I have some 200+ objects somewhere, they won’t ALL be doing collision checks with ALL of them (since that many checks a loop seemed like just a little too much)
But I’m wondering if their is another, more efficient way of being able to only do collision checks with relevant objects that might be faster.
Of course, I’m worried that this might actually only marginally be affecting my frame rate, and that the occasional freezing and choppiness might be caused by something else, so any general advice on optimizing is helpful too, as well as possibly advice on
- Loop Structure/Threading (particularly, should I make another thread for key inputs or something similar to that?)
- Graphics Rendering (Right now I’m mostly just using h.drawImage(), but is there some graphics library that would improve my performance? (Portability is important))
- Swing? (I’m using a JApplet right now, but would Swing or something else be faster? Or would it just be overkill)
- This one’s a little embarassing, but Building (I’m using Eclipse and so far I haven’t been able to turn even a Hello World project into a .jar file)
- Anything else that might possibly be the source of my strange skipping