Garry's Mod Wiki

Revision Difference

Global.WorldToLocal#565517

<function name="WorldToLocal" parent="Global" type="libraryfunc"> <description>Translates the specified position and angle into the specified coordinate system.</description> <description>Translates a worldspace vector and angle into a specific coordinate system.</description> <realm>Shared</realm> <args> <arg name="position" type="Vector">The position that should be translated from the current to the new system.</arg> <arg name="angle" type="Angle">The angles that should be translated from the current to the new system.</arg> <arg name="newSystemOrigin" type="Vector">The origin of the system to translate to.</arg> <arg name="newSystemAngles" type="Angle">The angles of the system to translate to.</arg> <arg name="position" type="Vector">A worldspace vector.</arg> <arg name="angle" type="Angle">A worldspace angle.</arg> <arg name="newSystemOrigin" type="Vector">The origin of the new coordinate system.</arg> <arg name="newSystemAngles" type="Angle">The angles of the new coordinate system.</arg> </args> <rets> <ret name="" type="Vector">Local position</ret>⤶ <ret name="" type="Angle">Local angles</ret>⤶ <ret name="" type="Vector">The correspondent local space `position`</ret>⤶ <ret name="" type="Angle">The correspondent local space `angle`</ret>⤶ </rets> </function> <example> <description>Matrix math which showcases how this is calculated internally.</description> <description>A matrix math that shows how this is calculated internally.</description> <code> local worldTransform = Matrix() worldTransform:SetTranslation(position) worldTransform:SetAngles(angle) worldTransform:SetTranslation( position ) worldTransform:SetAngles( angle ) local objectTransform = Matrix() objectTransform:SetTranslation(newSystemOrigin) objectTransform:SetAngles(newSystemAngles) objectTransform:SetTranslation( newSystemOrigin ) objectTransform:SetAngles( newSystemAngles ) -- Transform the world coordinates using the object transform as an inverted transformation matrix local worldToLocal = objectTransform:GetInverse() * worldTransform print(worldToLocal:GetTranslation(), worldToLocal:GetAngles()) print( worldToLocal:GetTranslation(), worldToLocal:GetAngles() ) </code> </example>