- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我正在尝试将触摸从 SpriteKit 转换为 SceneKit,但遇到了一些困难。我的目的是移动一个 3D“怪物”,使其看起来就像在同一位置(虽然在后面)是一个 2D Crystal 。 2D Crystal 被添加到 SpriteKit 提供的 overlaySKScene
并定位。我们的“怪物”是一个全 3D SCNNode
,它位于 SCNScene
中,如下所示。
我已经尝试了多种方法,其中最接近的是我在下面的 makeMonsterEatCrystalAtLocation
方法中,尽管我知道这是错误的,因为它只在 SpriteKit 世界中起作用 -它不会从 SpriteKit 转换为 SceneKit。我还查看了 SCNSceneRenderer
协议(protocol)中的 unprojectPoint
,但该方法仅适用于 SceneKit 本身。任何帮助将不胜感激!我不偏爱 Objective-C 或 Swift,即使下面是 Obj-C。
- (void)setupSceneKit {
_sceneView = [[SCNView alloc] initWithFrame:[[UIScreen mainScreen] bounds] options:@{@"SCNPreferredRenderingAPIKey": @(SCNRenderingAPIOpenGLES2)}];
[self.view insertSubview:_sceneView aboveSubview:backgroundView];
_sceneView.scene = [SCNScene scene];
_sceneView.allowsCameraControl = YES;
_sceneView.autoenablesDefaultLighting = NO;
_sceneView.backgroundColor = [UIColor clearColor];
camera = [SCNCamera camera];
[camera setXFov:20];
[camera setYFov:20];
camera.zFar = 10000.0f;
cameraNode = [SCNNode node];
cameraNode.camera = camera;
cameraNode.position = SCNVector3Make(0, 3, 700);
[_sceneView.scene.rootNode addChildNode:cameraNode];
}
- (void)createMonster {
_monsterNode = [SCNNode node];
[self.sceneView.scene.rootNode addChildNode:_monsterNode];
[SCNTransaction begin];
[SCNTransaction setAnimationDuration:0.5f];
_monsterNode.position = SCNVector3Zero;
[SCNTransaction commit];
}
- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
CGPoint crystalLocation = [[touches anyObject] locationInNode:self.sceneView.overlaySKScene];
SKNode *touchedCrystalNode = [self.sceneView.overlaySKScene nodeAtPoint:crystalLocation];
// determine that this is the SKNode touchedCrystalNode we're looking for...
[self makeMonsterEatCrystalAtLocation:crystalLocation];
}
- (void)makeMonsterEatCrystalAtLocation:(CGPoint)location {
// Attempt to move the "monster" (SCNNode in SceneKit) to appear as if its in the same
// location as the "crystal" (which is presented in the SpriteKit overlay: self.sceneView.overlaySKScene) and eventually
// play an animation of the monster eating the crystal.
CGPoint scnPoint = [self.sceneView.overlaySKScene convertPointToView:location];
[SCNTransaction begin];
[SCNTransaction setAnimationDuration:0.5f];
self.monsterNode.position = SCNVector3Make(scnPoint.x, scnPoint.y, 0);
[SCNTransaction commit];
// ...
}
最佳答案
一种方法是将您的怪物放在 SK3DNode
中。然后,您可以在 SpriteKit 中度过大部分时间,而无需在坐标系之间进行转换。
投影时,你必须处理几个不同的坐标系:SpriteKit 场景(原点在左下角)、SceneKit View 的屏幕坐标(原点在左上角或左下角,取决于操作系统)、 View 的相机局部坐标系统,以及 SceneKit 场景的世界坐标系。
让我们从 SceneKit 方面开始。您的相机有自己的 3D 坐标系,表示 view frustum 的内容.屏幕上的单个点对应于平截头体内从近平面到远平面的一条线段;相机空间中的 Z 轴对应于点在近平面和远平面之间的相对位置。您的 SCNView
,由于遵守 SCNSceneRenderer
协议(protocol),可以在屏幕坐标和 3D 世界空间之间进行转换。
要移动您的怪物,您将使用 projectPoint
获取其当前相机 x/y/z 坐标,找出您要移动到的屏幕位置,然后使用 unprojectPoint
来计算该屏幕位置的世界位置。像这样:
let screenDestinationX = 100.0
let screenDestinationY = 200.0
let monsterCoordinates = sceneView.projectPoint(MonsterNode.position)
let monsterDestinationCoordinates = SCNVector3D(x: screenDestinationX,
y: screenDestinationY,
z: monsterCoordinates.z)
let monsterDestinationPosition = sceneView.unprojectPoint(monsterDestinationCoordinates)
通过为怪物的开始和结束保持相同的相机 Z 坐标,您将使怪物与相机保持相同的距离。
现在是 SpriteKit 方面。如果您的 sceneView.overlaySKScene
将其 scaleMode
设置为 SKSceneScaleMode.ResizeFill
,则您的 SKScene
的大小将与你的屏幕。当然,Y 轴的方向可能不同。所以向你的 Crystal 询问它的 SpriteKit 坐标,必要时翻转 Y(高度减去 Y,而不仅仅是 Y),并且你有目的地 X 和 Y,你可以传递给 SceneKit 进行投影。
我有一个示例项目,它显示了 SceneKit 和覆盖 SpriteKit 坐标之间的转换,位于 https://github.com/halmueller/ImmersiveInterfaces/tree/master/Tracking%20Overlay。 .它并不完全符合您的要求,但它确实显示了交互。
最后,下面是一个显示世界坐标和相机坐标之间转换的 Playground:
import SceneKit
import SpriteKit
import XCPlayground
let sceneView = SCNView(frame: CGRect(x: 0, y: 0, width: 800, height: 600))
XCPlaygroundPage.currentPage.liveView = sceneView
var scene = SCNScene()
sceneView.scene = scene
sceneView.backgroundColor = SKColor.greenColor()
sceneView.debugOptions = .ShowWireframe
// default lighting
sceneView.autoenablesDefaultLighting = true
// a camera
var cameraNode = SCNNode()
cameraNode.camera = SCNCamera()
print (cameraNode.camera?.zFar, cameraNode.camera?.zNear)
cameraNode.camera?.automaticallyAdjustsZRange = false
cameraNode.position = SCNVector3(x: 5, y: 5, z: 50)
scene.rootNode.addChildNode(cameraNode)
let center = SCNNode()
center.position = SCNVector3(x:0, y:0, z:0)
scene.rootNode.addChildNode(center)
let centerConstraint = SCNLookAtConstraint(target: center)
cameraNode.constraints = [centerConstraint]
let sphere = SCNSphere(radius: 2)
sphere.geodesic = true
let sphereNode1 = SCNNode(geometry: sphere)
sphereNode1.position = SCNVector3(x:-8, y:5, z:10)
scene.rootNode.addChildNode(sphereNode1)
let sphereNode2 = SCNNode(geometry: sphere)
sphereNode2.position = SCNVector3(x:8, y:-5, z:-30)
scene.rootNode.addChildNode(sphereNode2)
let sphere1Coordinates = sceneView.projectPoint(sphereNode1.position)
print ("sphere1 position", sphereNode1.position, "screen coordinates",sphere1Coordinates)
let sphere2Coordinates = sceneView.projectPoint(sphereNode2.position)
print ("sphere2 position", sphereNode2.position, "screen coordinates",sphere2Coordinates)
let sphere2NearCoordinates = SCNVector3(x: sphere2Coordinates.x, y: sphere2Coordinates.y, z: sphere1Coordinates.z)
let sphere2FarPosition = sphereNode2.position
let sphere2NearPosition = sceneView.unprojectPoint(sphere2NearCoordinates)
let sphere1FarCoordinates = SCNVector3(x: sphere1Coordinates.x, y: sphere1Coordinates.y, z: sphere2Coordinates.z)
let sphere1NearPosition = sphereNode1.position
let sphere1FarPosition = sceneView.unprojectPoint(sphere1FarCoordinates)
let sphere1LowerCoordinates = SCNVector3(x: sphere1Coordinates.x, y: sphere1Coordinates.y - 100, z: sphere2Coordinates.z)
let sphere1LowerPosition = sceneView.unprojectPoint(sphere1LowerCoordinates)
let forwardAction = SCNAction.moveTo(sphere2NearPosition, duration: 3)
let backAction = SCNAction.moveTo(sphere2FarPosition, duration: 3)
let shiftAction = SCNAction.moveTo(sphere1LowerPosition, duration: 3)
let forwardBackShift = SCNAction.sequence([forwardAction, backAction, forwardAction, shiftAction])
sphereNode2.runAction(forwardBackShift) {
print ("sphere1 position", sphereNode1.position, "screen coordinates",sceneView.projectPoint(sphereNode1.position))
print ("sphere2 position", sphereNode2.position, "screen coordinates",sceneView.projectPoint(sphereNode2.position))
}
关于objective-c - 将 SpriteKit SKScene overlaySKScene 的 2D 点映射到 SceneKit SCNScene 中的 3D 点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34443727/
我遵循了一本名为“Sitepoint Full Stack Javascript with MEAN”的书中的教程,我刚刚完成了第 6 章,应该已经创建了一个带有“数据库”的“服务器”。数据库只不过是
在 Jquery 中,我创建两个数组,一个嵌入另一个数组,就像这样...... arrayOne = [{name:'a',value:1}, {name:'b',value:2}] var arra
这个问题在这里已经有了答案: What is the explanation for these bizarre JavaScript behaviours mentioned in the 'Wa
我被放在别人的代码上,有一个类用作其他组件的基础。当我尝试 ng serve --aot(或 build --prod)时,我得到以下信息。 @Component({ ...,
我正在测试一些代码,并使用数据创建了一个 json 文件。 问题是我在警报中收到“[object Object],[object Object]”。没有数据。 我做错了什么? 这是代码:
我想打印 [object Object],[object Object] 以明智地 "[[{ 'x': '1', 'y': '0' }, { 'x': '2', 'y': '1' }]]"; 在 ja
我有一个功能 View ,我正在尝试以特殊格式的方式输出。但我无法让列表功能正常工作。 我得到的唯一返回是[object Object][object Object] [object Object]
在使用优秀的 Sim.js 和 Three.js 库处理 WebGL 项目时,我偶然发现了下一个问题: 一路走来,它使用了 THREE.Ray 的下一个构造函数: var ray = new THRE
我正在使用 Material UI 进行多重选择。这是我的代码。 {listStates.map(col => (
我的代码使用ajax: $("#keyword").keyup(function() { var keyword = $("#keyword").val(); if (keyword.
我遇到了下一个错误,无法理解如何解决它。 Can't resolve all parameters for AuthenticationService: ([object Object], ?, [o
我正在尝试创建一个显示动态复选框的表单,至少应选中其中一个才能继续。我还需要获取一组选中的复选框。 这是组件的代码: import { Component, OnInit } from '@angul
我正在开发 NodeJs 应用程序,它是博客应用程序。我使用了快速验证器,我尝试在 UI 端使用快速闪存消息将帖子保存在数据库中之前使用闪存消息验证数据,我成功地将数据保存在数据库中,但在提交表单后消
我知道有些人问了同样的问题并得到了解答。我已经查看了所有这些,但仍然无法解决我的问题。我有一个 jquery snipet,它将值发送到处理程序,处理程序处理来自 JS 的值并将数据作为 JSON 数
我继承了一个非常草率的项目,我的任务是解释为什么它不好。我注意到他们在整个代码中都进行了这样的比较 (IQueryable).FirstOrDefault(x => x.Facility == fac
我只是在删除数组中的对象时偶然发现了这一点。 代码如下: friends = []; friends.push( { a: 'Nexus', b: 'Muffi
这两个代码片段有什么区别: object = nil; [object release] 对比 [object release]; object = nil; 哪个是最佳实践? 最佳答案 object
我应该为其他人将从中继承的第一个父对象传递哪个参数,哪个参数更有效 Object.create(Object.prototype) Object.create(Object) Object.creat
我在不同的对象上安排不同的选择器 [self performSelector:@selector(doSmth) withObject:objectA afterDelay:1]; [self per
NSLog(@"%p", &object); 和 NSLog(@"%p", object); 有什么区别? 两者似乎都打印出一个内存地址,但我不确定哪个是对象的实际内存地址。 最佳答案 这就是我喜欢的
我是一名优秀的程序员,十分优秀!