ProcessLineOfSight: Difference between revisions

From Multi Theft Auto: Wiki
Jump to navigation Jump to search
No edit summary
 
(Undo revision 42888 by LuXorioN (talk) - The syntax was separated for easier reference, because this function returns a lot of variables)
(44 intermediate revisions by 17 users not shown)
Line 1: Line 1:
__NOTOC__  
{{Client function}}
This function checks if there is anything between 2 points in the world, and if there is, tells you where and with what.
__NOTOC__
This function casts a ray between two points in the world, and tells you information about the point that was hit, if any. The two positions '''must''' be within the local player's draw distance as the collision data is not loaded outside this area, and the call will just fail as if the ray didn't hit.
 
This function is relatively expensive to call, so over use of this in scripts may have a detrimental effect on performance.
 
This function is useful for checking for collisions and for editor-style scripts. If you wish to find what element is positioned at a particular point on the screen, use this function combined with [[getWorldFromScreenPosition]]. If you wish to just know if something is hit, and don't care about what or where was hit, use [[isLineOfSightClear]].


==Syntax==  
==Syntax==  
bool float float float element processLineOfSight ( float startX, float startY, float startZ, float endX, float endY, float endZ, [ bool checkBuildings, bool checkVehicles, bool checkPeds, bool checkObjects, bool checkDummies, bool seeThroughStuff, bool ignoreSomeObjectsForCamera, bool shootThroughStuff )
Return values labelled for ease of reference.
<syntaxhighlight lang="lua">
bool               -- hit
float float float -- hitX, hitY, hitZ
element           -- hitElement
float float float  -- normalX, normalY, normalZ
int                -- material
float              -- lighting
int                -- piece
int                -- worldModelID
float float float  -- worldModelPositionX,Y,Z
float float float  -- worldModelRotationX,Y,Z
int                -- worldLODModelID
                  processLineOfSight ( float startX, float startY, float startZ,  
                                      float endX, float endY, float endZ,  
                                      [ bool checkBuildings = true,  
                                      bool checkVehicles = true,  
                                      bool checkPlayers = true,  
                                      bool checkObjects = true,  
                                      bool checkDummies = true,  
                                      bool seeThroughStuff = false,  
                                      bool ignoreSomeObjectsForCamera = false,  
                                      bool shootThroughStuff = false,
                                      element ignoredElement = nil,
                                      bool includeWorldModelInformation = false,
                                      bool bIncludeCarTyres ] )
</syntaxhighlight>  
</syntaxhighlight>  


===Required Arguments===  
===Required Arguments===  
<!-- List each argument one per line. This should be the argument's name as in the argument list above, NOT the argument's data type -->
*'''startX:''' The start ''x'' position
*'''argumentName:''' description
*'''startY:''' The start ''y'' position
*'''startZ:''' The start ''z'' position
*'''endX:''' The end ''x'' position
*'''endY:''' The end ''y'' position
*'''endZ:''' The end ''z'' position


<!-- Only include this section below if there are optional arguments -->
===Optional Arguments===  
===Optional Arguments===  
{{OptionalArg}}  
{{OptionalArg}}  
*'''argumentName2:''' description
*'''checkBuildings:''' Allow the line of sight to be blocked by GTA's internally placed buildings, i.e. the world map.
*'''argumentName3:''' description
*'''checkVehicles:''' Allow the line of sight to be blocked by [[Vehicle|vehicles]].
*'''checkPlayers:''' Allow the line of sight to be blocked by [[Player|players]].
*'''checkObjects:''' Allow the line of sight to be blocked by [[Object|objects]].
*'''checkDummies:''' Allow the line of sight to be blocked by GTA's internal dummies.  These are not used in the current MTA version so this argument can be set to ''false''.
*'''seeThroughStuff:''' Allow the line of sight to be blocked by translucent game objects, e.g. glass.
*'''ignoreSomeObjectsForCamera:''' Allow the line of sight to pass through objects that have (K) property enabled in "object.dat" data file. (i.e. Most dynamic objects like boxes or barrels)
*'''shootThroughStuff:''' Allow the line of sight to be blocked by things that can be shot through
*'''ignoredElement:''' Allow the line of sight to pass through a certain specified element. This is usually set to the object you are tracing from so it does not interfere with the results.
*'''includeWorldModelInformation :''' Include the results of hitting a world model.
*'''bIncludeCarTyres :''' Includes car tyre hits.


===Returns===
===Returns===
If theres a collision, the function will return true and the collision position, if the collision is with a game entity, it will return that too, otherwise 'nil'.
*'''hit:''' ''true'' if there is a collision, ''false'' otherwise
If there is not a collision the function will return false.
The other values are only filled if there is a collision, they contain ''nil'' otherwise
*'''hitX, hitY, hitZ:''' collision position
*'''hitElement:''' the MTA element hit if any, ''nil'' otherwise
*'''normalX, normalY, normalZ:''' the normal of the surface hit
*'''material:''' an integer representing the [[Material IDs|GTASA material ID]] of the surface hit when applicable (world, objects)
*'''lighting:''' a float between 0 (fully dark) and 1 (bright) representing the amount of light that the hit building surface will transfer to peds or vehicles that are in contact with it. The value can be affected by the game time of day, usually with a lower (darker) value being returned during the night.
*'''piece:''' an integer representing the part of the element hit if hitElement is a vehicle or a ped/player, ''0'' otherwise.
**For a ped/player, piece represents the body part hit:
{{BodyParts}}
**For vehicles, piece represents the vehicle part hit:
{{VehicleParts}}
*'''worldModelID:''' If includeWorldModelInformation was set to ''true'' and a world model was hit, this will contain the model ID.
*'''worldModelPositionX,Y,Z:''' If worldModelID is set, this will contain the world model position.
*'''worldModelRotationX,Y,Z:''' If worldModelID is set, this will contain the world model rotation.
*'''worldLODModelID:''' If worldModelID is set, this will contain the LOD model ID if applicable.


==Example==  
==Examples==  
<!-- Explain what the example is in a single sentance -->
This example shows how you can tell what position and element the camera is looking at, up to 50 units away.
This example does...
<syntaxhighlight lang="lua">
<!-- Add the code below, an emphasis should be on making it clear, not optimized. You could provide two versions if you wish, one clear and well commented, the other optimized -->
local w, h = guiGetScreenSize ()
local tx, ty, tz = getWorldFromScreenPosition ( w/2, h/2, 50 )
local px, py, pz = getCameraMatrix()
hit, x, y, z, elementHit = processLineOfSight ( px, py, pz, tx, ty, tz )
if hit then
    outputChatBox ( "Looking at " .. x .. ", " .. y .. ", " .. z )
    if elementHit then
        outputChatBox ( "Hit element " .. getElementType(elementHit) )
    end
end
</syntaxhighlight>
This example shows how you can get the surface type a vehicle is on. This is useful if you want to do a script to dirt cars over time. Please note that this function doesn't count if the vehicle is streamed in or not, so expect this function to fail or return incorrect values on unloaded vehicles.
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
--This line does...
function getSurfaceVehicleIsOn(vehicle)
blabhalbalhb --abababa
    if isElement(vehicle) and (isVehicleOnGround(vehicle) or isElementInWater(vehicle)) then -- Is an element and is touching any surface?
--This line does this...
        local cx, cy, cz = getElementPosition(vehicle) -- Get the position of the vehicle
mooo
        local gz = getGroundPosition(cx, cy, cz) - 0.001 -- Get the Z position of the ground the vehicle is on (-0.001 because of processLineOfSight)
        local hit, _, _, _, _, _, _, _, surface = processLineOfSight(cx, cy, cz, cx, cy, gz) -- This will get the material of the thing the car is standing on
        if hit then
            return surface -- If everything is correct, stop executing this function and return the surface type
        end
    end
    return false -- If something isn't correct, return false
end
</syntaxhighlight>
</syntaxhighlight>
==Changelog==
{{ChangelogHeader}}
{{ChangelogItem|1.3.0-9.04273|''bIncludeCarTyres'' argument added}}
{{ChangelogItem|1.3.0-9.04273|''worldModelID'' return value fixed}}
{{ChangelogItem|1.3.0-9.04405|''lighting'' return value fixed}}


==See Also==
==See Also==
<!-- Change FunctionArea to the area that this function is in on the main function list page, e.g. Server, Player, Vehicle etc -->
{{Client world functions}}
{{FunctionArea_functions}}
[[Category:Incomplete]] -- leave this unless you complete the function

Revision as of 17:51, 18 November 2014

This function casts a ray between two points in the world, and tells you information about the point that was hit, if any. The two positions must be within the local player's draw distance as the collision data is not loaded outside this area, and the call will just fail as if the ray didn't hit.

This function is relatively expensive to call, so over use of this in scripts may have a detrimental effect on performance.

This function is useful for checking for collisions and for editor-style scripts. If you wish to find what element is positioned at a particular point on the screen, use this function combined with getWorldFromScreenPosition. If you wish to just know if something is hit, and don't care about what or where was hit, use isLineOfSightClear.

Syntax

Return values labelled for ease of reference.

bool               -- hit
float float float  -- hitX, hitY, hitZ
element            -- hitElement
float float float  -- normalX, normalY, normalZ
int                -- material
float              -- lighting
int                -- piece
int                -- worldModelID
float float float  -- worldModelPositionX,Y,Z
float float float  -- worldModelRotationX,Y,Z
int                -- worldLODModelID
                  processLineOfSight ( float startX, float startY, float startZ, 
                                       float endX, float endY, float endZ, 
                                       [ bool checkBuildings = true, 
                                       bool checkVehicles = true, 
                                       bool checkPlayers = true, 
                                       bool checkObjects = true, 
                                       bool checkDummies = true, 
                                       bool seeThroughStuff = false, 
                                       bool ignoreSomeObjectsForCamera = false, 
                                       bool shootThroughStuff = false, 
                                       element ignoredElement = nil,
                                       bool includeWorldModelInformation = false,
                                       bool bIncludeCarTyres ] )

Required Arguments

  • startX: The start x position
  • startY: The start y position
  • startZ: The start z position
  • endX: The end x position
  • endY: The end y position
  • endZ: The end z position

Optional Arguments

NOTE: When using optional arguments, you might need to supply all arguments before the one you wish to use. For more information on optional arguments, see optional arguments.

  • checkBuildings: Allow the line of sight to be blocked by GTA's internally placed buildings, i.e. the world map.
  • checkVehicles: Allow the line of sight to be blocked by vehicles.
  • checkPlayers: Allow the line of sight to be blocked by players.
  • checkObjects: Allow the line of sight to be blocked by objects.
  • checkDummies: Allow the line of sight to be blocked by GTA's internal dummies. These are not used in the current MTA version so this argument can be set to false.
  • seeThroughStuff: Allow the line of sight to be blocked by translucent game objects, e.g. glass.
  • ignoreSomeObjectsForCamera: Allow the line of sight to pass through objects that have (K) property enabled in "object.dat" data file. (i.e. Most dynamic objects like boxes or barrels)
  • shootThroughStuff: Allow the line of sight to be blocked by things that can be shot through
  • ignoredElement: Allow the line of sight to pass through a certain specified element. This is usually set to the object you are tracing from so it does not interfere with the results.
  • includeWorldModelInformation : Include the results of hitting a world model.
  • bIncludeCarTyres : Includes car tyre hits.

Returns

  • hit: true if there is a collision, false otherwise

The other values are only filled if there is a collision, they contain nil otherwise

  • hitX, hitY, hitZ: collision position
  • hitElement: the MTA element hit if any, nil otherwise
  • normalX, normalY, normalZ: the normal of the surface hit
  • material: an integer representing the GTASA material ID of the surface hit when applicable (world, objects)
  • lighting: a float between 0 (fully dark) and 1 (bright) representing the amount of light that the hit building surface will transfer to peds or vehicles that are in contact with it. The value can be affected by the game time of day, usually with a lower (darker) value being returned during the night.
  • piece: an integer representing the part of the element hit if hitElement is a vehicle or a ped/player, 0 otherwise.
    • For a ped/player, piece represents the body part hit:
  • 3: Torso
  • 4: Ass
  • 5: Left Arm
  • 6: Right Arm
  • 7: Left Leg
  • 8: Right Leg
  • 9: Head
    • For vehicles, piece represents the vehicle part hit:
  • 0: Frame
  • 2: Trunk
  • 3: Hood
  • 4: Rear
  • 5: Front left door
  • 6: Front right door
  • 7: Rear left door
  • 8: Rear right door
  • 13: Front Left tyre
  • 14: Front Right tyre
  • 15: Back Left tyre
  • 16: Back Right tyre

(Other potential IDs haven't been documented yet and might depend on vehicle model)

  • worldModelID: If includeWorldModelInformation was set to true and a world model was hit, this will contain the model ID.
  • worldModelPositionX,Y,Z: If worldModelID is set, this will contain the world model position.
  • worldModelRotationX,Y,Z: If worldModelID is set, this will contain the world model rotation.
  • worldLODModelID: If worldModelID is set, this will contain the LOD model ID if applicable.

Examples

This example shows how you can tell what position and element the camera is looking at, up to 50 units away.

local w, h = guiGetScreenSize ()
local tx, ty, tz = getWorldFromScreenPosition ( w/2, h/2, 50 )
local px, py, pz = getCameraMatrix()
hit, x, y, z, elementHit = processLineOfSight ( px, py, pz, tx, ty, tz )
if hit then
    outputChatBox ( "Looking at " .. x .. ", " .. y .. ", " ..  z )
    if elementHit then
        outputChatBox ( "Hit element " .. getElementType(elementHit) )
    end
end

This example shows how you can get the surface type a vehicle is on. This is useful if you want to do a script to dirt cars over time. Please note that this function doesn't count if the vehicle is streamed in or not, so expect this function to fail or return incorrect values on unloaded vehicles.

function getSurfaceVehicleIsOn(vehicle)
    if isElement(vehicle) and (isVehicleOnGround(vehicle) or isElementInWater(vehicle)) then -- Is an element and is touching any surface?
        local cx, cy, cz = getElementPosition(vehicle) -- Get the position of the vehicle
        local gz = getGroundPosition(cx, cy, cz) - 0.001 -- Get the Z position of the ground the vehicle is on (-0.001 because of processLineOfSight)
        local hit, _, _, _, _, _, _, _, surface = processLineOfSight(cx, cy, cz, cx, cy, gz) -- This will get the material of the thing the car is standing on
        if hit then
            return surface -- If everything is correct, stop executing this function and return the surface type
        end
    end
    return false -- If something isn't correct, return false
end

Changelog

Version Description
1.3.0-9.04273 bIncludeCarTyres argument added
1.3.0-9.04273 worldModelID return value fixed
1.3.0-9.04405 lighting return value fixed

See Also