ios - Gamekit's GKOctree not finding elements
Asked Answered
H

1

9

I'm trying to use GKOctree for efficient retrieval of object in 3D space. However the following code doesn't seem to work as expected:

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
Hako answered 15/6, 2017 at 0:38 Comment(1)
There's a similar question on the Apple Developer forums here and here, but sadly no answers.Gadmon
E
4

I did some testing and it seems there is a bug within GKOctree that inverts the sign of the z axis.

If you change the code like this, it works:

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
Egress answered 4/3, 2021 at 12:43 Comment(3)
This is very strange behaviour; and it only seems to apply when creating the GKOctree object, and not when creating a GKBox to search within...Gadmon
This doesn't work consistentlyLankton
Thats interesting- can you share your findings, probably creating a new question?Egress

© 2022 - 2024 — McMap. All rights reserved.