Difference between revisions of "B2.World:rayCast"
m (Text replacement - "</source>" to "</syntaxhighlight>") |
|||
Line 1: | Line 1: | ||
__NOTOC__ | __NOTOC__ | ||
− | + | '''Available since:''' Gideros 2011.6<br/> | |
− | ''' | + | '''Class:''' [[b2.World]]<br/> |
− | ''' | ||
− | === | + | === Description === |
Ray-casts the world for all fixtures in the path of the ray. Your callback controls whether you get the closest point, any point, or n-points. The ray-cast ignores shapes that contain the starting point. | Ray-casts the world for all fixtures in the path of the ray. Your callback controls whether you get the closest point, any point, or n-points. The ray-cast ignores shapes that contain the starting point. | ||
+ | <syntaxhighlight lang="lua"> | ||
+ | b2.World:rayCast(x1,y1,x2,y2,listener,data) | ||
+ | </syntaxhighlight> | ||
Listener function is called for each fixture found in the query and accepts 6 parameters (7 if data parameter is provided): | Listener function is called for each fixture found in the query and accepts 6 parameters (7 if data parameter is provided): | ||
Line 20: | Line 22: | ||
*return fraction: clip the ray to this point | *return fraction: clip the ray to this point | ||
*return 1: don't clip the ray and continue | *return 1: don't clip the ray and continue | ||
− | |||
− | |||
− | |||
− | === | + | === Parameters === |
− | '''x1''': (number) | + | '''x1''': (number) the x coordinate of the ray starting point <br/> |
− | '''y1''': (number) | + | '''y1''': (number) the y coordinate of the ray starting point <br/> |
− | '''x2''': (number) | + | '''x2''': (number) the x coordinate of the ray ending point <br/> |
− | '''y2''': (number) | + | '''y2''': (number) the y coordinate of the ray ending point <br/> |
− | '''listener''': (function) | + | '''listener''': (function) the listener function that processes the results <br/> |
− | '''data''': (any) | + | '''data''': (any) an optional data parameter that is passed as a first argument to the listener function '''optional'''<br/> |
− | === | + | === Example === |
'''Detecting bodies with raycasting''' | '''Detecting bodies with raycasting''' | ||
<syntaxhighlight lang="lua"> | <syntaxhighlight lang="lua"> |
Latest revision as of 22:36, 30 April 2025
Available since: Gideros 2011.6
Class: b2.World
Description
Ray-casts the world for all fixtures in the path of the ray. Your callback controls whether you get the closest point, any point, or n-points. The ray-cast ignores shapes that contain the starting point.
b2.World:rayCast(x1,y1,x2,y2,listener,data)
Listener function is called for each fixture found in the query and accepts 6 parameters (7 if data parameter is provided):
- the fixture hit by the ray
- the x coordinate of the point of initial intersection
- the y coordinate of the point of initial intersection
- the x coordinate of the normal vector at the point of intersection
- the y coordinate of the normal vector at the point of intersection
- fraction
You control how the ray cast proceeds by returning a number:
- return no value or -1: ignore this fixture and continue
- return 0: terminate the ray cast
- return fraction: clip the ray to this point
- return 1: don't clip the ray and continue
Parameters
x1: (number) the x coordinate of the ray starting point
y1: (number) the y coordinate of the ray starting point
x2: (number) the x coordinate of the ray ending point
y2: (number) the y coordinate of the ray ending point
listener: (function) the listener function that processes the results
data: (any) an optional data parameter that is passed as a first argument to the listener function optional
Example
Detecting bodies with raycasting
local raycastCallback function(fixture, hitX, hitY, vectX, vectY, fraction)
--so if this function is called, it means we hit some kind of object
--and its fixture is stored in first variable we named "fixture"
--so we can for example get body
local body = fixture:getBody()
end
--now we add callback function for projected raycast above body
--Parameters:
--object x coordinate
--object y coordinate
--projection vector on x axis
--projection vector on y axis
--callback function
local x, y = body:getPosition()
world:rayCast(x, y, x, y-100, raycastCallback)