流程如下:
1.獲取到手指在螢幕的位置 2.然後轉換到SCNView上 3.然後返回與手指點選方向的所有3D物體
######主要程式碼如下
- (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];
}
}
}
複製程式碼
######設定物理模擬程式碼如下
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;
複製程式碼