more Efficient Gravity

My gravity script is extremely inefficient. Basically it brings down a 80 fps to a 4 fps by itself.

here it is:

public void Gravity(){
	y+=Minions.gravity;
	for (int i=0;i<c.size();i++){
		if (bounds().intersects(c.get(i).bounds())){
			if(c.get(i).destroyed==false){

				y=c.get(i).y-9;
	}
	
		}
	}
}

this gets called every frame
I know that it is the for loop that is causing all of the problems but i don’t know how to fix it

Do you check if the object that is dragged by gravity actually jumped?

the objects dont jump but they just equal the block that they are touching’s y

I’m not completely sure what you ment there, but do you mean that you want the player to stay on top of a block?

oh sorry, my ground is like terraria in the sense of many little blocks being rendered which is prob why the for loop is so slow :-\

What does the variable “c” represent? I’m guessing it’s a column of blocks, but have no idea what type they are except that they have “bounds”. It’s probably intersect() that is taking the most time, but without knowing what types any of these variables are, it’s anyone’s guess. If unsure, I would run it through a profiler - these are indispensable when trying to sort out performance issues like these. I’ve made a lot of assumptions in the past that turned out to be wrong after profiling.

^ yup. Explaining what the variables hold and what exactly the methods do would probably help a lot.

in 99% when you have a performance problem, take a look at the complexity of your code. With this I mean, do you do the calculation 100 or 1.000.000 times.

From the informaion give I can only guess, but I think that you are something like comparing every entity/block with every other entity/block. This would have a N*N complexity which is bad.

Think about a way where you only have to check on a small subset of your entities for interesction.

“c” is are the blocks that have been spawn and @Danny02 your probably right, ill just lower the nmber of blocks being accesed

Please, for the love of god, don’t check all the blocks. Check the block below the current y position of the entity. If the block below the entity is missing, perform your falling. I think what you’re doing right now is looping through all of the blocks in your map, and that’s horribly inefficient, and it’s honestly bad coding… just check the block below the entity. That’s all you have to do.

I fixed it thanks guys.