Collision detection algorithms
Assuming the collision detection runs for all objects on the map (because it does; neither programmer has much of an idea how to optimize that).
Which of these solutions is more effective? Is there a better way to do it? Code:
bool check_collision(int x1, int y1, int x2, int y2, int r1, int r2) { Code:
bool check_collision(int x1, int y1, int x2, int y2, int r1, int r2){ |
Well, eliminating the square root is a good idea, but you're doing it rather inefficiently. This would be a lot easier. Note that there's no need for floating point math (if your coordinates remain under 32768).
Code:
bool check_collision (int x1, int y1, int x2, int y2, int r1, int r2) Sure, there are some small gains to be made this way, but the more important thing is to reduce the amount of checks done in the first place. You said to assume that the collision check always runs, but that's a bad premise, especially if that means you're checking every single object against every other object. That has a big-O of nē, which is bad. For 1000 objects, the amount of checks would be over half a million, which is ridiculous :no: Here's some a tip for an method to get that number down, assuming a 2D map (though extendable to 3D):
|
The current time is 10:43 PM (GMT) |
Powered by vBulletin® Version 3.7.1
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.