Java 有效的碰撞调整
我已经在Android上编写了一个增强现实框架。该框架的一个特点是内置碰撞检测和调整功能;也就是说,如果检测到检测,框架将移动对象,使其不再碰撞。它的工作如预期,但我想知道是否有一个更有效的方法来处理它。或者行业内是否有处理这些情况的通用方法 该代码是在Android View对象的onDraw()中调用的,该对象被频繁调用 以下是碰撞检测和调整代码:Java 有效的碰撞调整,java,android,collision-detection,android-canvas,Java,Android,Collision Detection,Android Canvas,我已经在Android上编写了一个增强现实框架。该框架的一个特点是内置碰撞检测和调整功能;也就是说,如果检测到检测,框架将移动对象,使其不再碰撞。它的工作如预期,但我想知道是否有一个更有效的方法来处理它。或者行业内是否有处理这些情况的通用方法 该代码是在Android View对象的onDraw()中调用的,该对象被频繁调用 以下是碰撞检测和调整代码: private static final Set<Marker> updated = new HashSet<Marker&g
private static final Set<Marker> updated = new HashSet<Marker>();
private static final int COLLISION_ADJUSTMENT = 100;
private static void adjustForCollisions(Canvas canvas, List<Marker> collection) {
updated.clear();
// Update the AR markers for collisions
for (Marker marker1 : collection) {
// If marker1 has already been updated or is not in view then skip it
if (updated.contains(marker1) || !marker1.isInView())
continue;
int collisions = 1;
for (Marker marker2 : collection) {
// If marker2 has already been updated or is not in view or is the same as marker 1 then skip it
if (updated.contains(marker2) || !marker2.isInView() || marker1.equals(marker2))
continue;
// Detect a collision
if (marker1.isMarkerOnMarker(marker2)) {
// Move the marker "up" the screen by COLLISION_ADJUSTMENT pixels
marker2.getLocation().get(locationArray);
float y = locationArray[1];
float h = collisions * COLLISION_ADJUSTMENT;
locationArray[1] = y + h;
marker2.getLocation().set(locationArray);
marker2.update(canvas, 0, 0);
collisions++;
// mark marker2 as updated
updated.add(marker2);
}
}
// mark marker2 as updated
updated.add(marker1);
}
}
private static final Set updated=new HashSet();
专用静态最终整数碰撞调整=100;
私有静态void adjustForCollisions(画布、列表集合){
更新的.clear();
//更新碰撞的AR标记
用于(标记1:集合){
//如果marker1已更新或不在视图中,则跳过它
if(updated.contains(marker1)| |!marker1.isInView())
继续;
int=1;
用于(标记2:集合){
//如果标记2已更新或不在视图中,或与标记1相同,则跳过它
if(updated.contains(marker2)| |!marker2.isInView()| | marker1.equals(marker2))
继续;
//检测碰撞
如果(标记1.ismarkeron标记2)){
//通过调整像素将标记“向上”移动到屏幕上
marker2.getLocation().get(locationArray);
浮动y=位置数组[1];
浮动h=碰撞*碰撞调整;
位置阵列[1]=y+h;
marker2.getLocation().set(locationArray);
marker2.update(canvas,0,0);
碰撞++;
//将marker2标记为已更新
更新。添加(marker2);
}
}
//将marker2标记为已更新
更新。添加(标记1);
}
}
在github上。一个简单的改进方法是不将
用于(对象o:collection)
如果有很多对象,这可能会对性能产生巨大影响
for(int i = 0; i < collection.length();i++){
for(int j = i+1; j < collection.length(); j++){
//check collision collection[i] vs collection[j]
}
}
您可以从步骤到中看到,[i]从未针对自身或任何以前的索引进行过检查,因此,在我们进一步迭代集合时,所需的检查数量大大减少
[0]vs[1]与[1]vs[0]相同,因此我们基本上不需要对同一件事进行两次检查。这可能更适合CodeReview Stack Exchange站点。Tom G我不确定我是否同意。我不是在寻找代码审查。这似乎是一个常见的问题,我想知道是否有一个通用的解决方案/方法。不会(int j=I+1;j
outer i=0
inner (check [i] vs [j])
j=1 [0] vs [1]
j=2 [0] vs [2]
j=3 [0] vs [3]
.....
j=N [0] vs [N] (N is last index)
end inner
we've now checked [0] against all other objects and the inner loop finishes
outer loop incrments i
i++
i=1
outer i=1
inner (check [i] vs [j])
j=2 [1] vs [2]
j=2 [1] vs [3]
j=3 [1] vs [4]
.....
j=N [1] vs [N] (N is last index)
end inner
....
i++
i=N-1;
outer i=N-1
inner (check [i] vs [j])
j=N [N-1] vs [N]
end inner
end outer