SceneKit with SpriteKit intersection of nodes

自作多情 提交于 2019-12-25 08:18:30

问题


Hi I want to have a selection box on my game. I assume that making with 'SpriteKit' overlay is the easiest way to do But is there any API to detect intersection of 'SKShapeNode' and 'SCNNode' (Ignoring the Z axis ofcourse) ?

What have I tried to do: _unitsArray is an array of SCNNodes that I wish to have they're bounding boxes in a SKScene

                   for (SCNNode * node in _unitsArray)
                   {
                       SCNVector3 min = SCNVector3Zero;
                       SCNVector3 max = SCNVector3Zero;

                       [node getBoundingBoxMin:&min max:&max];

                       NSLog(@"Min Before %lf, %lf, %lf", min.x, min.y, min.z);
                       NSLog(@"Max Before %lf, %lf, %lf", max.x, max.y, max.z);


                       min = vector3Multiply(min, node.scale);
                       max = vector3Multiply(max, node.scale);


                       //Lets Get Nodes's Position
                       SCNVector3 position = node.position;
                       NSLog(@"Position =  %lf, %lf, %lf", position.x, position.y, position.z);

                       SCNView * view = (SCNView *)self.view;
                       SCNVector3 posinView = [view projectPoint:position];

                       NSLog(@"Position in View =  %lf, %lf, %lf", posinView.x, posinView.y, posinView.z);

                                                 min = [view projectPoint:min];
                       max = [view projectPoint:max];
                       NSLog(@"Min Scaled + Projected %lf, %lf, %lf", min.x, min.y, min.z);
                       NSLog(@"Max Scaled + Projected %lf, %lf, %lf", max.x, max.y, max.z);

                       CGFloat width    =  max.x - min.x;
                       CGFloat height   =  max.y - min.y;
                       CGRect maybeRect = CGRectMake(posinView.x - width /2, posinView.y - height /2, width, height);

                       SKSpriteNode * Snode = [SKSpriteNode spriteNodeWithColor:[SKColor yellowColor] size:maybeRect.size];
                       [Snode setPosition:CGPointMake(posinView.x, posinView.y)];
                       [self addChild:Snode];
}

For some reason the Snode is twice bigger than expected :-O Also i don't know what to do with the returned Z Can some1 shed some light ?

来源:https://stackoverflow.com/questions/40297443/scenekit-with-spritekit-intersection-of-nodes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!