GetDistanceBetweenPoints3D: Difference between revisions

From Multi Theft Auto: Wiki
Jump to navigation Jump to search
No edit summary
No edit summary
Line 2: Line 2:
__NOTOC__
__NOTOC__
This function returns the distance between two 3 dimensional points using the pythagorean theorem.
This function returns the distance between two 3 dimensional points using the pythagorean theorem.
This involves a square root operation and is thus computationally expensive.


==Syntax==
==Syntax==
Line 19: Line 18:


==Example==
==Example==
<section name="Server and client" class="both" show="true">
This example gets the distance between two vehicles and outputs it to the chat box.
This example gets the distance between two vehicles and outputs it to the chat box.
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
Line 26: Line 24:
outputChatBox ( "The distance between vehicle1 and vehicle2 is "..tostring(getDistanceBetweenPoints3D ( vehicle1x, vehicle1y, vehicle1z, vehicle2x,
outputChatBox ( "The distance between vehicle1 and vehicle2 is "..tostring(getDistanceBetweenPoints3D ( vehicle1x, vehicle1y, vehicle1z, vehicle2x,
  vehicle2y, vehicle2z )) )</syntaxhighlight>
  vehicle2y, vehicle2z )) )</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}}

Revision as of 03:34, 22 July 2012

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.

vehicle1x, vehicle1y, vehicle1z = getElementPosition ( vehicle1 )
vehicle2x, vehicle2y, vehicle2z = getElementPosition ( vehicle2 )
outputChatBox ( "The distance between vehicle1 and vehicle2 is "..tostring(getDistanceBetweenPoints3D ( vehicle1x, vehicle1y, vehicle1z, vehicle2x,
 vehicle2y, vehicle2z )) )


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