Reputation: 23
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?
Upvotes: 1
Views: 1419
Reputation: 23
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;
}
Upvotes: 1
Reputation: 45
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];
Upvotes: 1
Reputation: 13462
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?
Upvotes: 2