首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >GameplayKit + SceneKit,GKAgent旋转转换

GameplayKit + SceneKit,GKAgent旋转转换
EN

Stack Overflow用户
提问于 2017-08-23 08:02:59
回答 1查看 419关注 0票数 4

我在Scenekit (和ARKit,尽管这对这个问题并不重要)应用程序中使用GameplayKit很有趣。

具体地说,我一直在使用实体、组件和代理来处理行为,除了一件事之外,所有这些都运行得很好。

我已经成功地在组件中使用GKAgent在场景中移动,并避开其他对象。这一切似乎都在起作用。但是,我只能让GKAgent位置工作,而不能让"rotation“属性工作。

代码语言:javascript
运行
复制
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

代码语言:javascript
运行
复制
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)
}

我是个新手,而且我也不是一个线性代数专家,所以我不能百分之百确定我的矩阵转换函数是否做了它们应该做的事情。

当我只使用代理的"position“属性时,一切都很好,但是当我添加从代理到节点的旋转/变换时,一切都开始被附身。

对我做错了什么有什么想法/建议/帮助吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-08-27 12:07:09

我想通了。矩阵乘法是实现这一功能的关键。任何有3d游戏开发经验的人可能都会告诉我,没有任何脑力消耗:)

代码语言:javascript
运行
复制
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
  }

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

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45828631

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档