如前所述其他答案(s),您可以使用四叉樹結構來確定碰撞更快。
我會推薦GEOS開源的C++庫,它有一個很好的四叉樹實現。 Here are the docs for their quadtree class。
所以,你的僞代碼是這樣的:
Quadtree quadtree;
// Create and populate the quadtree.
// Change it whenever the balls move.
// Here's the intersection loop:
for (int i=0; i<ballCount; ++i) {
Envelope envelope = ...; // Get the bounds (envelope) of ball i
std::vector<void*> possiblyIntersectingBalls;
quadtree.query(envelope, possiblyIntersectingBalls);
// Now loop over the members of possiblyIntersectingBalls to check
// if they really intersect, since quadtree only checks bounding
// box intersection.
}
來源
2012-07-06 22:06:26
csd
我不認爲你可以使它更容易* *比目前你在做什麼,但是你可以把它很多*更快*。你在問什麼? – 2012-07-06 22:04:50