B2.World:rayCast: Difference between revisions

From GiderosMobile
PaulR (talk | contribs)
No edit summary
No edit summary
 
(4 intermediate revisions by 2 users not shown)
Line 1: Line 1:
__NOTOC__
__NOTOC__
<languages />
'''Available since:''' Gideros 2011.6<br/>
'''<translate>Available since</translate>:''' Gideros 2011.6<br/>
'''Class:''' [[b2.World]]<br/>
'''<translate>Class</translate>:''' [[Special:MyLanguage/b2.World|b2.World]]<br/>
=== <translate>Description</translate> ===
<translate><br />
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. <br />
<br />
Listener function is called for each fixture found in the query and accepts 6 parameters (7 if data parameter is provided):<br />
<br />
1. the fixture hit by the ray<br />
2. the x coordinate of the point of initial intersection <br />
3. the y coordinate of the point of initial intersection<br />
4. the x coordinate of the normal vector at the point of intersection <br />
5. the y coordinate of the normal vector at the point of intersection <br />
6. fraction<br />
<br />
You control how the ray cast proceeds by returning a number:<br />
<br />
- return no value or -1: ignore this fixture and continue<br />
- return 0: terminate the ray cast<br />
- return fraction: clip the ray to this point<br />
- return 1: don&#039;t clip the ray and continue<br />


<br /></translate>
=== Description ===
<source lang="lua">
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)
<syntaxhighlight lang="lua">
</source>
b2.World:rayCast(x1,y1,x2,y2,listener,data)
<br />
</syntaxhighlight>
=== <translate>Parameters</translate> ===
 
'''x1''': (number) <translate>the x coordinate of the ray starting point</translate> <br/>
Listener function is called for each fixture found in the query and accepts 6 parameters (7 if data parameter is provided):
'''y1''': (number) <translate>the y coordinate of the ray starting point</translate> <br/>
#the fixture hit by the ray
'''x2''': (number) <translate>the x coordinate of the ray ending point</translate> <br/>
#the x coordinate of the point of initial intersection
'''y2''': (number) <translate>the y coordinate of the ray ending point</translate> <br/>
#the y coordinate of the point of initial intersection
'''listener''': (function) <translate>the listener function that processes the results</translate> <br/>
#the x coordinate of the normal vector at the point of intersection
'''data''': (any) <translate>an optional data parameter that is passed as a first argument to the listener function</translate> '''optional'''<br/>
#the y coordinate of the normal vector at the point of intersection
<br />
#fraction
=== <translate>Examples</translate> ===
 
'''Detecting bodies with raycasting'''<br/>
You control how the ray cast proceeds by returning a number:
<source lang="lua">local raycastCallback function(fixture, hitX, hitY, vectX, vectY, fration)
*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 <br/>
'''y1''': (number) the y coordinate of the ray starting point <br/>
'''x2''': (number) the x coordinate of the ray ending point <br/>
'''y2''': (number) the y coordinate of the ray ending point <br/>
'''listener''': (function) the listener function that processes the results <br/>
'''data''': (any) an optional data parameter that is passed as a first argument to the listener function '''optional'''<br/>
 
=== Example ===
'''Detecting bodies with raycasting'''
<syntaxhighlight lang="lua">
local raycastCallback function(fixture, hitX, hitY, vectX, vectY, fraction)
     --so if this function is called, it means we hit some kind of object
     --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"
     --and its fixture is stored in first variable we named "fixture"
     --so we can for example get body
     --so we can for example get body
     local body = fixture:getBody()
     local body = fixture:getBody()
end
end
   
   
Line 54: Line 49:
--callback function
--callback function
local x, y = body:getPosition()
local x, y = body:getPosition()
world:rayCast(x, y, x, y-100, raycastCallback)</source>
world:rayCast(x, y, x, y-100, raycastCallback)
</syntaxhighlight>
 
{{B2.World}}

Latest revision as of 20: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):

  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

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)





LiquidFun