I'm currently building a game that is implementing collision detection in Scenekit, something I haven't dealt with before.
The game consists of a ship flying through a tunnel, the end goal being that when the ship hits the tunnel it will bounce off.
I have my ship set up using the following:
func setupShipNodes() {
shipNode = scene.rootNode.childNode(withName: "ship", recursively: true)!
shipNode.name = "shipNode"
let shipPhysicsShape = SCNPhysicsShape(node: shipNode)
shipNode.physicsBody = SCNPhysicsBody(type: .dynamic, shape: shipPhysicsShape)
shipNode.physicsBody?.contactTestBitMask = shipNode.physicsBody!.collisionBitMask
shipMesh = scene.rootNode.childNode(withName: "shipMesh", recursively: true)
shipMesh.name = "shipMesh"
}
And my tunnels are set up this way:
func setupTunnel() {
tunnelShape = SCNTube(innerRadius: 5, outerRadius: 5, height: 100)
tunnelNode = SCNNode(geometry: tunnelShape)
tunnelNode.geometry?.firstMaterial?.diffuse.contents = UIColor.orange
tunnelNode.position = SCNVector3(x: 0, y: 0, z: 0)
tunnelNode.eulerAngles = SCNVector3(x: 1.5708, y: 0, z: 0)
let physicsShape = SCNPhysicsShape(geometry: tunnelShape)
tunnelNode.physicsBody = SCNPhysicsBody(type: .static, shape: physicsShape)
tunnelNode.physicsBody?.contactTestBitMask = tunnelNode.physicsBody!.collisionBitMask
tunnelNode.name = "tunnelNode"
scene.rootNode.addChildNode(tunnelNode)
let material = SCNMaterial()
material.isDoubleSided = true
material.diffuse.contents = UIColor.red
addedTunnelNode.geometry?.firstMaterial = material
addedTunnelNode.geometry?.firstMaterial? = material
let physicsShape = SCNPhysicsShape(geometry: geometry)
addedTunnelNode.physicsBody = SCNPhysicsBody(type: .static, shape: physicsShape)
addedTunnelNode.physicsBody?.contactTestBitMask = addedTunnelNode.physicsBody!.collisionBitMask
Please note that this second tunnel is a custom geometry octagon built up of triangles by specifying the vertices.
At the moment, the game reads as if the tunnels and ship are continuously colliding and it creates a very laggy, bounce-around type aspect to the game. I want it so collisions are only detected when the nodes touch and when they do, the ship will bounce off of the tunnel instead of going through it.
What am I doing wrong and how can I solve this?
Thanks to everyone who took the time to read this.