流程如下:
1.获取到手指在屏幕的位置
2.然后转换到SCNView上
3.然后返回与手指点击方向的所有3D物体
######主要代码如下
代码语言:txt复制- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
// 获取到手势的对象
UITouch *touch = [touches allObjects].firstObject;
// 手势在SCNView中的位置
CGPoint touchPoint = [touch locationInView:self.jpARSCNView];
//该方法会返回一个SCNHitTestResult数组,这个数组中每个元素的node都包含了指定的点(CGPoint)
NSArray *hitResults = [self.jpARSCNView hitTest:touchPoint options:nil];
if (hitResults.count > 0) {
SCNHitTestResult * hit = [hitResults firstObject];
SCNNode *node = hit.node;
if (node.geometry == self.jpBox) {
NSLog(@"点击了箱子");
[self removeBox];
[self addBox];
}
}
}
######设置物理仿真代码如下
代码语言:txt复制SCNBox * box = [SCNBox boxWithWidth:0.18 height:0.18 length:0.18 chamferRadius:0];
self.geometry = box;
SCNPhysicsShape * shape = [SCNPhysicsShape shapeWithGeometry:box options:nil];
self.physicsBody = [SCNPhysicsBody bodyWithType:SCNPhysicsBodyTypeDynamic shape:shape];
[self.physicsBody setAffectedByGravity:NO];
self.physicsBody.categoryBitMask = 1;
self.physicsBody.contactTestBitMask = 2;
代码