• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Java Vectors

Kailas

Newbie
Hi everyone.

I'm working on a 2d android game with fairly simple physics. Every update all movable objects are updated

  • They apply their velocity (add velocity * deltaT to position).
  • They check for collisions against any walls in the level.
  • Then they find which grid spaces they are in and give the physics manager a vector containing the coordinates of each space they are in.
  • After updating all objects the physics manager goes through each grid space in the level and gets a vector containing all of the objects in that space which it performs the following loop on.
for(int i = 0; i < objects.size(); i++)
{
for(int j = i + 1; j < objects.size(); j++)
{
if(!objects.get(i).equals(objects.get(j)))
{
CheckCollisions(objects.get(i), objects.get(j));
}
}
}

  • Then .clear() is called on the objects vector so that it is ready for next update.
My problem is that the if statement shouldn't be necessary because there is no way for an object to add itself to the same space multiple times in the same update (and the vector gets cleared after every update).

But if I take out that if statement; every few updates every object in the level collides with itself (which is rather annoying).

I have single stepped through my code and I'm certain that no object ever actually adds itself to a single grid space more than once in an update. Does anyone have any ideas of what could possibly be causing this problem?
 
Back
Top Bottom