How do you find X/Y offset Size of a part

Right, I was trying to find the coordinates of a part and I needed the Size X offset. Well, this didn’t work at all. For a GUI, you’re quite lucky since you get the offset values in the X value but for this, I couldn’t get anything. I tried searching all around but they were either unsolved or totally unrelated. How do you find the offset X of a part?

I don’t think parts have offset/scale, are you looking for part.Size.X?

1 Like

Yes, I’m looking for part.Size.X but the negative X values(instead of right, it’s left)

Basically, X is right and I want the offset so it’ll now be -X is left since it’s the inverse. It’s hard to explain.

How about you explain what you’re doing and why you need it instead then?

Basically, I just want the negative X values(so I want the left values).

The negative X size is -part.Size.X
Surely that’s not what you want though, right? That line of code is useless in just about every scenario that you might be hung up on. What do you need this for?

I’m making a mathmodule and I need the coordinates.

The coordinates of the part are simply part.Position

1 Like

I’m trying to find the coordinates of a part on a part

then it’s just the coordinates in it’s relative space. have you heard of the term “free vector”? Literally just do local v = p + -part.Size*1 → local v = p + Vector2.new(-part.Size.X, part.Size.Y)

1 Like

The coordinates of the corners of the part? Here are the locations of all six sides and eight corners.

local origin = part.CFrame
local sideXp, sideXn = origin*CFrame.new(part.Size.X/2,0,0), origin*CFrame.new(part.Size.X/-2,0,0)
local sideYp, sideYn = origin*CFrame.new(0,part.Size.Y/2,0), origin*CFrame.new(0,part.Size.Y/-2,0)
local sideZp, sideZn = origin*CFrame.new(0,0,part.Size.Z/2), origin*CFrame.new(0,0,part.Size.Z/-2)
local cornerXpYpZp = orign*CFrame.new(part.Size.X/2, part.Size.Y/2, part.Size.Z/2)
local cornerXpYpZn = orign*CFrame.new(part.Size.X/2, part.Size.Y/2, part.Size.Z/-2)
local cornerXpYnZp = orign*CFrame.new(part.Size.X/2, part.Size.Y/-2, part.Size.Z/2)
local cornerXpYnZn = orign*CFrame.new(part.Size.X/2, part.Size.Y/-2, part.Size.Z/-2)
local cornerXnYpZp = orign*CFrame.new(part.Size.X/-2, part.Size.Y/2, part.Size.Z/2)
local cornerXnYpZn = orign*CFrame.new(part.Size.X/-2, part.Size.Y/2, part.Size.Z/-2)
local cornerXnYnZp = orign*CFrame.new(part.Size.X/-2, part.Size.Y/-2, part.Size.Z/2)
local cornerXnYnZn = orign*CFrame.new(part.Size.X/-2, part.Size.Y/-2, part.Size.Z/-2)

I think @exp_lol123 is asking about how to get the 2D size of a part on screen?

You can do that like this: How do I detect a clients camera seeing an object? - #2 by nicemike40

Basically

  1. Find the corners of a part
  2. Project them onto the screen with WorldToScreenPoint
  3. Find the bounding box from those
local function BoundingBox2d(camera, part)
	-- 1. get all eight points in 3D space for the part

	local pos, front, right, up = part.Position, part.CFrame.LookVector, part.CFrame.RightVector, part.CFrame.UpVector
	local hx, hy, hz = part.Size.X / 2, part.Size.Y / 2, part.Size.Z / 2

	local points = {
		pos - front * hz - right * hx - up * hy,
		pos - front * hz - right * hx + up * hy,
		pos - front * hz + right * hx - up * hy,
		pos - front * hz + right * hx + up * hy,
		pos + front * hz - right * hx - up * hy,
		pos + front * hz - right * hx + up * hy,
		pos + front * hz + right * hx - up * hy,
		pos + front * hz + right * hx + up * hy,
	}

	-- 2. compute AABB of the part's bounds on screen

	local minX, maxX, minY, maxY = math.huge, -math.huge, math.huge, -math.huge

	for i = 1, 8 do
		local screenPoint = camera:WorldToScreenPoint(points[i])
		local x, y = screenPoint.X, screenPoint.Y

		if screenPoint.Z > 0 then
			if x < minX then minX = x end
			if x > maxX then maxX = x end
			if y < minY then minY = y end
			if y > maxY then maxY = y end
		end
	end

	-- create rect
	return Rect.new(minX, minY, maxX, maxY)
end

Usage example:

image

local frame = script.Parent

local part = workspace.Part
local camera = workspace.CurrentCamera

game:GetService("RunService"):BindToRenderStep("Adjust", Enum.RenderPriority.Last.Value, function()

	local rect = BoundingBox2d(camera, part)

	-- see https://developer.roblox.com/en-us/api-reference/datatype/Rect

	frame.Position = UDim2.fromOffset(rect.Min.X, rect.Min.Y)
	frame.Size = UDim2.fromOffset(rect.Width, rect.Height)
end)

image

2 Likes

You know more about dev forum then me i bet how do i make it where i don’t get notifications from other topics from you lol

That wasn’t really what I wanted but honestly, I feel bad for saying that since you put a lot of effort into this reply.

Are you following him? You can go to his devforum profile and turn that off. I’ve never followed anyone though so I can’t say that this is the issue.

No worries already had it written :slight_smile:

And @ThatLazyBucket lol stop following me