我有一系列实体(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