碰撞检测
程序员文章站
2022-03-13 12:33:17
...
//
// 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
上一篇: 打印1到最大的n位数(Java)
下一篇: 在两个排序数组中找到第k小的数