2014-07-16 68 views
1

我想知道哪些節點被擊中,但我的方法僅適用於節點與像SCNBox幾何,SCNFloor ECT但不與.dea模式工作:的HitTest與.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]; 

問題在哪裏?

回答

2

英雄節點沒有連接到它的幾何形狀,但它有那些具有幾何形狀的子節點。因此英雄節點不會出現在命中測試結果中。

是否檢查,如果英雄節點是你hitNode工作的父母?

+0

它的工作原理!謝謝:) – Tomasz

+0

聖@ *&#謝謝你這麼多,我被封鎖對這個 – darkheartfelt

1

我也跟着@mnuages意見就出來了這一點,我使用的是從蘋果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]; 
+0

我加入,我認爲,更好的辦法回答一個小時:) – Tomasz

1
if([daeNode childNodeWithName:hitTestResultNode.name recursively:YES]) 
    { 
     NSLog(@"hit!"); 
    } 

daeNode - 從.dae

0節點

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