[Slick2D] Extreme Lagging

Even more fun with this annoying errors within this game. This time it’s duper annoying and super game killing lag! Could someone please just review my source. Then tell me whats wrong, and what i should change.

Download: https://www.dropbox.com/s/6szr0uway97qcjd/Zombie%20Nauts.zip

I tried to fix the most things i could. It had no effect. So, I hope you more experienced developers will figure out what the problem is. Thanks in advance!

Actually i think ik the problem. Every time a zombie spawns it creates a new instances, so im pretty sure its just filling the JVM up with instances.

That appears to fix it a little but it stills lags pretty bad.

Nope, I was curious enough to try it, running my quad core flat out at 100%. There’s your problem.

Profile:

I don’t have time right now to look through it all, but it looks like somewhere there are a shitton of Shapes (looks like Rectangles) being created and processed. As in obscenely too many, considering this profile was only about ~15 seconds long, enough to see and kill 1 zombie.

Also Fraps reports 1600 FPS in the main menu, bouncing rapidly between 5 and ~1200 in game. Please limit to 60 to start with, People don’t want their graphics cards to melt.

Thank you soooooooo mcuh!!! +1 Ik exactly what the problem is. It’s the collision boxes and such.

I’m making the rectangles on a seperate thread now… will tht fix it?

To cap your framerate, go into your game.class (or whatever your main class is) and put appgc.setTargetFrameRate(60); under appgc = new AppGameContainer(new Game(gamename), startWidth, startHeight, false);

IE:

		AppGameContainer appgc;
		try{
			appgc = new AppGameContainer(new Game(gamename), startWidth, startHeight, false);
			appgc.setIcon(icons);
			appgc.setVSync(true);
			appgc.setTargetFrameRate(60);
			appgc.start();
		}catch(SlickException e){
			e.printStackTrace();
		}

That will bandaid it, you’re basically creating a bazilliondy rectangles instead of just one and moving it around. I haven’t looked at the code, but I’m guessing what you’re doing is destorying/recreating a new rectangle every time you need to move it instead of moving the existing one.

YaI just realized that each time the thread loops its setting bounds to a new rectangle… im now moving it :wink: Thanks soooo much guys. This game would nvr get done without this forum.

What’s stopping you from just using simple AABB collision detection so you don’t have to create rectangle instances in the first place?

I agree with using AABB, Heroes even made a post about it a while back: http://www.java-gaming.org/topics/fast-simple-aabb-collision-detection/28059/view.html

Translate that for your needs, and you should be good. Ideally, each entity in game (zombies, the player, etc.) have an x, a y, width and height, and then can do this simple collision check with any other entity.

Sample:


public class Entity { //subclasses: Zombie, Player, etc
      protected float x, y, width, height;

      public boolean collides(Entity e) {
            ...
      }

      //.... other stuff that all Entity's should have in common (health, isAlive, etc)
}

What he is doing with the rectangle instances is almost exactly the same as AABB collision.

Though i agree he should “extend” his program by "implement"ing OO program/class design. (haha , sorry that was lame… but kinda funny)

Also creating your rectangles in a different thread will do nothing. Won’t speed anything up and wont fix the fact that you are creating a ridiculous amount of Rectangles.

[edit] You are creating lots of threads that are never completing.

@Everyone Nvr knew AABB existed. @The Lion King And I already have fixed this error.

Went to Zombie class to look for something, but WTF is this?!


boundsT = new Thread(new Runnable() {
      public void run() {
            while(true) {
                  bounds = new Rectangle((int)x, (int)y, main.getWidth(), main.getHeight());
            }
      }
});

It gets called every time a Zombie or Bullet is constructed. :o

This serves literally no purpose other than to attempt to destroy one’s computer.

If you were intending this to keep bounds ‘synced’ to the x,y, and main fields, this is not how to do that.
This suggests a fundamental gap in your understanding of programming concepts (which can be dangerous, luckily Java tends to be forgiving, if this was C, your comp might be fried right now), so I urge you to take a step back and get a good book / read the official Oracle tutorials, and also read up on Object Oriented concepts. You will thank me later.

I removed it from Zombie and Bullet, voila, no lag.

You say you have 3 years experience, but in reality, I think I have more game programming experience… Kinda strange.

Here is a link to my zombie game.
http://www.desura.com/games/zombie-massacre/downloads/zombie-massacre-game-files
I mean, try and play it and see the amount of zombies.

If you’re spawning a lot of zombies, the problem isn’t that you check for collision. The problem is that you’re constantly checking for collision between zombies that are very far away.
You need to have a list of entities that contains all the level entities. This list shouldn’t be touched when it comes to collision. It should be used only to render/update entities.
You need to chunk your map into chunks of lists. Each chunk would have certain area on the map. You need to assign each entities to 1 or few chunks, so you know on which chunk the entity is. When you need to check for collision, check which chunk you’re on and get entity list from that chunk.
It doesn’t have to be chunks. Tiles do just fine :smiley: Though I’m running 16x16 pixel tiles atm in my game, and i don’t really have problem, but running 128 or 256 chunks would probably be better for performance I am guessing.

I’ve actually wondered about this.
If we are to believe his profile, he has been programming since 8, and programming in Java since 10.

Yet some of the questions being asked lead me to believe otherwise.

@Jacob Pickens: Do you really have 3 years experience in Java? and 5 years programming overall? Be honest.

Just to emphasize. An average 8 year old doesn’t know how to do multiplication and would be very gifted if they truly understood the concept of a variable.

Yes I do. When I got my first computer (When I was 8 ) I instantly download GameMaker and made games in that for about 4.5 years (Just recently stopped to start using java games). During that time I also started learning tiny bits of Java. I never really programmed a lot of GUI stuff though. Then finally this current year I told myself that Gamemaker was babyish. (Being 13 I didn’t want anything to do with anything babyish) So I started researching Java game development. Honestly, I’ve only had like 2 months of Java game programming experience. So, ya I didn’t lie… I don’t think?

@The Lion King Well then I must be gifted because I’ve been programming since I was 8. ;D

2 months ≠ 3 years.

When it said experience i thought it meant Java not java game programming… i’ll change it now