1

我想知道哪个节点被击中,但我的方法仅适用于具有 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];

问题出在哪里?

4

3 回答 3

2

英雄节点没有附加几何图形,但它具有确实具有几何图形的子节点。因此,英雄节点不会出现在命中测试结果中。

检查 hero 节点是否是 hitNode 的父节点有效吗?

于 2014-07-16T14:37:00.187 回答
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];
于 2014-07-17T05:09:01.657 回答
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;
}
于 2014-07-17T12:47:03.130 回答