Instance Method

rayTestWithSegmentFromPoint:toPoint:options:

Searches for physics bodies along a line segment between two points in the physics world.

Declaration

- (NSArray<SCNHitTestResult *> *)rayTestWithSegmentFromPoint:(SCNVector3)origin toPoint:(SCNVector3)dest options:(NSDictionary<SCNPhysicsTestOption, id> *)options;

Parameters

origin

An endpoint of the line segment to search, specified in the scene’s world coordinate system.

dest

The other endpoint of the line segment to search, specified in the scene’s world coordinate system.

options

A dictionary of options affecting the test, or nil to use default options. For applicable keys and the possible values, see Physics Test Options Keys.

Return Value

An array of SCNHitTestResult objects describing search results.

Discussion

Use this method to implement concepts such as line of sight in your app. For example, in a game you might implement behavior for an enemy character by searching for physics bodies along a line between the enemy character’s position and the player character’s position, as illustrated below:

// Options: Look only for the closest object along line of sight,
// and use the collision bitmask to avoid finding the enemy itself.
NSDictionary *options = @{ SCNPhysicsTestSearchModeKey : SCNPhysicsTestSearchModeClosest,
                     SCNPhysicsTestCollisionBitMaskKey : @(kMyCategoryPlayer) };
 
NSArray *results = [physicsWorld rayTestWithSegmentFromPoint:enemy.position
                                                     toPoint:player.position
                                                     options:options];
if (results.firstObject.node == player) {
    // Enemy can see player: begin pursuit.
} else {
    // Enemy cannot see player: remain idle.
}

See Also

Searching for Physics Bodies

- convexSweepTestWithShape:fromTransform:toTransform:options:

Searches for physics bodies in the space formed by moving a convex shape through the physics world.