didBeginContact 委托方法未触发 ARKit 碰撞检测
Posted
技术标签:
【中文标题】didBeginContact 委托方法未触发 ARKit 碰撞检测【英文标题】:didBeginContact delegate method not firing for ARKit collision detection 【发布时间】:2020-08-02 12:37:01 【问题描述】:我无法触发 didBeginContact 方法,我已经尝试了一段时间,但我无法发现错误,可以换一个新的眼睛:
- (void)viewDidLoad
[super viewDidLoad];
self.lastRender = nil;
self.accelX = 0.0;
self.accelY = 0.0;
self.accelZ = 0.0;
self.isLooping = TRUE;
self.tripWire = TRUE;
self.lastPaddleNode = [[SCNNode alloc] init];
self.paddleNode = [[SCNNode alloc] init];
SCNPlane* paddlePlane = [SCNPlane planeWithWidth:0.067056 height:0.138176];
self.paddleNode.geometry = paddlePlane;
self.paddleNode.geometry.firstMaterial.diffuse.contents = [UIColor colorWithRed:133.0/255.0f green:158.0/255.0f blue:122.0/255.0f alpha:0.8];
self.paddleNode.simdTransform = matrix_identity_float4x4;
self.paddleNode.name = @"paddle";
SCNPhysicsBody* paddlePhys = [[SCNPhysicsBody alloc] init];
paddlePhys.type = SCNPhysicsBodyTypeDynamic;
paddlePhys.physicsShape = [SCNPhysicsShape shapeWithGeometry:paddlePlane options:nil];
//paddlePhys.affectedByGravity = FALSE;
paddlePhys.categoryBitMask = SCNPhysicsCollisionCategoryAll;
paddlePhys.collisionBitMask = SCNPhysicsCollisionCategoryAll;
paddlePhys.contactTestBitMask = SCNPhysicsCollisionCategoryAll;
paddlePhys.usesDefaultMomentOfInertia = TRUE;
self.paddleNode.physicsBody = paddlePhys;
SCNNode* ball = [[SCNNode alloc] init];
SCNPhysicsBody* phys = [[SCNPhysicsBody alloc] init];
phys.type = SCNPhysicsBodyTypeDynamic;
SCNSphere* sphere = [SCNSphere sphereWithRadius:0.067f];
phys.physicsShape = [SCNPhysicsShape shapeWithGeometry:sphere options:nil];
ball.geometry = sphere;
ball.geometry.firstMaterial.diffuse.contents = [UIColor yellowColor];
//phys.affectedByGravity = FALSE;
phys.categoryBitMask = SCNPhysicsCollisionCategoryAll;
phys.collisionBitMask = SCNPhysicsCollisionCategoryAll;
phys.contactTestBitMask = SCNPhysicsCollisionCategoryAll;
phys.usesDefaultMomentOfInertia = TRUE;
phys.velocityFactor = SCNVector3Make(10.0, 10.0, 10.0);
ball.physicsBody = phys;
ball.physicsBody.continuousCollisionDetectionThreshold = 0.134f;
ball.simdPosition = simd_make_float3(0.0, 0.27, -0.27);
ball.name = @"ball";
//phys.velocityFactor = SCNVector3Make(10, 10, 10);
[self.sceneView setPreferredFramesPerSecond:60];
self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.delegate = self;
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
// Set the view's delegate
self.sceneView.delegate = self;
// Show statistics such as fps and timing information
self.sceneView.showsStatistics = YES;
//phys.velocityFactor = SCNVector3Make(10, 10, 10);
// Create a new scene
SCNScene* scene = [[SCNScene alloc] init];
scene.physicsWorld.contactDelegate = self;
self.sceneView.scene = scene;
[locationManager startUpdatingHeading];
while(!(self.locationManager.heading.trueHeading <= 360 && self.locationManager.heading.trueHeading >= 359))
NSLog(@"heading: %f", self.locationManager.heading.trueHeading);
if (self.locationManager.heading.trueHeading <= 360 && self.locationManager.heading.trueHeading >= 359)
break;
[self.sceneView.scene.rootNode addChildNode:self.paddleNode];
[self.sceneView.scene.rootNode addChildNode:ball];
[self startUpdates];
我现在想要写的是当我的游戏中的两个节点发生碰撞时触发的委托方法:
- (void)physicsWorld:(SCNPhysicsWorld *)world didBeginContact:(SCNPhysicsContact *)contact
NSLog(@"in didBeginContact");
CollisionCategory contactMask =
contact.nodeA.physicsBody.categoryBitMask | contact.nodeB.physicsBody.categoryBitMask;
// first, sort out what kind of collision
if (contactMask == (CollisionCategoryPaddle | CollisionCategoryBall))
// next, sort out which body is the missile and which is the rocket
// and do something about it
if (contact.nodeA.physicsBody.categoryBitMask == CollisionCategoryPaddle)
NSLog(@"nodeA is paddle!");
//[self hitRocket:contact.nodeB withMissile:contact.nodeA];
else
NSLog(@"nodeB is paddle!");
//[self hitRocket:contact.nodeA withMissile:contact.nodeB];
注意
我知道我正在使用SCNPhysicsCollisionCategoryAll
,然后在该方法中检查不同类型的位掩码。那不是我的问题,我的问题是在那之前,我从来没有看到这个日志语句,这是我didBeginContact
委托方法的第一行:
NSLog(@"in didBeginContact");
为什么我的方法没有触发?我想我正确设置了代表。谢谢。
更新
根据评论者的建议,我的 viewDidLoad
现在看起来像这样:
- (void)viewDidLoad
[super viewDidLoad];
// Create a new scene
SCNScene* scene = [[SCNScene alloc] init];
scene.physicsWorld.contactDelegate = self;
self.sceneView.scene = scene;
// Set the view's delegate
self.sceneView.delegate = self;
SCNPlane* paddlePlane = [SCNPlane planeWithWidth:0.067056 height:0.138176];
self.paddleNode = [SCNNode nodeWithGeometry:paddlePlane];
self.paddleNode.geometry.firstMaterial.diffuse.contents = [UIColor colorWithRed:133.0/255.0f green:158.0/255.0f blue:122.0/255.0f alpha:0.8];
self.paddleNode.simdTransform = matrix_identity_float4x4;
self.paddleNode.name = @"paddle";
[self.sceneView.scene.rootNode addChildNode:self.paddleNode];
SCNPhysicsBody* paddlePhys = [[SCNPhysicsBody alloc] init];
paddlePhys.type = SCNPhysicsBodyTypeDynamic;
paddlePhys.physicsShape = [SCNPhysicsShape shapeWithGeometry:self.paddleNode.geometry options:@SCNPhysicsShapeTypeKey: SCNPhysicsShapeTypeBoundingBox];
//paddlePhys.affectedByGravity = FALSE;
paddlePhys.categoryBitMask = SCNPhysicsCollisionCategoryAll;
paddlePhys.collisionBitMask = SCNPhysicsCollisionCategoryAll;
paddlePhys.contactTestBitMask = SCNPhysicsCollisionCategoryAll;
//paddlePhys.usesDefaultMomentOfInertia = TRUE;
self.paddleNode.physicsBody = paddlePhys;
SCNNode* ball = [[SCNNode alloc] init];
[self.sceneView.scene.rootNode addChildNode:ball];
SCNPhysicsBody* phys = [[SCNPhysicsBody alloc] init];
phys.type = SCNPhysicsBodyTypeDynamic;
SCNSphere* sphere = [SCNSphere sphereWithRadius:0.067f];
phys.physicsShape = [SCNPhysicsShape shapeWithGeometry:sphere options:@SCNPhysicsShapeTypeKey: SCNPhysicsShapeTypeConvexHull];
ball.geometry = sphere;
ball.geometry.firstMaterial.diffuse.contents = [UIColor yellowColor];
//phys.affectedByGravity = FALSE;
phys.categoryBitMask = SCNPhysicsCollisionCategoryAll;
phys.collisionBitMask = SCNPhysicsCollisionCategoryAll;
phys.contactTestBitMask = SCNPhysicsCollisionCategoryAll;
phys.usesDefaultMomentOfInertia = TRUE;
phys.velocityFactor = SCNVector3Make(10.0, 10.0, 10.0);
ball.physicsBody = phys;
ball.physicsBody.continuousCollisionDetectionThreshold = 0.134f;
ball.simdPosition = simd_make_float3(0.0, 0.27, -0.27);
ball.name = @"ball";
//phys.velocityFactor = SCNVector3Make(10, 10, 10);
// Show statistics such as fps and timing information
self.sceneView.showsStatistics = YES;
self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.delegate = self;
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
[locationManager startUpdatingHeading];
while(!(self.locationManager.heading.trueHeading <= 360 && self.locationManager.heading.trueHeading >= 358))
NSLog(@"heading: %f", self.locationManager.heading.trueHeading);
if (self.locationManager.heading.trueHeading <= 360 && self.locationManager.heading.trueHeading >= 358)
break;
[self.sceneView setPreferredFramesPerSecond:60];
self.lastRender = nil;
self.accelX = 0.0;
self.accelY = 0.0;
self.accelZ = 0.0;
self.isLooping = TRUE;
self.tripWire = TRUE;
self.lastPaddleNode = [[SCNNode alloc] init];
[self startUpdates];
我同意评论者的观点,这可能是这里的排序方式有问题,我只是看不到它是什么...hellllppp,我快要昏昏欲睡了...
更新
我更改了按位掩码如下,我没有使用自定义枚举类型,因为我只有两个对象,所以我想我可以只使用...All
和...Default
,但它不是这样工作的。当我理解按位与运算时,我会尝试自定义枚举类型,如果有人可以向我解释左手/右手部分,以及如何进行操作?
//Paddle
paddlePhys.categoryBitMask = SCNPhysicsCollisionCategoryDefault;
paddlePhys.collisionBitMask = SCNPhysicsCollisionCategoryAll;
paddlePhys.contactTestBitMask = SCNPhysicsCollisionCategoryAll;
...
//Ball
phys.categoryBitMask = SCNPhysicsCollisionCategoryAll;
phys.collisionBitMask = SCNPhysicsCollisionCategoryDefault;
phys.contactTestBitMask = SCNPhysicsCollisionCategoryDefault;
这应该有效吗?我现在也有这样的桨组:
paddlePhys.type = SCNPhysicsBodyTypeKinematic;
paddlePhys.physicsShape = [SCNPhysicsShape shapeWithGeometry:self.paddleNode.geometry options:@SCNPhysicsShapeTypeKey: SCNPhysicsShapeTypeBoundingBox];
我的球是这样的:
phys.type = SCNPhysicsBodyTypeDynamic;
phys.physicsShape = [SCNPhysicsShape shapeWithGeometry:sphere options:@SCNPhysicsShapeTypeKey: SCNPhysicsShapeTypeConvexHull];
现在我在一起了:
- (void)viewDidLoad
[super viewDidLoad];
self.sceneView.delegate = self;
// Show statistics such as fps and timing information
self.sceneView.showsStatistics = YES;
SCNScene* scene = [[SCNScene alloc] init];
// Create a new scene
SCNPlane* paddlePlane = [SCNPlane planeWithWidth:0.067056 height:0.138176];
paddlePlane.firstMaterial.doubleSided = YES;
self.paddleNode = [SCNNode nodeWithGeometry:paddlePlane];
self.paddleNode.geometry.firstMaterial.diffuse.contents = [UIColor colorWithRed:133.0/255.0f green:158.0/255.0f blue:122.0/255.0f alpha:0.8];
self.paddleNode.simdTransform = matrix_identity_float4x4;
self.paddleNode.name = @"paddle";
SCNPhysicsBody* paddlePhys = [[SCNPhysicsBody alloc] init];
paddlePhys.type = SCNPhysicsBodyTypeKinematic;
paddlePhys.physicsShape = [SCNPhysicsShape shapeWithGeometry:self.paddleNode.geometry options:@SCNPhysicsShapeTypeKey: SCNPhysicsShapeTypeBoundingBox];
//paddlePhys.affectedByGravity = FALSE;
paddlePhys.categoryBitMask = SCNPhysicsCollisionCategoryDefault;
paddlePhys.collisionBitMask = SCNPhysicsCollisionCategoryAll;
paddlePhys.contactTestBitMask = SCNPhysicsCollisionCategoryAll;
//paddlePhys.usesDefaultMomentOfInertia = TRUE;
self.paddleNode.physicsBody = paddlePhys;
self.paddleNode.simdPivot = [self makeTranslationMatrix:0 ty: 0 tz:0.1];
SCNNode* ball = [[SCNNode alloc] init];
SCNPhysicsBody* phys = [[SCNPhysicsBody alloc] init];
SCNSphere* sphere = [SCNSphere sphereWithRadius:0.067f];
phys.type = SCNPhysicsBodyTypeDynamic;
phys.physicsShape = [SCNPhysicsShape shapeWithGeometry:sphere options:@SCNPhysicsShapeTypeKey: SCNPhysicsShapeTypeConvexHull];
ball.geometry = sphere;
ball.geometry.firstMaterial.diffuse.contents = [UIColor yellowColor];
//phys.affectedByGravity = FALSE;
phys.categoryBitMask = SCNPhysicsCollisionCategoryAll;
phys.collisionBitMask = SCNPhysicsCollisionCategoryDefault;
phys.contactTestBitMask = SCNPhysicsCollisionCategoryDefault;
phys.usesDefaultMomentOfInertia = TRUE;
phys.velocityFactor = SCNVector3Make(10.0, 10.0, 10.0);
ball.physicsBody = phys;
ball.physicsBody.continuousCollisionDetectionThreshold = 0.134f;
ball.simdPosition = simd_make_float3(0.0, 0.27, -0.27);
ball.name = @"ball";
self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.delegate = self;
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
[locationManager startUpdatingHeading];
while(!(self.locationManager.heading.trueHeading <= 360 && self.locationManager.heading.trueHeading >= 358))
NSLog(@"heading: %f", self.locationManager.heading.trueHeading);
if (self.locationManager.heading.trueHeading <= 360 && self.locationManager.heading.trueHeading >= 358)
break;
[self.sceneView setPreferredFramesPerSecond:60];
self.lastRender = nil;
self.accelX = 0.0;
self.accelY = 0.0;
self.accelZ = 0.0;
self.isLooping = TRUE;
self.tripWire = TRUE;
self.lastPaddleNode = [[SCNNode alloc] init];
[scene.rootNode addChildNode:ball];
[scene.rootNode addChildNode:self.paddleNode];
scene.physicsWorld.contactDelegate = self;
self.sceneView.scene = scene;
[self startUpdates];
我还想知道问题是否出在 CMDeviceMotion 事件的块中,这里是:
- (void)startUpdates
//dispatch_queue_t myQueue = dispatch_queue_create("com.eamon.corona_pong", DISPATCH_QUEUE_SERIAL);
// Determine the update interval.
NSTimeInterval updateInterval = 1.0/60.0;
// Create a CMMotionManager object.
CMMotionManager *mManager = [(AppDelegate *)[[UIApplication sharedApplication] delegate] sharedManager];
if ([mManager isDeviceMotionAvailable] == YES)
[mManager setDeviceMotionUpdateInterval:updateInterval];
// do something appropriate here
if ([CMMotionManager availableAttitudeReferenceFrames] & CMAttitudeReferenceFrameXTrueNorthZVertical)
[mManager startDeviceMotionUpdatesUsingReferenceFrame:CMAttitudeReferenceFrameXArbitraryCorrectedZVertical toQueue:[NSOperationQueue mainQueue] withHandler:^(CMDeviceMotion * _Nullable motion, NSError * _Nullable error)
//[self handleMotionWrapper:motion];
[SCNTransaction begin];
[SCNTransaction setAnimationDuration:0];
//simd_float4x4 rotateY = [self makeYRotationMatrix:270.0];
//simd_float4x4 rotate = [self makeXRotationMatrix:270.0];
simd_float4x4 cameraTrans = self.sceneView.session.currentFrame.camera.transform;
simd_float4x4 temp = simd_mul(cameraTrans, [self makeYRotationMatrix:270.0]);
//simd_float4x4 rotated = simd_mul(temp, [self makeYRotationMatrix:135]);
simd_float4x4 transMatrixMotion = [self makeTranslationMatrix:self.lastPaddleNode.simdPosition.x ty:self.lastPaddleNode.simdPosition.y - 0.1 tz:self.lastPaddleNode.simdPosition.z];
self.paddleNode.simdPosition = simd_make_float3(simd_mul(simd_make_float4(temp.columns[3][0], temp.columns[3][1], temp.columns[3][2], 1.0), transMatrixMotion));
self.paddleNode.simdTransform = temp;
[SCNTransaction commit];
self.lastPaddleNode = self.paddleNode;
self.tripWire = FALSE;
];
我仍在制定运动代码,但我认为碰撞应该可以工作,即使我还没有完全按照我想要的方式定向桨(我只需要将它围绕 y 旋转 90 度-轴,但事实证明它很烦人,但对于碰撞检测应该没关系,对吧?)
另外,SCNPlane
呢,我还用firstMaterial
属性的doubleSided
属性将我的SCNPlane
(桨)双面制作,但它没有帮助,也许我需要使用浅SCN盒子?我以前尝试过,但也许我所做的一些更改意味着如果我制作我的桨和SCNBox
而不是双面的SCNPlane
,它会起作用吗?
谢谢!
更新 我想我会分享我最近的尝试,但没有用,但我认为它可能更接近,可能更容易发现问题:
typedef NS_OPTIONS(NSUInteger, CollisionCategory)
CollisionCategoryPaddle = 1 << 0,
CollisionCategoryBall = 1 << 1,
;
- (void)viewDidLoad
[super viewDidLoad];
self.sceneView.delegate = self;
// Show statistics such as fps and timing information
self.sceneView.showsStatistics = YES;
SCNScene* scene = [[SCNScene alloc] init];
self.sceneView.scene = scene;
self.sceneView.scene.physicsWorld.contactDelegate = self;
// Create a new scene
SCNBox* paddlePlane = [SCNBox boxWithWidth:0.067056 height:0.138176 length:0.01 chamferRadius:0.0];
paddlePlane.firstMaterial.doubleSided = YES;
self.paddleNode = [SCNNode nodeWithGeometry:paddlePlane];
self.paddleNode.geometry.firstMaterial.diffuse.contents = [UIColor colorWithRed:133.0/255.0f green:158.0/255.0f blue:122.0/255.0f alpha:0.8];
self.paddleNode.simdTransform = matrix_identity_float4x4;
self.paddleNode.name = @"paddle";
[self.sceneView.scene.rootNode addChildNode:self.paddleNode];
SCNPhysicsBody* paddlePhys = [[SCNPhysicsBody alloc] init];
paddlePhys.type = SCNPhysicsBodyTypeKinematic;
paddlePhys.physicsShape = [SCNPhysicsShape shapeWithGeometry:self.paddleNode.geometry options:nil];
//paddlePhys.affectedByGravity = FALSE;
paddlePhys.categoryBitMask = CollisionCategoryPaddle;
paddlePhys.collisionBitMask = CollisionCategoryBall;
paddlePhys.contactTestBitMask = CollisionCategoryBall;
//paddlePhys.usesDefaultMomentOfInertia = TRUE;
self.paddleNode.physicsBody = paddlePhys;
SCNPhysicsBody* phys = [[SCNPhysicsBody alloc] init];
SCNSphere* sphere = [SCNSphere sphereWithRadius:0.067f];
self.ballNode = [[SCNNode alloc] init];
self.ballNode.name = @"ball";
self.ballNode.geometry = sphere;
self.ballNode.geometry.firstMaterial.diffuse.contents = [UIColor yellowColor];
//self.ballNode.physicsBody.continuousCollisionDetectionThreshold = (CGFloat)0.134f;
self.ballNode.simdPosition = simd_make_float3(0.0, 0.27, -0.27);
[self.sceneView.scene.rootNode addChildNode:self.ballNode];
phys.type = SCNPhysicsBodyTypeDynamic;
phys.physicsShape = [SCNPhysicsShape shapeWithGeometry:self.ballNode.geometry options:nil];
phys.categoryBitMask = CollisionCategoryBall;
phys.collisionBitMask = CollisionCategoryPaddle;
phys.contactTestBitMask = CollisionCategoryPaddle;
//phys.usesDefaultMomentOfInertia = TRUE;
//phys.velocityFactor = SCNVector3Make(10.0, 10.0, 10.0);
self.ballNode.physicsBody = phys;
//phys.affectedByGravity = FALSE
self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.delegate = self;
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
[locationManager startUpdatingHeading];
while(!(self.locationManager.heading.trueHeading <= 360 && self.locationManager.heading.trueHeading >= 358))
NSLog(@"heading: %f", self.locationManager.heading.trueHeading);
if (self.locationManager.heading.trueHeading <= 360 && self.locationManager.heading.trueHeading >= 358)
break;
[self.sceneView setPreferredFramesPerSecond:60];
self.lastRender = nil;
self.accelX = 0.0;
self.accelY = 0.0;
self.accelZ = 0.0;
self.isLooping = TRUE;
self.tripWire = TRUE;
self.lastPaddleNode = [[SCNNode alloc] init];
[self startUpdates];
我也尝试过这种方式,虽然我不确定我是否正确使用它:
phys.physicsShape = [SCNPhysicsShape shapeWithGeometry:self.ballNode.geometry options:@SCNHitTestOptionCategoryBitMask:[NSNumber numberWithUnsignedInt:CollisionCategoryBall], SCNPhysicsTestCollisionBitMaskKey:[NSNumber numberWithUnsignedInt:CollisionCategoryPaddle]];
【问题讨论】:
尝试将addChildNode
调用:[self.sceneView.scene.rootNode addChildNode:self.paddleNode];
和 [self.sceneView.scene.rootNode addChildNode:ball];
移到 physicsBody
创建上方,看看是否有帮助?
昨晚我正在这样做!虫子睡着了,现在试试我会告诉你的!
@MohammadRezaFarahani 查看我的编辑
好的,现在请确保您为对象使用不同的categoryBitMask
。
好的,我一秒再回到枚举类型冲突
【参考方案1】:
1.) 将
scene
添加到sceneView
2.) 声明您将用于物理对象的节点。
3.) 对实例化对象(颜色等)进行必要的修改。
--注意第 4 步非常重要--
4.) 只有在上述步骤(1、2 和 3)之后,您才能将
physicsBody
详细信息添加到对象! (又名node.physicsBody.xxx
)将节点添加到场景中。
在我看来SceneKit
处理这部分事情的方式很荒谬,需要修改。在尚未将 SCNNode 添加到场景中的情况下花费了太多时间,由于您实例化它的属性的顺序而导致问题:(
【讨论】:
以上是关于didBeginContact 委托方法未触发 ARKit 碰撞检测的主要内容,如果未能解决你的问题,请参考以下文章
didBeginContact:(SKPhysicsContact *)联系人未调用
NSFetchedResultsController 委托方法未针对这些更改触发
NSFetchedResultsController 委托方法在 mergeChangesFromContextDidSaveNotification 后未触发