79732655

Date: 2025-08-12 02:49:00
Score: 0.5
Natty:
Report link

This is really just what I ended up doing after testing the answer from Andy Jazz. I've left his answer as the correct answer, but this function does what I needed without the additional need to receive a tap. In my scenario, the distance needs to be computed whether it's a tap, or a thumbstick movement on a Game Controller; I need to know if the player can move in the direction it is facing. This function works well.

    /// Returns the distance to the closest node from `position` in the direction specified by `direction`.  Its worth noting that the distance is
    /// computed along a line parallel with the world floor, 25% of a wall height from the floor.
    /// - Parameters:
    ///   - direction: A vector used to determine the direction being tested.
    ///   - position: The position from which the test if being done.
    /// - Returns: A distance in world coordinates from `position` to the closest node, or `.zero` if there is nothing at all.
    func distanceOfMovement(inDirection direction: SIMD3<Float>, fromPosition position: SIMD3<Float>) -> Float {
        // Set the x slightly off center so that in some scenarios (like there is an ajar doorway) the door is
        // detected instead of the wall on the far side of the next chamber beyond the door.  The Y adjustment
        // is to allow for furniture that we dont want the player to walk into.
        //
        let adjustedPosition = position + SIMD3<Float>(0.1,  ModelConstants.wallHeight * -0.25,  0.0)

        if let scene = self.scene {
            let castHits: [CollisionCastHit] = scene.raycast(
                origin: adjustedPosition,
                direction: direction,
                query: .nearest)
            
            if let hit = castHits.first {
                return hit.distance
            }
        }
        
        return .zero
    }

The primary downside of this mechanism is that, unlike SceneKit, if I want this function to work, I have to add a CollisionComponent to each and every wall or furniture item in the model. I'm left wondering about the potential performance impact of that, and also the need to construct sometimes complex CollisionComponents (doorways for example) where in SceneKit, this was all done behind the scenes somehow.

Reasons:
  • Blacklisted phrase (0.5): I need
  • RegEx Blacklisted phrase (1): I want
  • Long answer (-1):
  • Has code block (-0.5):
  • Self-answer (0.5):
Posted by: PKCLsoft