Solved
Optimization
Posted on 1998-11-19
Hello
As some of you know, I am creating an Asteroids clone. I am having some trouble getting this code to run fast however.
// Move current shots:
for (j = 0; j < 1000; j++)
{ if (m_Players[i].m_Shots[j].IsDead () == true)
{ continue; }
COLORREF color = dc->GetPixel ((int) m_Players[i].m_Shots[j].GetX (), (int) m_Players[i].m_Shots[j].GetY ());
for (k = 0; k < m_NumRocks; k++) // Asteroid <-> shot collision
{ if (m_Rocks[k].IsDead () == true)
{ continue; }
if (color > 0)
{ if (m_Rocks[k].Distance (m_Players[i].m_Shots[j].GetX (), m_Players[i].m_Shots[j].GetY ()) < m_Rocks[k].GetSize ())
{ m_Rocks[k].Explode (dc);
m_Rocks[k].Spawn (dc, m_Rocks, m_NumRocks);
m_Players[i].m_Shots[j].Explode (dc, m_Players[i].m_Particles, 1000, 10);
}
}
}
for (k = 0; k < 2; k++) // Player <-> shot collision
{ if (k == i)
{ continue; }
if (m_Players[k].IsDead () == true)
{ continue; }
if (m_Players[i].m_Shots[j].Distance (m_Players[k].GetX (), m_Players[k].GetY ()) < 20)
{ m_Players[k].Hit (dc, 3);
m_Players[i].m_Shots[j].Explode (dc, m_Players[i].m_Particles, 1000, 50);
}
}
}
You see, occasionally as the result of a power up, a lot of shots are generated. These shots must be checked each frame to make sure they haven't collided with anything. The code above is what is doing the checking.
There are a few problems with the code, however. The main one is the fact that it is so slow at times. Note that the asteroids are polygons hence the "GetPixel" to make sure the shot is indeed within the polygon.