# Best scaling factor from the 3D world to resolution.

• 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

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
• 1
• 2 weeks later...
• Moderators

Can you add screenshots or a gif or a video to see this code in action ?

What a genius !

• Moderators
Can you add screenshots or a gif or a video to see this code in action ?

Sure thing!

Image1

Image2

Image3

Image4

• Moderators

Nice great job.

Good job IYAMA

• Moderators

```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.

• Moderators

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.

• Moderators

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

Thx for the reminder. =)

• 7 months later...

do you mayby also know something like this but then for costum map objects?

• Moderators
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.