碰撞检测

//
//  GameViewController.m
//  碰撞检测
//
//  Created by 柯木超 on 2019/4/9.
//  Copyright © 2019年 柯木超. All rights reserved.
//

#import "GameViewController.h"
typedef NS_ENUM(NSUInteger,MaskType) {
    MaskTypeWithBull1 = 4,
    MaskTypeWithBull2 = 8,
};

@implementation GameViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.scnView = [[SCNView alloc]initWithFrame:self.view.bounds];
    self.scnView.backgroundColor = [UIColor grayColor];
    
    [self.view addSubview: self.scnView];
    
    self.scnView.allowsCameraControl = YES;
    // 设置场景
    self.scnView.scene = [SCNScene scene];
    self.scnView.scene.physicsWorld.contactDelegate = self;
    // 添加照相机
    SCNCamera *camera = [SCNCamera camera];
    
    // 设置正投影
    camera.usesOrthographicProjection = YES;
    // 设置正投影比例
    camera.orthographicScale = 10;
    
    SCNNode *caNode = [SCNNode node];
    caNode.camera = camera;
    caNode.position = SCNVector3Make(0, 0, 50);
    // 把节点添加进去场景
    [self.scnView.scene.rootNode addChildNode:caNode];
    
    //添加两个正方体
    // 创建几何立体
    SCNBox *box1 = [SCNBox boxWithWidth:0.5 height:0.5 length:0.5 chamferRadius:0];
    
    // 创建节点,绑定几何体
    SCNNode *node1 = [SCNNode nodeWithGeometry:box1];
    node1.position = SCNVector3Make(0, 0, -20);
    
    node1.name = @"ball1";
    SCNPhysicsBody *ballPhysicsBody = [SCNPhysicsBody bodyWithType:SCNPhysicsBodyTypeDynamic shape:[SCNPhysicsShape shapeWithGeometry:[SCNSphere sphereWithRadius:0.35] options:nil]];
    ballPhysicsBody.mass = 3;
    ballPhysicsBody.friction = 2;
    ballPhysicsBody.contactTestBitMask = 1;
    ballPhysicsBody.categoryBitMask = 1;
    //设置physicsBody的类别掩码,可以理解为一个身份标识,类似tag之类的
    ballPhysicsBody.categoryBitMask = MaskTypeWithBull1;
    //设置physicsBody碰撞掩码,设置可以碰撞的物体
    ballPhysicsBody.collisionBitMask = MaskTypeWithBull2;
    //设置physicsBody关系掩码,用于碰撞后在代理里面处理后续操作
    ballPhysicsBody.contactTestBitMask = MaskTypeWithBull2;
    ballPhysicsBody.affectedByGravity = NO;
    node1.physicsBody = ballPhysicsBody;
    
    // 把节点添加进去场景
    [self.scnView.scene.rootNode addChildNode:node1];
    
    // 创建几何立体
    SCNBox *box2 = [SCNBox boxWithWidth:0.5 height:0.5 length:0.5 chamferRadius:0];
    
    // 创建节点,绑定几何体
    SCNNode *node2 = [SCNNode nodeWithGeometry:box2];
    node2.position = SCNVector3Make(0, 10, -20);
    node2.name = @"ball2";
    SCNPhysicsBody *ballPhysicsBody2 = [SCNPhysicsBody bodyWithType:SCNPhysicsBodyTypeStatic shape:[SCNPhysicsShape shapeWithGeometry:[SCNSphere sphereWithRadius:0.35] options:nil]];
    ballPhysicsBody2.mass = 3;
    ballPhysicsBody2.friction = 2;
    ballPhysicsBody2.contactTestBitMask = 1;
    ballPhysicsBody2.categoryBitMask = 1;
    //设置physicsBody的类别掩码,可以理解为一个身份标识,类似tag之类的
    ballPhysicsBody2.categoryBitMask = MaskTypeWithBull2;
    //设置physicsBody碰撞掩码,设置可以碰撞的物体
    ballPhysicsBody2.collisionBitMask = MaskTypeWithBull1 ;
    //设置physicsBody关系掩码,用于碰撞后在代理里面处理后续操作
    ballPhysicsBody2.contactTestBitMask = MaskTypeWithBull1;
    ballPhysicsBody2.affectedByGravity = YES;
    node2.physicsBody = ballPhysicsBody2;
    // 把节点添加进去场景
    [self.scnView.scene.rootNode addChildNode:node2];
    
    // 具体操作:
    //    是否开启控制摄像机:
    self.scnView.allowsCameraControl =  YES;
    //    一个手指操作的时候,摄像机对准(0,0,0)沿着球体表面旋转
    //    连个手指平移手势,照相机是在x轴和y轴运动
    //    捏合e手势,是在Z 轴移动
}

- (BOOL)shouldAutorotate
{
    return YES;
}

- (BOOL)prefersStatusBarHidden {
    return YES;
}

- (UIInterfaceOrientationMask)supportedInterfaceOrientations
{
    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
        return UIInterfaceOrientationMaskAllButUpsideDown;
    } else {
        return UIInterfaceOrientationMaskAll;
    }
}

#pragma mark SCNPhysicsContactDelegate
-(void)physicsWorld:(SCNPhysicsWorld *)world didBeginContact:(SCNPhysicsContact *)contact{
    
    NSString *nodeNameA = contact.nodeA.name;
    NSString *nodeNameB = contact.nodeB.name;
    NSLog(@"nodeNameA==%@",nodeNameA);
    NSLog(@"nodeNameB==%@",nodeNameB);
}

@end

 

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值