This object allows you to perform collision tests against the physics objects in a PhysicsWorld.
Raycast objects are full userdata objects. That means:
You can carry out raycasts in three different ways, depending on how you want to retrieve the results.
Use this option if you only need to conduct a single raycast, and you want the results immediately.
In this scenario, call the stingray.PhysicsWorld.raycast() function. You must specify the starting point, direction and maximum length of the ray, as well as parameters that control the collision test (see Raycast Parameters below).
For example:
local foundCollision, collisionPos, distance, normal, actor = stingray.PhysicsWorld.raycast(my_world, start, dir, "closest")
In this example, the raycast is called with the "closest" parameter, which means that the function will return detailed information about the first collision point (if any).
Use this option if you want to carry out multiple raycasts with the same parameters (see Raycast Parameters below), and you need to handle the results immediately. This option is typically more efficient than calling stingray.PhysicsWorld.raycast() multiple times in sequence, since you are re-using the same Raycast object.
In this scenario, create a new Raycast object by calling stingray.PhysicsWorld.make_raycast(), omitting the callback parameter entirely. Then call the stingray.Raycast.cast() function of the new object. For example:
local raycastObj = stingray.PhysicsWorld.make_raycast(my_world, "all") local collisionResultsA = raycastObj:cast(start, dirA, length) local collisionResultsB = raycastObj:cast(start, dirB, length) ...
In this example, because the Raycast is initialized with the "all" parameter, the collisionResultsA and collisionResultsB variables are each set to an array of collision_hit data tables.
Use this option if you want to carry out multiple raycasts, and you can defer handling their results to a later frame.
In this scenario:
For example:
local raycastObj = stingray.PhysicsWorld.make_raycast(my_world, my_raycast_callback, "all", "types", "statics") raycastObj:cast(start, dirA, length) raycastObj:cast(start, dirB, length) ...
In this case, because the Raycast is initialized with the "all" parameter, the my_raycast_callback function will be passed an array of collision_hit data tables.
When you create a new Raycast object using stingray.PhysicsWorld.make_raycast(), or when you carry out a single synchronous raycast by calling stingray.PhysicsWorld.raycast(), you can specify some parameters that control the behavior of the collision test and the kinds of results that are produced.
The accepted parameter values are:
"any": Use this value if you only want to know whether or not the ray hit anything. When you use this option, the raycast produces only a single boolean value that indicates whether or not a collision was detected.
"all": Use this value if you want details on every collision along the path of the ray. When you use this option, the raycast produces a table that contains an array of collision_hit tables, each of which records one collision point along the ray.
"closest": Use this value if you only care about the first collision encountered by the ray. This is the default mode. When you use this option, the raycast produces several values:
"types": Determines the types of objects that should be considered for collision. If you specify this parameter, you must follow it with an additional string parameter that specifies the types. "statics" specifies that the ray should only test against static objects. "dynamics" specifies that the ray should only test against dynamic objects. "both" specifies that the ray should test against both static and dynamic objects; this is the default value.
"collision_filter": This parameter allows you to specify a collision filter that the ray should use to determine which objects it collides with. If you specify this parameter, you must follow it with an additional string parameter that specifies the name of the collision filter, as defined in your global.physics_properties data file. By default, the ray collides with all objects in the scene.
"mesh_both_sides" Add this parameter if you want the raycast to also hit back facing triangles in a physics mesh.