- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我在 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% 确定我的矩阵转换函数是否完全按照它们应该做的去做。
当我只使用代理的“位置”属性时,一切都很好,但是当我添加从代理到节点的旋转/变换时,一切都开始表现得像被附身了。
对我做错的事情有什么想法/建议/帮助吗?
最佳答案
我想通了。矩阵乘法是实现此功能的关键。任何有 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
}
我希望有人觉得这很有用。
关于swift - GameplayKit + SceneKit, GKAgent 旋转转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45828631/
我通过向其行为添加目标 toFollow: GKPath 来让代理遵循 GKPath。一切正常,但是代理在到达路径上的目标点时继续以类似 8 的形状移动。如何防止代理在到达 GKPath 上的目标点时
我目前正在使用 spritekit 创建一个自上而下的僵尸游戏,我需要用僵尸实现寻路。我在网上找不到很多教程,我找到的少数教程只描述了一个播放器,而且已经过时了。我在整个类(class)中遇到障碍,以
有没有人找到 GameplayKit 用于寻路的算法类型的引用资料?我查看了 Apple 的文档,但没有找到任何内容。 最终我很好奇它的性能与在没有 GameplayKit 的情况下实现的 A* 的对
我正在使用 Spritekit 深入研究 GameplayKit,根据我收集的信息,您将 GKEntity 子类化,然后开始向该实体添加 GKComponents。实体将或多或少只是一袋填充某些功能的
我有一个 GKAgent2D 来控制 SKNode 敌人向目的地移动。 当我暂停代理正在移动的 SKScene 时,敌人节点似乎继续移动而没有显示这种移动。所有可见的 Action 都停止(也就是敌人
我正在学习使用 swift 和 Gameplaykit 生成随机数。在下面的代码中,数字是在我初始化 randomDist 时生成的,它只是在我调用 nextInt 时从已经生成的数字中提供样本,还是
我正在试验 Apple 的 GameplayKit、SpriteKit 和 Swift。我正在尝试通过只能从一侧通过的瓷砖进行寻路。 下图显示了两个问题: 左边是一个简单的解决方案,我知道如何从绿色位
暂停 GKAgent 的最佳方法是什么? 我的游戏在某些级别使用了一些代理,我需要在我的游戏暂停/gameOver 时暂停它们。 我不会在我的游戏中暂停整个 SKScene,而是暂停一个 worldN
在我的游戏中,我有一个符合 NSCoding 的 Combat 类,它还使用 GameplayKit 的 GKGridGraph 和 GKGridGraphNode 对象,但它们不符合 NSCodin
情况 我正在使用 GKObstacleGraph 在以下 map 上进行寻路: 橙色区域是障碍物 (GKPolygonObstacle),而紫色点和线是自定义放置的节点 (GKGraphNode2D)
是否有显示 GKPath 的技巧? 例如我的路径: class ScenarioPath: GKPath { //SET // | | | | | | // --
我正在学习 SceneKit 团队在 SceneKit: What's New 中演示的示例代码.我发现一个文件的扩展名为 .gks,它似乎是 GameplayKit 的资源。 这是加载: #if E
我想要一个 Sprite 跟随玩家,我试图通过 GameplayKit 的 Pathfinding 来实现这一点。这是可行的,但是有问题。我使用以下代码获取从敌人 Sprite 到玩家 Sprite
我觉得这可能是一个错误,任何人都可以重现或看到我做事方式的一些错误。 我正在尝试将 GKPolygonObstacle 添加到 iOS 或 macOS Playground 中的 GKMeshGrap
我在 Scenekit(和 ARKit,尽管这对这个问题并不重要)应用程序中使用 GameplayKit 一直很有趣。 具体来说,我一直在为行为使用实体、组件和代理,除了一件事外,它们都运行良好。 我
从这里的 GameplayKit 开始,我正在为一个无尽的奔跑者建模,玩家可以通过点击屏幕在角色之间切换。角色第一次可以近距离攻击(组件),第二次跳跃(组件),第三次射击(组件)。 我想知道什么是最好
类似于我发布的问题 here ,我现在意识到这个问题比我预期的更微不足道,因为这适用于 GameplayKit 的某些元素,但不适用于其他元素.. 我有一个障碍物,一个 SKNode,我试图将其定义为
我有一个 SceneKit 游戏,我正在尝试将其与 GameplayKit 集成。 为了让 GameplayKit 正常工作,我需要通过调用 updateWithDeltaTime(seconds:)
文档 states GameplayKit also works well for 3D games built with the SceneKit framework 但是,似乎没有提及使用Scen
我确信这对您来说是一个简单的问题。 如何用一个 GKState 编写一个具有两个参数的函数? 更新 苹果使用 func willExitWithNextState(_ nextState: GKSta
我是一名优秀的程序员,十分优秀!