ProcessLineAgainstMesh: Difference between revisions
mNo edit summary |
m (--) |
||
| (One intermediate revision by the same user not shown) | |||
| Line 38: | Line 38: | ||
==Example== | ==Example== | ||
This example prints the name of the texture hit by the player's crosshair on any vehicle in the world. It uses processLineAgainstMesh to check for collisions between a line from the camera to a point in front of it and the meshes of vehicles. | |||
<syntaxhighlight lang="lua"> | |||
addEventHandler("onClientRender", root, | |||
function() | |||
local camX, camY, camZ, targetX, targetY, targetZ = getCameraMatrix() | |||
local dirX = targetX - camX | |||
local dirY = targetY - camY | |||
local dirZ = targetZ - camZ | |||
local endX = camX + dirX * 100 | |||
local endY = camY + dirY * 100 | |||
local endZ = camZ + dirZ * 100 | |||
for _, vehicle in ipairs(getElementsByType("vehicle")) do | |||
local hit, _, _, textureName = processLineAgainstMesh( | |||
vehicle, | |||
camX, camY, camZ, | |||
endX, endY, endZ | |||
) | |||
if hit and textureName then | |||
print("Hit texture: " .. textureName) | |||
break | |||
end | |||
end | |||
end | |||
) | |||
</syntaxhighlight> | |||
==See Also== | ==See Also== | ||
{{Client world functions}} | {{Client world functions}} | ||
Latest revision as of 17:03, 5 April 2026
Does a raycast against an element's renderable mesh model [not the collision model!]. The same functionality is already present in processLineOfSight, but the latter is a little buggy due to the fact that the collision model is always simplified, and not exactly the same as the mesh, which leads to situations where no hit is detected, even though the visible mesh is hittable [or vice versa]. Also, when one is interested in a specific element the overhead is a lot smaller [as we can skip all the collision detection done by the before-mentioned function].
Syntax
Single line for convenience.
bool, float, float, string, string, float, float, float processLineAgainstMesh(element toTest, float startX, float startY, float startZ, float endX, float endY, float endZ)
Return values labelled for ease of reference.
bool -- hit
float float -- texU, texV
string -- textureName
string -- frameName
float float float -- worldX, worldY, worldZ
processLineAgainstMesh(element toTest,
float startX, float startY, float startZ,
float endX, float endY, float endZ)
Required Arguments
- toProcess: The element to process the line against
- startX, startY, startZ: The start [origin] of the line
- endX, endY, endZ: The end of the line
Returns
- hit: true if there is a collision with the given element's mesh, false otherwise [in which case all other values are nil]
- texU, texV: the U, V coordinates on the hit geometry's texture
- textureName: name of the hit geometry's texture
- frameName: hit frame's name
- worldX, worldY, worldZ: collision position in world space
Example
This example prints the name of the texture hit by the player's crosshair on any vehicle in the world. It uses processLineAgainstMesh to check for collisions between a line from the camera to a point in front of it and the meshes of vehicles.
addEventHandler("onClientRender", root,
function()
local camX, camY, camZ, targetX, targetY, targetZ = getCameraMatrix()
local dirX = targetX - camX
local dirY = targetY - camY
local dirZ = targetZ - camZ
local endX = camX + dirX * 100
local endY = camY + dirY * 100
local endZ = camZ + dirZ * 100
for _, vehicle in ipairs(getElementsByType("vehicle")) do
local hit, _, _, textureName = processLineAgainstMesh(
vehicle,
camX, camY, camZ,
endX, endY, endZ
)
if hit and textureName then
print("Hit texture: " .. textureName)
break
end
end
end
)
See Also
- createSWATRope
- getBirdsEnabled
- getCoronaReflectionsEnabled
- getGarageBoundingBox
- getGaragePosition
- getGarageSize
- getGroundPosition
- getInteriorFurnitureEnabled
- getNearClipDistance
- getPedsLODDistance
- getRoofPosition
- getScreenFromWorldPosition
- getVehiclesLODDistance
- getWorldFromScreenPosition
- isAmbientSoundEnabled
- isLineOfSightClear
- isWorldSoundEnabled
- processLineOfSight
- resetAmbientSounds
- resetBlurLevel
- resetColorFilter
- resetCoronaReflectionsEnabled
- resetNearClipDistance
- resetPedsLODDistance
- resetVehiclesLODDistance
- resetWorldSounds
- setAmbientSoundEnabled
- setBirdsEnabled
- setColorFilter
- setCoronaReflectionsEnabled
- setInteriorFurnitureEnabled
- setNearClipDistance
- setPedsLODDistance
- setVehiclesLODDistance
- setWorldSoundEnabled
- testLineAgainstWater
- areTrafficLightsLocked
- getAircraftMaxHeight
- getAircraftMaxVelocity
- getCloudsEnabled
- getFarClipDistance
- getFogDistance
- getGameSpeed
- getGravity
- getHeatHaze
- getInteriorSoundsEnabled
- getJetpackMaxHeight
- getMinuteDuration
- getMoonSize
- getOcclusionsEnabled
- getRainLevel
- getSunColor
- getSunSize
- getTime
- getTrafficLightState
- getWeather
- getWindVelocity
- getSkyGradient
- getZoneName
- isGarageOpen
- removeWorldModel
- resetFarClipDistance
- resetFogDistance
- resetHeatHaze
- resetMoonSize
- resetRainLevel
- resetSkyGradient
- resetSunColor
- resetSunSize
- resetWindVelocity
- restoreAllWorldModels
- restoreWorldModel
- setAircraftMaxHeight
- setAircraftMaxVelocity
- setCloudsEnabled
- setFarClipDistance
- setFogDistance
- setGameSpeed
- setGarageOpen
- setGravity
- setHeatHaze
- setInteriorSoundsEnabled
- setMinuteDuration
- setMoonSize
- setOcclusionsEnabled
- setRainLevel
- setSkyGradient
- setSunColor
- setSunSize
- setTime
- setTrafficLightState
- setTrafficLightsLocked
- setWeather
- setWeatherBlended
- setWindVelocity
- setJetpackMaxHeight