Jump to content

Best scaling factor from the 3D world to resolution.


IIYAMA

Was this usefull?  

25 members have voted

  1. 1. Was this usefull?

    • Maybe, but I do not understand how to use it.
      2
    • No, I do not understand factors.
      1
    • I am not a scripter, but I like to vote...
      2
    • Yes, I was looking for this.
      20


Recommended Posts

  • Moderators
Posted (edited)

This tutorial is not for beginners.

This code will successfully find the player scaling factor, to convert the 3D world units to 2D pixels on your monitor.

The factor 0.622388530006 is probably a standard, but you never know.

Image1

Image2

Image3

Image4

The function that finds this factor.

local screenX,screenY = guiGetScreenSize() 
local scaleFactorBuffer = nil 
function findScaleFactor () 
    if scaleFactorBuffer then 
        return scaleFactorBuffer 
    else 
        local startX,startY,startZ = getWorldFromScreenPosition ( 0,0, 1 ) 
        local endX,endY,endZ = getWorldFromScreenPosition ( screenX,screenY, 1 ) 
        if startX and endX then 
            scaleFactorBuffer = 1/getDistanceBetweenPoints3D(startX,startY,startZ,endX,endY,endZ) 
            return scaleFactorBuffer 
        end 
    end 
    return false   
end 

The set up for the example below:

-- the set up -- 
local imagePosX,imagePosY,imagePosZ = -21.38184, 5.24524, 3.10965 
local imageResolution = 1000  
---------------- 

The example on how to use:

addEventHandler("onClientRender",root, 
function () 
    local scaleFactor = findScaleFactor () 
    if scaleFactor then 
        dxDrawText("scaleFactor: " .. scaleFactor,300,300) -- The scale factor is circa 0.622388530006 (units). 
        local rectangleCenterX,rectangleCenterY = getScreenFromWorldPosition(imagePosX,imagePosY,imagePosZ) 
        if rectangleCenterX and rectangleCenterY then 
         
            local playerX,playerY,playerZ = getElementPosition(getCamera()) 
             
            local size = (imageResolution*scaleFactor)/getDistanceBetweenPoints3D(playerX,playerY,playerZ,imagePosX,imagePosY,imagePosZ)  
             
            dxDrawText("size: " .. size,300,320) 
             
            dxDrawRectangle(rectangleCenterX+size/2,rectangleCenterY+size/2,size,size,tocolor(0,0,200)) 
             
        end 
    end 
end) 

Edited by Guest
  • Like 1

Do you want to improve your Lua programming skills and make less mistakes?   Start with Lua Language Server!   🙀

 

  Useful functions  3x 

  Tutorials  4x 

 

  • 2 weeks later...
  • Moderators
Posted
Can you add screenshots or a gif or a video to see this code in action ?

Sure thing!

Image1

Image2

Image3

Image4

Do you want to improve your Lua programming skills and make less mistakes?   Start with Lua Language Server!   🙀

 

  Useful functions  3x 

  Tutorials  4x 

 

  • Moderators
Posted

local playerX,playerY,playerZ = getElementPosition(localPlayer) 
local playerX,playerY,playerZ = getElementPosition(getCamera()) 

I just saw a little mistake in it or well, it might be more a visual.

In the code on top, I took the position of the localPlayer, but the result would be better when you use the position of the camera.

Do you want to improve your Lua programming skills and make less mistakes?   Start with Lua Language Server!   🙀

 

  Useful functions  3x 

  Tutorials  4x 

 

  • Moderators
Posted

Replace it in the original post then, people who will come here to get your code will just copy and paste from there and probably won't see your fix above.

The rEvolution is coming ...

  • Moderators
Posted

Ah yea, I was worried about losing the votes. But it seems it remains correct.

Thx for the reminder. =)

Do you want to improve your Lua programming skills and make less mistakes?   Start with Lua Language Server!   🙀

 

  Useful functions  3x 

  Tutorials  4x 

 

  • 7 months later...
  • Moderators
Posted
do you mayby also know something like this but then for costum map objects?

You have to scale custom objects in you model editor. MTA can't scale Collisions.

But if you do not care about collisions, this might help you: https://wiki.multitheftauto.com/wiki/Ge ... oundingBox

Do you want to improve your Lua programming skills and make less mistakes?   Start with Lua Language Server!   🙀

 

  Useful functions  3x 

  Tutorials  4x 

 

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...