SceneKit: How to detect contact without collision

老子叫甜甜 提交于 2019-12-03 16:20:15
FlippinFun

This question deserves a full tutorial. In short, each physics body has a categoryBitMask and collisionBitMask.

categoryBitMask This is the bit that represents the body.

collisionBitMask This is the bit that represents which bodies collide with it.

By default (when assigned a physicsBody) all bodies collide with the exception of a kinematicBody, which act special. Objects don't collide with it. But don't get confused, it can collide with other objects when manually moved (such as by dragging it with a finger).

How to handle!!!

Assign your bodies whether it be static or dynamic. Assign there category and collision mask. If the mask match up they collide. How about an example!

These bodies collide with each other

bodyA.physicsBody.categoryBitMask = 4;
bodyB.physicsBody.categoryBitMask = 8;

bodyA.physicsBody.collisionBitMask = 8;
bodyB.physicsBody.collisionBitMask = 4;

These don't collide

bodyA.physicsBody.categoryBitMask = 4;
bodyB.physicsBody.categoryBitMask = 8;

bodyA.physicsBody.collisionBitMask = 0;
bodyB.physicsBody.collisionBitMask = 0;

Noticed I used categories by power of two. This is because the contactDelegate uses bitwise AND to compare. Read up on bitwise if you don't understand. It will really help.

These bodies collide without physics

EXAMPLE: You have a hero that runs through a ghost and you want to know that it happened without either one being effected. Such as bouncing off each other.

bodyA.physicsBody.categoryBitMask = 4;
bodyB.physicsBody.categoryBitMask = 8;

bodyA.physicsBody.collisionBitMask = 0;
bodyB.physicsBody.collisionBitMask = 0;

So the following code is designed so that bodyA(your hero) runs through bodyB(ghost).

bodyB.physicsField.categoryBitMask = 4

Noticed that the above is a physicsField.catagoryBitMask. It is assigned a 4 so that it matches up with bodyA.

let collisionField = SCNPhysicsField.customFieldWithEvaluationBlock(){ 
    (_, _, _, _, _) in 
    WHAT U WANT TO DO ON CONTACT
}

bodyB.physicsField = collisionField

The physicsField is not the shape of your geometry unfortunately. Its default is the shape of its bounding box or sphere/elliptical if you set the physicsField property usesEllipsoidalExtent to "true/yes"

The area of effect can be changed using the physicsFields halfExtent property like so...

bodyB.physicsField.halfExtent = SCNVector3Make(.5, .5, .5)

The above will extend the field of a box the size of (2, 2, 2) to (2.5, 2.5, 2.5).

EDIT: If they still collide, try removing the physicsbody from bodyB (this will remove all physics simulation from the node). I don't think it will be a problem as long as the catagory and collision bitmask are assigned properly.

This is the best method I could find. All coding is from my head, with the support of Apple docs, so may not be exactly right but I hope this helps!

I have found the best way is to just set the mass so small on a kinematic body that the other one will pass through it.

sensorBox.physicsBody.mass = 0.00000000001; // super tiny mass makes it a "sensor"

Don't forget your scene's physics world can call a "contactTestBetweenBody: andBody: withOptions... this works great.

Usage

[myScene.physicsWorld contactTestBetweenBody:(SCNPhysicsBody *) andBody:(SCNPhysicsBody *) options:(NSDictionary *)];
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!