I'm making a little piece of game to learn, so I'm kinda starting everything from scratch just to experience the difficulties. It is very simple by now, but I'm noticing a quick degrading in performance as soon as I start adding more elements to the system.
I have
- a World object which defines world boundaries and a vector of box objects
- a Box object that is the main element of the system
- a PhysicSystem object that handles the boxes movement and collision
- a Renderer class with static functions to draw the world and the boxes.
Here's the draw function:
void drawFrame() {
/* ... projection matrix settings .. */
physys.calcBoxPositions(); //calculates box movement and detect collision
Renderer::drawWorld(w); //draws the world boundaries
Renderer::drawWorldBoxes(&w); //draws each box
glutSwapBuffers();
}
The box movement is just a constant increment on the y axis. The collision detection each box to all the others and the world, if it detects a collision, it stops incrementing the box position.
void PhysicSystem::calcBoxPositions() {
std::vector<Box>::iterator it1;
std::vector<Box> &boxList = this->world.getBoxList();
for (it1 = boxList.begin(); it1 != boxList.end(); it1++) {
Box& b1 = *it1;
b1.setXY(b1.getX(), b1.getY() + this->gravity); //gravity is 0.001f
bool collided = false;
std::vector<Box>::iterator it2;
for (it2 = boxList.begin(); it2 != boxList.end(); it2++) {
Box& b2 = *it2;
if (&b1 != &b2) { // not the same box
collided = checkCollision(b1,b2);
if (collided) {
b1.stop();
collided = false;
}
}
if (b1.bottom() <= this->world.getY())
collided = true;
if (b1.top() >= this->world.getY() + this->world.getHeight())
collided = true;
if (collided) {
b1.stop();
collided = false;
}
}
}
I start the thing with 4 boxes and it runs smoothly. When I add like 5 more boxes the frame rate drops heavily. I know that the collision checking has square complexity, but I don't think that it alone is the problem with so few boxes.
Any council/help/suggestion/critic will be appreciated,
Thanks