Convert 3d position to 2d relative position

As you can see from the image, the 2D plane is the black part, and the 3D position is the client’s mouse hit position. Subtracting the position from the top left corner highlighted in red from the mouse hit position works fine but does not work for objects that are not at a perfect 90-degree angle.

1 Like

https://create.roblox.com/docs/reference/engine/classes/Camera#WorldToScreenPoint

local camera = workspace.CurrentCamera
local worldPoint = Vector3.new(0, 10, 0)
local vector, onScreen = camera:WorldToScreenPoint(worldPoint)

local screenPoint = Vector2.new(vector.X, vector.Y)
local depth = vector.Z

This won’t work as this uses the client’s camera.

As far as I know thats the only way to convert a 3D position to a 2D relative position though, what exactly are you trying to do?

Do you want the 2D position on the SurfaceGui from the mouse position in the world?

1 Like

My main goal is to make a gui element draggable on a surface gui.

This requires a lot of math, I suggest hoping that this works:

frame.Draggable = true
frame.Selectable = true
frame.Active = true
-- Run this on the frame you want to be draggable in the UI

This will of course work, but it’s deprecated, and when the mouse moves too fast, the frame just stops following.

Well if you can’t find a solution, that’s the only way (if it works).

In the past, I made a module to get a 2D position on a SurfaceGui from a raycast, but I don’t have it on me right now. This was for a VR game.

1 Like

Here’s what you need to get the position of the mouse on the GUI. Hope it all works out.

local relative = (-surface.CFrame:PointToObjectSpace(mousePos) + surface.Size/2) * pixelsPerStud

This will give you a Vector3 scaled to the amount of pixels on the GUI. Depending on which side of the surface your GUI is on, you’ll need to use different axes of the returned Vector3 and you might need to make them negative. For testing I used a SurfaceGui on the ‘Front’ face.

5 Likes

I already have it working, but like I said, it does not really work properly if the screen is not at a perfect 90-degree angle.

Maybe setting the rotation of the object in memory to a perfect 90-degree and set the position from where the mouse hit was then adding the offset, this could maybe work.

I see that this works perfectly; I assumed I would have to perform some complicated math to make it work, but instead I can simply use this. Thank you so much!

1 Like

To be fair, Linear Algebra is college-level math. Roblox just does it for us.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.