Difference between revisions of "B2.World:rayCast"

From GiderosMobile
Line 18: Line 18:
 
You control how the ray cast proceeds by returning a number:<br />
 
You control how the ray cast proceeds by returning a number:<br />
 
<br />
 
<br />
&lt;ul&gt;<br />
+
- return no value or -1: ignore this fixture and continue<br />
&lt;li&gt;return no value or -1: ignore this fixture and continue&lt;/li&gt;<br />
+
- return 0: terminate the ray cast<br />
&lt;li&gt;return 0: terminate the ray cast&lt;/li&gt;<br />
+
- return fraction: clip the ray to this point<br />
&lt;li&gt;return fraction: clip the ray to this point&lt;/li&gt;<br />
+
- return 1: don&#039;t clip the ray and continue<br />
&lt;li&gt;return 1: don&#039;t clip the ray and continue&lt;/li&gt;<br />
+
 
&lt;/ul&gt;<br />
 
<br />
 
 
<br /></translate>
 
<br /></translate>
 
<source lang="lua">
 
<source lang="lua">
 
  b2.World:rayCast(x1,y1,x2,y2,listener,data)
 
  b2.World:rayCast(x1,y1,x2,y2,listener,data)
 
</source>
 
</source>
 +
<br />
 
=== <translate>Parameters</translate> ===
 
=== <translate>Parameters</translate> ===
 
'''x1''': (number) <translate>the x coordinate of the ray starting point</translate> <br/>
 
'''x1''': (number) <translate>the x coordinate of the ray starting point</translate> <br/>
Line 36: Line 35:
 
'''listener''': (function) <translate>the listener function that processes the results</translate> <br/>
 
'''listener''': (function) <translate>the listener function that processes the results</translate> <br/>
 
'''data''': (any) <translate>an optional data parameter that is passed as a first argument to the listener function</translate> '''optional'''<br/>
 
'''data''': (any) <translate>an optional data parameter that is passed as a first argument to the listener function</translate> '''optional'''<br/>
 +
<br />
 
=== <translate>Examples</translate> ===
 
=== <translate>Examples</translate> ===
 
'''Detecting bodies with raycasting'''<br/>
 
'''Detecting bodies with raycasting'''<br/>

Revision as of 14:06, 5 October 2018


Available since: Gideros 2011.6
Class: b2.World

Description


Ray-cast 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.

Listener function is called for each fixture found in the query and accepts 6 parameters (7 if data parameter is provided):

1. the fixture hit by the ray
2. the x coordinate of the point of initial intersection
3. the y coordinate of the point of initial intersection
4. the x coordinate of the normal vector at the point of intersection
5. the y coordinate of the normal vector at the point of intersection
6. 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


 b2.World:rayCast(x1,y1,x2,y2,listener,data)


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

Examples

Detecting bodies with raycasting

local raycastCallback function(fixture, hitX, hitY, vectX, vectY, fration)
    --so if this function is called, it means we hit some kind of object
    --and it's 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)