Convert vector3 position to Udim2

Hello people! I’m trying to make a map that displays all player teammates. But I have an issue, all the posts on the internet would work (I think) but half of my map is beneath (0,0,0) I think that’s why it’s behaving like this.

This is what I have as of right now:

local mapPositionConverter = {}

local minPosition = Vector3.new(-512, 0, -512) -- bottom left
local maxPosition = Vector3.new(-1783.2, 0, -1786.1) -- top right

function mapPositionConverter.CFrameToUdim(cfValue : CFrame)
    local x = (cfValue.Position.X - minPosition.X) / (maxPosition.X - minPosition.X)
    local y = (cfValue.Position.Z - minPosition.Z) / (maxPosition.Z - minPosition.Z)

    return UDim2.new(x, 0, y, 0)
end

return mapPositionConverter

And it’s showing me this:

External Media

(I realised my map was flipped horizontaly wrong, but that doesn’t change anything. It would still show it wrong I think.)

I’m a little confused on what your issue is, can you clarify what you’re experiencing, and what the expected behavior is?

1 Like

Like what @nowodev said, what’s the issue here?

I’m just going to remind you that both x and y have a range between 0 to 1 (wherein .5 is expected to be the center) – and these values would only work if your “dot” GUI has an anchor point of (.5, .5)

I did my own testing, and I was able to make it work with your code – and although I had to modify the y-axis code to make it work for my own usecase) – it seems to work just fine? Here’s the test code I used:

Just keep in mind that the green box on workspace is at a position of Vector3.new(0, 1, 0) – basically (0, 0) in terms of x and z.

local minPosition = Vector3.new(-5, 0, 5) -- bottom left
local maxPosition = Vector3.new(5, 0, -5) -- top right

function CFrameToUdim(cfValue : CFrame)
	local x = (cfValue.Position.X - minPosition.X) / (maxPosition.X - minPosition.X)
	local y = (maxPosition.Z - cfValue.Position.Z) / (maxPosition.Z - minPosition.Z)

	return UDim2.new(x, 0, y, 0)
end

while true do
	script.Parent.Frame.Frame.Position = CFrameToUdim(workspace.TEST.CFrame)
	task.wait()
end

Please take a look in the video I sent. Roblox keeps making my streamable links to a download link, sorry for that. It’s hard for me to explain without you looking at the video.

Please take a look at the video to see what my issue is. I’m sorry that roblox made it as a downloadable attachment and not as a web media player.


Ah, I see the issue now. Please describe it next time though!

Simplest solution would probably be to parent the icon/pointer/thingamajig to the actual map image/frame, so that the scale is correct.

1 Like

If the points in your drawing are correct, try these positions:

local minPosition = Vector3.new(-1783.2, 0, 1776.2) -- bottom left
local maxPosition = Vector3.new(1795, 0, -1786.7) -- top right

2 Likes

Oh I didn’t catch that, that might be the issue (or another contributing part of it).

1 Like

Thank you. I thought I had it parented to the map, but now that I checked I didn’t. Really sorry for wasting your time. I’ll try it now, I’m pretty sure this would fix it as it’s in Scale and not Offset.

1 Like

This did not fix the issue sadly.

Try @Downrest’s point corrections too, I believe you have your coordinates wrong.

1 Like

Ahahah, thank you! I forgot changing one of the variables. Very noobie mistake.

1 Like