我想知道哪个节点被点击,但我的方法仅适用于像SCNBox和SCNFloor这样的几何节点,但不适用于DAE模型:
- (void) handleTap:(UIGestureRecognizer*)gestureRecognize {
// retrieve the SCNView
SCNView *scnView = (SCNView *)self.view;
// check what nodes are tapped
CGPoint p = [gestureRecognize locationInView:scnView];
NSArray *hitResults = [scnView hitTest:p options:nil];
// check that we clicked on at least one object
if([hitResults count] > 0) {
SCNNode *hitNode = ((SCNHitTestResult*)[hitResults objectAtIndex:0]).node;
if(hitNode == boxNode) {
NSLog(@"box hit"); //works
}
if(hitNode == floorNode) {
NSLog(@"floor hit"); //works
}
if(hitNode == heroNode) {
NSLog(@"heroNode from .dae hit"); //doesn't work
}
}
}
这就是我制作.dae节点(heroNode)的方法:
SCNScene *heroScene = [SCNScene sceneNamed:@"hero" inDirectory:nil options:nil];
heroNode = [heroScene.rootNode childNodeWithName:@"root" recursively:YES];
[scene.rootNode addChildNode:heroNode];
问题出在哪里?
答案 0 :(得分:2)
英雄节点没有附加到它的几何体,但它具有具有几何体的子节点。因此,英雄节点不会出现在命中测试结果中。
检查英雄节点是否是你的hitNode的父节点吗?
答案 1 :(得分:1)
我遵循了@mnuages的建议并提出了这个问题,我正在使用Apples WWDC 2014中的boss.dae文件。SceneKit中的新功能
- (void) handleTap:(UIGestureRecognizer*)gestureRecognize {
// retrieve the SCNView
SCNView *scnView = (SCNView *)self.view;
// check what nodes are tapped
CGPoint p = [gestureRecognize locationInView:scnView];
NSArray *hitResults = [scnView hitTest:p options:nil];
// check that we clicked on at least one object
if([hitResults count] > 0){
// retrieved the first clicked object
SCNHitTestResult *result = [hitResults objectAtIndex:0];
//search in the node tree with a specified name.
SCNNode *tempNode = [self.monsterCharacter childNodeWithName:@"Box03" recursively:YES];
// Search for the node named "name"
if (tempNode == result.node.parentNode) {
NSLog(@"FOUND IT");
}
}
}
在viewDidLoad中我创建了这样的字符:
//add Monster to scene
SCNNode *heroNodeRoot = [SMLGameView loadNodeWithName:nil fromSceneNamed:@"art.scnassets/characters/boss/boss.dae"];
self.monsterCharacter = [[SMLMonster alloc] initWithNode:heroNodeRoot withSkeleton:@"skeleton"];
self.monsterCharacter.position = SCNVector3Make(0, 0, 0);
[scene.rootNode addChildNode:self.monsterCharacter];
答案 2 :(得分:1)
if([daeNode childNodeWithName:hitTestResultNode.name recursively:YES])
{
NSLog(@"hit!");
}
daeNode - 来自.dae的节点
hitTestResultNode - 来自SCNHitTestResult的节点:
CGPoint p = [gestureRecognize locationInView:scnView];
NSArray *hitResults = [scnView hitTest:p options:nil];
// check that we clicked on at least one object
if([hitResults count] > 0)
{
SCNHitTestResult *hitResult = (SCNHitTestResult*)[hitResults objectAtIndex:0];
SCNNode *hitTestResultNode = hitResult.node;
}