GetDistanceBetweenPoints3D: Difference between revisions
Jump to navigation
Jump to search
m (Example fix) |
|||
(9 intermediate revisions by 2 users not shown) | |||
Line 18: | Line 18: | ||
Returns a float containing the distance between the two points as a [[float]]. Returns ''false'' if an argument passed was invalid. | Returns a float containing the distance between the two points as a [[float]]. Returns ''false'' if an argument passed was invalid. | ||
==Example== | |||
This example gets the distance between two vehicles and outputs it to the chat box. | |||
<section name="Server" class="server" show="true"> | |||
<syntaxhighlight lang="lua"> | |||
-- create the vehicles which we're going to measure distance between of | |||
vehicle1 = createVehicle(445, -2629.79248, 1370.82996, 7.10079) | |||
vehicle2 = createVehicle(560, -2629.71899, 1350.18188, 7.10897) | |||
-- get position of both created vehicles | |||
vehicle1x, vehicle1y, vehicle1z = getElementPosition(vehicle1) | |||
vehicle2x, vehicle2y, vehicle2z = getElementPosition(vehicle2) | |||
-- measure the distance | |||
outputChatBox("The distance between vehicle1 and vehicle2 is " ..tostring(getDistanceBetweenPoints3D(vehicle1x, vehicle1y, vehicle1z, vehicle2x, vehicle2y, vehicle2z))) | |||
</syntaxhighlight> | |||
</section> | |||
This example checks whether or not the player is close enough (5 meters from a location of SF Bridge) | |||
<section name="Server" class="server" show="true"> | |||
<syntaxhighlight lang="lua"> | |||
function checkIfClose(p, cmd) | |||
-- player x, y, z | |||
local x1, y1, z1 = getElementPosition(p) | |||
-- location x, y, z (to check if player is close enough to) | |||
local x2, y2, z2 = -2629.79248, 1370.82996, 7.10079 | |||
if getDistanceBetweenPoints3D(x1, y1, z1, x2, y2, z2) <= 5 then | |||
return outputChatBox("You are close enough (within 5 meters)!") | |||
else | |||
return outputChatBox("You are NOT close enough!") | |||
end | |||
end | |||
addCommandHandler("closeornot", checkIfClose) | |||
--[REWRITE BY ANDREI] | |||
local half = 1 / 2 | |||
function getDistanceFromPoints(x1, y1, z1, x2, y2, z2) | |||
return ((x2 - x1) * (x2 - x1) ^ 2 + (y2 - y1) * (y2 - y1) ^ 2 + (z2 - z1) * (z2 - z1)) ^ half | |||
end | |||
</syntaxhighlight> | |||
</section> | |||
''getDistanceBetweenPoints3D'' can also be used to measure the length of 3 dimensional vectors. This example calculates the speed of a vehicle by measuring the size of the it's velocity vector: | |||
<syntaxhighlight lang="lua"> | |||
speed = getDistanceBetweenPoints3D ( 0, 0, 0, getElementVelocity ( vehicle ) ) | |||
</syntaxhighlight> | |||
''Lua note: Using multiple return values as arguments for another function can only be done at the end of the argument list.'' | |||
==See Also== | ==See Also== | ||
{{Utility functions}} | {{Utility functions}} |
Latest revision as of 18:11, 13 August 2024
This function returns the distance between two 3 dimensional points using the pythagorean theorem.
Syntax
float getDistanceBetweenPoints3D ( float x1, float y1, float z1, float x2, float y2, float z2 )
Required Arguments
- x1: The X position of the first point
- y1: The Y position of the first point
- z1: The Z position of the first point
- x2: The X position of the second point
- y2: The Y position of the second point
- z2: The Z position of the second point
Returns
Returns a float containing the distance between the two points as a float. Returns false if an argument passed was invalid.
Example
This example gets the distance between two vehicles and outputs it to the chat box.
Click to collapse [-]
Server-- create the vehicles which we're going to measure distance between of vehicle1 = createVehicle(445, -2629.79248, 1370.82996, 7.10079) vehicle2 = createVehicle(560, -2629.71899, 1350.18188, 7.10897) -- get position of both created vehicles vehicle1x, vehicle1y, vehicle1z = getElementPosition(vehicle1) vehicle2x, vehicle2y, vehicle2z = getElementPosition(vehicle2) -- measure the distance outputChatBox("The distance between vehicle1 and vehicle2 is " ..tostring(getDistanceBetweenPoints3D(vehicle1x, vehicle1y, vehicle1z, vehicle2x, vehicle2y, vehicle2z)))
This example checks whether or not the player is close enough (5 meters from a location of SF Bridge)
Click to collapse [-]
Serverfunction checkIfClose(p, cmd) -- player x, y, z local x1, y1, z1 = getElementPosition(p) -- location x, y, z (to check if player is close enough to) local x2, y2, z2 = -2629.79248, 1370.82996, 7.10079 if getDistanceBetweenPoints3D(x1, y1, z1, x2, y2, z2) <= 5 then return outputChatBox("You are close enough (within 5 meters)!") else return outputChatBox("You are NOT close enough!") end end addCommandHandler("closeornot", checkIfClose) --[REWRITE BY ANDREI] local half = 1 / 2 function getDistanceFromPoints(x1, y1, z1, x2, y2, z2) return ((x2 - x1) * (x2 - x1) ^ 2 + (y2 - y1) * (y2 - y1) ^ 2 + (z2 - z1) * (z2 - z1)) ^ half end
getDistanceBetweenPoints3D can also be used to measure the length of 3 dimensional vectors. This example calculates the speed of a vehicle by measuring the size of the it's velocity vector:
speed = getDistanceBetweenPoints3D ( 0, 0, 0, getElementVelocity ( vehicle ) )
Lua note: Using multiple return values as arguments for another function can only be done at the end of the argument list.
See Also
- addDebugHook
- base64Decode
- base64Encode
- debugSleep
- decodeString
- encodeString
- fromJSON
- generateKeyPair
- getColorFromString
- getDevelopmentMode
- getDistanceBetweenPoints2D
- getDistanceBetweenPoints3D
- getEasingValue
- getNetworkStats
- getNetworkUsageData
- getPerformanceStats
- getRealTime
- getTickCount
- getTimerDetails
- getTimers
- getFPSLimit
- getUserdataType
- getVersion
- gettok
- isTransferBoxVisible
- setTransferBoxVisible
- hash
- inspect
- interpolateBetween
- iprint
- isOOPEnabled
- isTimer
- killTimer
- md5
- passwordHash
- passwordVerify
- pregFind
- pregMatch
- pregReplace
- removeDebugHook
- resetTimer
- setDevelopmentMode
- setFPSLimit
- setTimer
- ref
- deref
- sha256
- split
- teaDecode
- teaEncode
- toJSON
- tocolor
- getProcessMemoryStats
- utfChar
- utfCode
- utfLen
- utfSeek
- utfSub
- bitAnd
- bitNot
- bitOr
- bitXor
- bitTest
- bitLRotate
- bitRRotate
- bitLShift
- bitRShift
- bitArShift
- bitExtract
- bitReplace