Updating collisions makes huge fps drops... ?

First I would like to apologize for my bad English and noobness level of over 9000. ::slight_smile:

I’m making a little 3d game with lwjgl but I came into a huge issue. I have a grid made out of squares and I am checking collisions for all of them. I am using the “ground square’s” collisions to get some kind of path finding and stuff like that… This appears to be making my fps drop from my 60(that is the fps limit I have) to 25 - 30. When I disable all of the squares’ collision checking the game runs just perfectly smooth. I am getting all the squares that are next to the object’s current one, so I can make the object not to be able to go to the square if I have set it to be impassable.

Here is the code I have to get the squares’ collisions:

public void update(){
		
		// c means in game character...
    // Yes... I know I should have used arrays here on "nextGroundPieces" xDd
		for(int i = 0; i < GameBoard.c.length; i++){ 
			
			
			//  ! ! ! gpi = Ground piece array index number ! ! !
			for(int gpi = 0; gpi < World.map.gp.length; gpi++){
				if(World.map.gp[gpi].getCollision(GameBoard.c[i].getX(), 0.0f/* This is because currently collision is done using only X and Z */ , GameBoard.c[i].getZ())){
					GameBoard.c[i].setCurrentGroundPiece(gpi);
				}
				
				if(Map.gp[gpi].getCollision(GameBoard.c[i].getX(), 0.0f/* This is because currently collision is done using only X and Z */ , GameBoard.c[i].getZ() + 1.25f)){
					GameBoard.c[i].setNextGroundPiece1(gpi);
				}
				
				if(Map.gp[gpi].getCollision(GameBoard.c[i].getX(), 0.0f/* This is because currently collision is done using only X and Z */ , GameBoard.c[i].getZ() - 1.25f)){
					GameBoard.c[i].setNextGroundPiece2(gpi);
				}
				
				if(Map.gp[gpi].getCollision(GameBoard.c[i].getX() + 1.25f, 0.0f/* This is because currently collision is done using only X and Z */ , GameBoard.c[i].getZ())){
					GameBoard.c[i].setNextGroundPiece3(gpi);
				}
				
				if(Map.gp[gpi].getCollision(GameBoard.c[i].getX() - 1.25f , 0.0f/* This is because currently collision is done using only X and Z */ , GameBoard.c[i].getZ())){
					GameBoard.c[i].setNextGroundPiece4(gpi);
				}
				
				
				
				if(Map.gp[gpi].getCollision(GameBoard.c[i].getX() + 1.25f , 0.0f/* This is because currently collision is done using only X and Z */ , GameBoard.c[i].getZ() + 1.25f)){
					GameBoard.c[i].setNextGroundPiece5(gpi);
				}
				
				if(Map.gp[gpi].getCollision(GameBoard.c[i].getX() - 1.25f, 0.0f/* This is because currently collision is done using only X and Z */ , GameBoard.c[i].getZ() - 1.25f)){
					GameBoard.c[i].setNextGroundPiece6(gpi);
				}
				
				if(Map.gp[gpi].getCollision(GameBoard.c[i].getX() + 1.25f, 0.0f/* This is because currently collision is done using only X and Z */ , GameBoard.c[i].getZ() - 1.25f)){
					GameBoard.c[i].setNextGroundPiece7(gpi);
				}
				
				if(Map.gp[gpi].getCollision(GameBoard.c[i].getX() - 1.25f , 0.0f/* This is because currently collision is done using only X and Z */ , GameBoard.c[i].getZ() + 1.25f)){
					GameBoard.c[i].setNextGroundPiece8(gpi);
				}
			}
				GameBoard.c[i].setPos(GameBoard.c[i].getX(), Map.gp[GameBoard.c[i].getCurrentGroundPiece()].getY(), GameBoard.c[i].getZ());
		}
		
	}

And yes I know this is made absolutely horribly but it would get the job done if it wouldn’t make the fps drop so badly. :persecutioncomplex:

Please help me. What can I do? :’(

What’s the type of game are you making? A chess game in 3D or what?

No …or kind of… , just playing around. But thats not the point.

Please be specific. What do you mean by that?

How many ground pieces and characters do you have?

Well… I have around 6400 squares and I am checking if any of those contains any objects (those “characters”). And if there are, it checks all the surrounding squares. I am updating the “check” all the time, and it appears to be dropping fps. But I cannot “pre-calculate” it cuz the objects are moving and stuff changing… The fps stays quite high if I have only 2 or 3 objects but the problems start at around 10. It has something to do with the collision checking for all of the squares but I cannot understant why it appears to be so “heavy” and rough to check 'em.

Okay… I just noticed that if I lower the ground pieces to 500, it improves the performance like… a lot. ;D But if I needed to have that huge amount of those ground pieces how could I be able to handle this? Or would it be even possible?

Would the logic still be correct if you only checked an area of World.map.gp around each game piece? That would be more performant than checking the entire world every time you want to know if there’s someone standing next to you. :wink:

If the answer is ‘yes!’ but followed by ‘how would I do that,’ then I must ask, how is World.map.gp structured?

Yup… This actually helped, I got it fixed and now its running smooth and nicely ;D I should have understood this earlier by myself. What a stupid mistake.
Thank you!