[英]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: 我想知道哪个节点被命中,但是我的方法仅适用于具有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
}
}
}
and this is how I make a .dae Node (heroNode): 这就是我制作.dae节点(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? 检查hero节点是否是您的hitNode的父节点有效吗?
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 我遵循@mnuages建议并提出了建议,我使用的是Apple 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");
}
}
} }
In viewDidLoad I create the character like this: 在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];
if([daeNode childNodeWithName:hitTestResultNode.name recursively:YES])
{
NSLog(@"hit!");
}
daeNode - Node from .dae daeNode-.dae中的节点
hitTestResultNode - Node from SCNHitTestResult: 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;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.