Created
February 18, 2021 17:10
-
-
Save Rubylium/e8f02a5b313355cc74392747249ec856 to your computer and use it in GitHub Desktop.
FiveM entity selection using NUI mouse
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- usage: local hit, endCoords, surfaceNormal, entityHit, entityType, direction = ScreenToWorld(flags, toIgnore) | |
function ScreenToWorld(flags, toIgnore) | |
local camRot = GetGameplayCamRot(0) | |
local camPos = GetGameplayCamCoord() | |
local posX = GetControlNormal(0, 239) | |
local posY = GetControlNormal(0, 240) | |
local cursor = vector2(posX, posY) | |
local cam3DPos, forwardDir = ScreenRelToWorld(camPos, camRot, cursor) | |
local direction = camPos + forwardDir * raycastLength | |
local rayHandle = StartShapeTestRay(cam3DPos, direction, flags, toIgnore, 0) | |
local _, hit, endCoords, surfaceNormal, entityHit = GetShapeTestResult(rayHandle) | |
if entityHit >= 1 then | |
entityType = GetEntityType(entityHit) | |
end | |
return hit, endCoords, surfaceNormal, entityHit, entityType, direction | |
end | |
function ScreenRelToWorld(camPos, camRot, cursor) | |
local camForward = RotationToDirection(camRot) | |
local rotUp = vector3(camRot.x + 1.0, camRot.y, camRot.z) | |
local rotDown = vector3(camRot.x - 1.0, camRot.y, camRot.z) | |
local rotLeft = vector3(camRot.x, camRot.y, camRot.z - 1.0) | |
local rotRight = vector3(camRot.x, camRot.y, camRot.z + 1.0) | |
local camRight = RotationToDirection(rotRight) - RotationToDirection(rotLeft) | |
local camUp = RotationToDirection(rotUp) - RotationToDirection(rotDown) | |
local rollRad = -(camRot.y * pi / 180.0) | |
local camRightRoll = camRight * cos(rollRad) - camUp * sin(rollRad) | |
local camUpRoll = camRight * sin(rollRad) + camUp * cos(rollRad) | |
local point3DZero = camPos + camForward * 1.0 | |
local point3D = point3DZero + camRightRoll + camUpRoll | |
local point2D = World3DToScreen2D(point3D) | |
local point2DZero = World3DToScreen2D(point3DZero) | |
local scaleX = (cursor.x - point2DZero.x) / (point2D.x - point2DZero.x) | |
local scaleY = (cursor.y - point2DZero.y) / (point2D.y - point2DZero.y) | |
local point3Dret = point3DZero + camRightRoll * scaleX + camUpRoll * scaleY | |
local forwardDir = camForward + camRightRoll * scaleX + camUpRoll * scaleY | |
return point3Dret, forwardDir | |
end | |
function RotationToDirection(rotation) | |
local x = rotation.x * pi / 180.0 | |
--local y = rotation.y * pi / 180.0 | |
local z = rotation.z * pi / 180.0 | |
local num = abs(cos(x)) | |
return vector3((-sin(z) * num), (cos(z) * num), sin(x)) | |
end | |
function World3DToScreen2D(pos) | |
local _, sX, sY = GetScreenCoordFromWorldCoord(pos.x, pos.y, pos.z) | |
return vector2(sX, sY) | |
end |
denisjecar1
commented
Dec 16, 2021
•
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment