ios - Gamekit 的 GKOctree 找不到元素

Gui*_*uig 8 gamekit ios octree swift

我正在尝试使用GKOctree在 3D 空间中有效检索对象。但是,以下代码似乎没有按预期工作:

import GameplayKit

let tree = GKOctree(boundingBox: GKBox(
  boxMin: vector_float3(x: -10, y: -10, z: -10),
  boxMax: vector_float3(x: 10, y: 10, z: 10)
), minimumCellSize: 0.1)

tree.add(NSObject(), at: vector_float3(x: 0, y: 0, z: 0))
tree.elements(at: vector_float3(x: 0, y: 0, z: 0)).count // 1, fine

tree.elements(in: GKBox(
  boxMin: vector_float3(x: -1, y: -1, z: -1),
  boxMax: vector_float3(x: 1, y: 1, z: 1)
)).count // 0, ??

tree.elements(in: GKBox(
  boxMin: vector_float3(x: 1, y: 1, z: 1),
  boxMax: vector_float3(x: -1, y: -1, z: -1)
)).count // 0, well I tried
Run Code Online (Sandbox Code Playgroud)

de.*_*de. 4

我做了一些测试,似乎存在一个错误,GKOctree它会反转 z 轴的符号。

如果你像这样改变代码,它会起作用:

import GameplayKit

let tree = GKOctree(boundingBox: GKBox(
    boxMin: vector_float3(x: -10, y: -10, z: 10),  // <---- check this out
    boxMax: vector_float3(x: 10, y: 10, z: -10)    // <---- check this out
), minimumCellSize: 0.1)

tree.add(NSObject(), at: vector_float3(x: 0, y: 0, z: 0))
tree.elements(at: vector_float3(x: 0, y: 0, z: 0)).count // 1, fine

tree.elements(in: GKBox(
    boxMin: vector_float3(x: -1, y: -1, z: -1),
    boxMax: vector_float3(x: 1, y: 1, z: 1)
)).count // 1, works

tree.elements(in: GKBox(
    boxMin: vector_float3(x: 1, y: 1, z: 1),
    boxMax: vector_float3(x: -1, y: -1, z: -1)
)).count // 1, works indeed
Run Code Online (Sandbox Code Playgroud)

  • 这是非常奇怪的行为;它似乎只适用于创建“GKOctree”对象,而不是创建“GKBox”来搜索时...... (2认同)