我有一系列實體(Manager.Scene.Entities
),技術上是圈子。
和我有一個方法,返回實體數組與當前實體相交:快速檢查實體是否相交?
var Intersected = Manager.Scene.Entities.filter(function (another) {
//check cases that not initiate intersection to prevent extra calculations
if (self.radius < another.radius
|| another.id == self.id
|| self.className == another.className)
return false;
//check circles intersections
var dx = self.x - another.x;
var dy = self.y - another.y;
dx = dx * dx + dy * dy;
dy = self.radius + another.radius;
return dx < dy * dy;
});
我型材,並注意到,該方法需要的執行時間,28%(看照片)。
是否有可能以某種方式進行優化?
PS。我修改了交點檢查,現在它找到了附近的實體,並且檢查了交點。它需要ex的21%。時間,而不是28%。
var Nearest = Manager.Scene.Entities.filter(function (another) {
return self.x * 2 > another.x || self.x/2 < another.x || self.y * 2 > another.y || self.y/2 < another.y;
});
var Intersected = Nearest.filter(function (another) {
if (self.radius < another.radius || another.id == self.id || self.className == another.className)
return false;
var dx = self.x - another.x;
var dy = self.y - another.y;
dx = dx * dx + dy * dy;
dy = self.radius + another.radius;
return dx < dy * dy;
});
我假定你計算所有實體交叉:使用[空間劃分策略(https://en.wikipedia.org/wiki/Space_partitioning),以避免'爲O(n^2)'行爲 – BeyelerStudios
@ BeyelerStudios有趣的聽到。我既不做gamedev也不做圖形/物理學,我推斷複雜形狀更有益。 – zerkms
@zerkms比*(邊界球)(https://en.wikipedia.org/wiki/Bounding_sphere)計算*某些東西的邊界框(最小值,最大值)更便宜,但邊界的相交測試球體只是距離的比較,而邊界框涉及多重比較 – BeyelerStudios