Ok this one has had me scratching my head for the past couple of hours.
I'm attempting to do raytesting on an SCNScenePhysicsWorld (hitTest in SCNRenderer works without a glitch though)
Steps i take: 1.- properties:
let scene = SCNScene()
var sceneView = SCNView(frame: CGRectZero, options: [SCNPreferredRenderingAPIKey:NSNumber(unsignedLong: SCNRenderingAPI.OpenGLES2.rawValue)])
// hit sphere
let hitSphere = SCNSphere(radius: 55)
var hitNode : SCNNode! = nil
var hitShape : SCNPhysicsShape! = nil
var hitBody : SCNPhysicsBody! = nil
2.-
setupWorld {
sceneView.scene = scene
sceneView.delegate = self
.
.
.
hitNode = SCNNode(geometry: hitSphere)
hitNode.position = SCNVector3Make(0, 0, 0)
// neither work
hitShape = SCNPhysicsShape(geometry: hitSphere, options: nil)
// hitShape = SCNPhysicsShape(node: hitNode, options: nil)
hitBody = SCNPhysicsBody(type: .Static, shape: hitShape)
hitNode.physicsBody = hitBody
.
.
.
spheresNode.position = SCNVector3Make(0, 0, 0)
spheresNode.addChildNode(renderNode)
spheresNode.addChildNode(hitNode)
scene.delegate = self
}
3.-
func renderer(renderer: SCNSceneRenderer, didRenderScene scene: SCNScene, atTime time: NSTimeInterval) {
.
.
.
let rayHit = scene.physicsWorld.rayTestWithSegmentFromPoint(nearpoint, toPoint: farpoint, options: [SCNPhysicsTestBackfaceCullingKey:false]).first
i keep getting rayHit = nil but i'm 100% sure the hitBody lies within the nearpoint and farpoint
I more than aware i must be missing something here, not sure what though
any clues or pointers?
PS: the nearpoint is inside the hitSphere, the farpoint outside, i want to find the coordinate it hits at. I'm already doing it via SCNSceneRenderer.hitTest but want to learn how to do it by rayTest.
}