【问题标题】:GameplayKit + SceneKit, GKAgent rotation conversionGameplayKit + SceneKit、GKAgent 旋转转换
【发布时间】:2018-01-31 09:20:26
【问题描述】:

我在 Scenekit(和 ARKit,尽管这对这个问题并不重要)应用程序中使用 GameplayKit 获得了一些乐趣。

具体来说,我一直在使用实体、组件和行为代理,除了一件事之外,它们都运行良好。

我已经设法在组件内使用 GKAgent 在场景中移动,并避开其他对象。这一切似乎都奏效了。但是,我只能让 GKAgent 位置工作,而不是“旋转”属性。

import Foundation
import SceneKit
import GameplayKit

class MoveComponent: GKScnComponent, GKAgentDelegate {

   //this class is abbreviated for perfunctory sakes   

   func agentWillUpdate(_ agent: GKAgent) {
    guard let visualComponent = entity?.component(ofType: self.visualType) as? VisualComponent else {
      return
    }

     //works with just "position", but turns possessed with both rotation and position
    agentSeeking.rotation = convertToFloat3x3(float4x4: visualComponent.parentMostNode.simdTransform)
    agentSeeking.position = visualComponent.parentMostNode.simdPosition
  }

  func agentDidUpdate(_ agent: GKAgent) {
    guard let visualComponent = entity?.component(ofType: self.visualType) as? VisualComponent else {
      return
    }

    //works with just "position", but turns possessed with both rotation and position
    visualComponent.parentMostNode.simdPosition = agentSeeking.position
    visualComponent.parentMostNode.simdTransform = convertToFloat4x4(float3x3: agentSeeking.rotation)
  }

}

感谢这个问题,我写了一些 3x3 矩阵和 4x4 矩阵之间的转换代码: Converting matrix_float3x3 rotation to SceneKit

import Foundation
import GameplayKit

func convertToFloat3x3(float4x4: simd_float4x4) -> simd_float3x3 {
  let column0 = convertToFloat3 ( float4: float4x4.columns.0 )
  let column1 = convertToFloat3 ( float4: float4x4.columns.1 )
  let column2 = convertToFloat3 ( float4: float4x4.columns.2 )

  return simd_float3x3.init(column0, column1, column2)
}

func convertToFloat3(float4: simd_float4) -> simd_float3 {
  return simd_float3.init(float4.x, float4.y, float4.z)
}

func convertToFloat4x4(float3x3: simd_float3x3) -> simd_float4x4 {
  let column0 = convertToFloat4 ( float3: float3x3.columns.0 )
  let column1 = convertToFloat4 ( float3: float3x3.columns.1 )
  let column2 = convertToFloat4 ( float3: float3x3.columns.2 )
  let identity3 = simd_float4.init(x: 0, y: 0, z: 0, w: 1)

  return simd_float4x4.init(column0, column1, column2, identity3)
}

func convertToFloat4(float3: simd_float3) -> simd_float4 {
  return simd_float4.init(float3.x, float3.y, float3.z, 0)
}

我对这一切有点陌生,而且我不是线性代数大师,所以我不能 100% 确定我的矩阵转换函数是否完全按照它们应该做的。

当我只使用代理的“位置”属性时,一切都很好,但是当我添加从代理到节点的旋转/变换时,一切都开始表现得着魔了。

任何想法/指针/帮助我做错了什么?

【问题讨论】:

    标签: swift scenekit gameplay-kit


    【解决方案1】:

    我想通了。矩阵乘法是让它工作的关键。任何有 3D 游戏开发经验的人都可能告诉我,无需任何脑力劳动:)

    func agentWillUpdate(_ agent: GKAgent) {
        guard let visualComponent = entity?.component(ofType: self.visualType) as? VisualComponent else {
          return
        }
    
        agentSeeking.position = visualComponent.parentMostNode.simdPosition
    
        let rotation = visualComponent.parentMostNode.rotation
        let rotationMatrix = SCNMatrix4MakeRotation(rotation.w, rotation.x, rotation.y, rotation.z)
        let float4x4 = SCNMatrix4ToMat4(rotationMatrix)
    
        agentSeeking.rotation = convertToFloat3x3(float4x4: float4x4)
      }
    
    
    func agentDidUpdate(_ agent: GKAgent) {
        guard let visualComponent = entity?.component(ofType: self.visualType) as? VisualComponent else {
          return
        }
    
    //    visualComponent.parentMostNode.simdPosition = agentSeeking.position
        let rotation3x3 = agentSeeking.rotation
        let rotation4x4 = convertToFloat4x4(float3x3: rotation3x3)
        let rotationMatrix = SCNMatrix4FromMat4(rotation4x4)
        let position = agentSeeking.position
    
        let translationMatrix = SCNMatrix4MakeTranslation(position.x, position.y, position.z)
        let transformMatrix = SCNMatrix4Mult(rotationMatrix, translationMatrix)
    
        visualComponent.parentMostNode.transform = transformMatrix
      }
    

    我希望有人觉得这很有用。

    【讨论】:

      猜你喜欢
      • 2017-07-26
      • 2017-10-29
      • 2016-07-29
      • 2018-06-05
      • 2018-03-25
      • 2018-12-17
      • 2015-02-14
      • 1970-01-01
      • 2017-07-31
      相关资源
      最近更新 更多