简体   繁体   中英

SceneKit – HitTest with DAE models

I want to know which Node is hit, but my method works only for nodes with geometry like SCNBox and SCNFloor but doesn't work with DAE models:

- (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
        }
    } 
}

and this is how I make a .dae Node (heroNode):

SCNScene *heroScene = [SCNScene sceneNamed:@"hero" inDirectory:nil options:nil];
heroNode = [heroScene.rootNode childNodeWithName:@"root" recursively:YES];
[scene.rootNode addChildNode:heroNode];

Where is the problem?

the hero node does not have a geometry attached to it, but it has child nodes that do have a geometry. As a result the hero node won't appear in the hit test results.

Does checking if the hero node is a parent of your hitNode work?

I followed @mnuages advice and came out with this, I'm using the boss.dae file from Apples WWDC 2014 What is new in 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");
    }
}

}

In viewDidLoad I create the character like this:

//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];
    if([daeNode childNodeWithName:hitTestResultNode.name recursively:YES])
    {
        NSLog(@"hit!");
    }

daeNode - Node from .dae

hitTestResultNode - Node from 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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM