Trigger an event when SCNode is near a certain transform.

Hi guys,

I'd like to trigger an event, in particular I'd like to load another scene when the camera node transform property is similar to a particular target transform.
Is it possible?
I thought about computing a distance between the camera transform and my target transform, but I don't know how to compute this distance. Any idea?

The measure of similarity does not need to be super precise but it has to be efficient since it has to run at 60fps.

Thank you

Giohn
Answered by DTS Engineer in 662033022
Hello Giohn,

If you only need to compare the distance between the camera and the node to trigger your event, then there are two approaches:
  1. You could add a physics body to the node that represents the trigger volume, add a physics body to the camera to detect collisions with the trigger volume, and then receive the contact in your SCNPhysicsContactDelegate. For more info about SceneKit physics contacts, see SCNPhysicsContact

  2. You could calculate the distance between the node and the camera every frame in the SCNSceneRendererDelegate. You could utilize the convertPosition(_:to:) method to get the position of one node relative to the other, and then calculate the distance by calculating the magnitude of that position.

If you have additional requirements related to orientation between the camera and the node, then you could use convertTransform(_:to:) to get the transform of one node relative to the other per-frame, and then make your decision to trigger the event or not based on that transform.
Accepted Answer
Hello Giohn,

If you only need to compare the distance between the camera and the node to trigger your event, then there are two approaches:
  1. You could add a physics body to the node that represents the trigger volume, add a physics body to the camera to detect collisions with the trigger volume, and then receive the contact in your SCNPhysicsContactDelegate. For more info about SceneKit physics contacts, see SCNPhysicsContact

  2. You could calculate the distance between the node and the camera every frame in the SCNSceneRendererDelegate. You could utilize the convertPosition(_:to:) method to get the position of one node relative to the other, and then calculate the distance by calculating the magnitude of that position.

If you have additional requirements related to orientation between the camera and the node, then you could use convertTransform(_:to:) to get the transform of one node relative to the other per-frame, and then make your decision to trigger the event or not based on that transform.
The fastest way would be to compare two "boxes" in 3D space.

You simply check if any one of the box A's points are inside box B.

Since SceneKit's "getBoundingBoxMin:Max:" is still broken, you will have to make up with the locations of the points yourself.

Simply create some variables for your points, and extend their locations based on the node's position.

If your game is not moving around vertically so much, you may not need to compare all the points of the two boxes. Instead just compare the 2D locations as if the world was flattened. (Or even go further to simply compare the node's singular point position against a 2D box)

That would be the most efficient way.

Both SceneKit's physics engine and doing a distance calculation, (as gchiste proposed), would be heavier than comparing box points. - And it is not much effort to compare points like that.
Trigger an event when SCNode is near a certain transform.
 
 
Q