基于SceneKit的3D模型触碰事件

流程如下:

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;
复制代码

代码

转载于:https://juejin.im/post/5afd55266fb9a07ab379aa02

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值