Have you ever wanted to convert a 3D point on the surface of a tilted part in Roblox Studio into a Vector2 based on its position on the surface area? It can be a tricky task, but fear not, I’ve got a solution for you!
First, let’s go over the problem. If you have a tilted part, using the GetSurfacePosition()
method won’t work, as it only returns the surface position on the un-tilted part. Instead, we need to calculate the position of the point relative to the part’s origin, and then use its orientation to determine its position on the surface area.
Here’s an example implementation of a function that does just that:
function GetSurfacePosition(part,surfacePoint)
local pos = surfacePoint - part.Position
pos = pos * part.CFrame:Inverse()
-- Calculate the normalized position of the point on the surface of the part
local normX = (pos.X + part.Size.X/2)/part.Size.X
local normY = (pos.Y + part.Size.Y/2)/part.Size.Y
-- Convert the normalized position to UDim2 offset
return Vector2.new(normX,normY)
end
This function takes a tilted part
and a 3D point
on its surface, and returns a Vector2 representing the position of the point on the surface area of the part.
The function first calculates the position of the point
relative to the part
’s origin in local space using the inverse of the part
’s orientation. It then calculates the dimensions of the part
.
Next, it calculates the surface normal of the part at the point
by constructing a CFrame with the point
as its origin and a vector pointing up in the Z direction, and then transforming it to local space. The surface normal is the LookVector of this transformed CFrame.
Finally, the function calculates the normalized position of the point
on the surface of the part
based on its X and Y coordinates relative to the surface normal, and converts the normalized position to UV Coordinates.
I hope this helps anyone who’s been struggling with this problem. If you have any questions or feedback, feel free to leave a comment below.