GetScreenFromWorldPosition: Difference between revisions

From Multi Theft Auto: Wiki
Jump to navigation Jump to search
mNo edit summary
(Undo revision 76324 by Deihim007 (talk) it returns 3 values)
Tag: Undo
 
(12 intermediate revisions by 10 users not shown)
Line 5: Line 5:
==Syntax==
==Syntax==
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
float float float getScreenFromWorldPosition ( float x, float y, float z, [ float edgeTolerance=0, bool relative=true ] )
float, float, float getScreenFromWorldPosition ( float x, float y, float z [, float edgeTolerance = 0.0, bool relative = true ] )
</syntaxhighlight>
</syntaxhighlight>


Line 16: Line 16:
{{OptionalArg}}
{{OptionalArg}}
{{New feature/item|3|1.0||
{{New feature/item|3|1.0||
*'''edgeTolerance:''' A [[float]] value indicating the distance the position can be off screen before the function returns false.
*'''edgeTolerance:''' A [[float]] value indicating the distance the position can be off screen before the function returns false. Note: it's clamped down on both axies to the size of screen at the given axis*10
*'''relative:''' A [[boolean]] value that indicates if edgeTolerance is in pixels [false], or relative to the screen size [true].
*'''relative:''' A [[boolean]] value that indicates if edgeTolerance is in pixels [false], or relative to the screen size [true].
}}
}}
Line 22: Line 22:
===Returns===
===Returns===
Returns two ''x'', ''y'' [[float]]s indicating the screen position and [[float]] distance between screen and given position if successful, ''false'' otherwise.
Returns two ''x'', ''y'' [[float]]s indicating the screen position and [[float]] distance between screen and given position if successful, ''false'' otherwise.
===Example===
<section class="client" name="Client-side Script" show="true">
This example add a '3d' text at coordinates 0, 0, 0 (center of map).
<syntaxhighlight lang="lua">
addEventHandler ( "onClientRender", root,
function ( )
if ( getDistanceBetweenPoints3D ( 0, 0, 3, getElementPosition ( localPlayer ) ) ) < 50 then
local coords = { getScreenFromWorldPosition ( 0, 0, 3 ) }
if coords[1] and coords[2] then
dxDrawText ( "Hello !", coords[1], coords[2], coords[1], coords[2], tocolor(255,255,255), 1, "default-bold" )
end
end
end )
</syntaxhighlight>
</section>


==See Also==
==See Also==
{{Client_world_functions}}
{{Client_world_functions}}

Latest revision as of 17:29, 2 April 2023

This function gets the screen position of a point in the world. This is useful for attaching 2D gui elements to parts of the world (e.g. players) or detecting if a point is on the screen (though it does not check if it is actually visible, you should use processLineOfSight for that).

Syntax

float, float, float getScreenFromWorldPosition ( float x, float y, float z [, float edgeTolerance = 0.0, bool relative = true ] )

Required Arguments

  • x: A float value indicating the x position in the world.
  • y: A float value indicating the y position in the world.
  • z: A float value indicating the z position in the world.

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.

  • edgeTolerance: A float value indicating the distance the position can be off screen before the function returns false. Note: it's clamped down on both axies to the size of screen at the given axis*10
  • relative: A boolean value that indicates if edgeTolerance is in pixels [false], or relative to the screen size [true].

Returns

Returns two x, y floats indicating the screen position and float distance between screen and given position if successful, false otherwise.

Example

Click to collapse [-]
Client-side Script

This example add a '3d' text at coordinates 0, 0, 0 (center of map).

addEventHandler ( "onClientRender", root,
function ( )
	if ( getDistanceBetweenPoints3D ( 0, 0, 3, getElementPosition ( localPlayer ) ) ) < 50 then
		local coords = { getScreenFromWorldPosition ( 0, 0, 3 ) }
		if coords[1] and coords[2] then
			dxDrawText ( "Hello !", coords[1], coords[2], coords[1], coords[2], tocolor(255,255,255), 1, "default-bold" )
		end
	end
end )

See Also