Help with CFrame math

I am making a side project which is a horror / puzzle type of game, but there’s one problem which is stopping me from developing further as I want to fix this. So, I have 3 beakers, which are aligned, but when I add another beaker, it doesn’t center properly. Here’s a video of the issue:


link if video above doesn’t work (https://streamable.com/wlh0i)

here’s the code for creating the beakers:

Code
local num_beakers = i.min(#beaker_model:GetChildren() + 1, #self.beakers)
local spacing = 2.5

created = created + 1

self.beaker = assets.beaker:Clone()
self.beaker.Name = self.beakers[num_beakers].name
self.beaker.Anchored = true
self.beaker.CastShadow = false
self.beaker.CFrame = cf * i.cf(self.last_split * (created / num_beakers) - spacing, 0, 0)
self.beaker.Parent = beakers
self.beaker.liquid.Color = self.beakers[num_beakers].color

self.last_split = self.last_split + spacing
self.beakers[num_beakers].obj = self.beaker

any help would be greatly appreciated.

2 Likes

When you want the beakers to be aligned, does it mean it mean that the middle of the 4 beakers matches with the middle of the camera or the middle of the table?

In the video, the middle of the beakers seem to be aligned with the middle of the camera, but the beakers are not aligned with the middle of the table. I noticed that the table moved a few studs to the left (relative to the camera) between the two setups. See if the position of table changed to check if it moved.
Hope this helps!

1 Like

maybe set your spacing to a different value whenever a new one is added. Because the spacing looks like it’s the same in the video the first test and the second one.

I would like it to be centered in the middle of the table, and the table did not move at all during the video, so that’s not a problem.

It has to be set to that, or else it wouldn’t look right.

You probably set up your spacing formula wrong. Ideally, every beaker should be equally spaced while also staying on the table. You can get all the beakers to align properly by finding the amount they should be spaced apart.

local tableLength = 10
local beakerNum = 4

beakerSpacing = tableLength/(beakerNum-1)

local cfDirection = CFrame.new(1,0,0) -- the direction your beakers are lined up in, make sure it's a unitVector or your spacings will be messed up

for i=1,beakerNum do
    local beaker = beakers[i]
    beaker.CFrame = initialCFrame + (i-1)*beakerSpacing*cfDirection
end

Using beakerSpacing, you can apply the positions correctly.

my bad for the last post, it looks like it’s working, it’s just not aligned

What does your code look like? Assuming the leftmost beaker is the first one, you have your initialCFrame set wrong, it should be the leftmost point on the table that you want the beaker to rest.

I have a PrimaryPart in the center of the table, I want it to center that. But here’s my code

also, the initialCF is just PrimaryPart.CFrame

local num_beakers = i.min(#beaker_model:GetChildren() + 1, #self.beakers)

--// setup beakers
created = created + 1

local spacing = menu_table:GetExtentsSize().X / (#self.beakers - 1)

self.beaker = assets.beaker:Clone()
self.beaker.Name = self.beakers[num_beakers].name
self.beaker.Anchored = true
self.beaker.CastShadow = false
self.beaker.CFrame = cf + (created - 1) * spacing * Vector3.new(0, 0, 1) 
self.beaker.Parent = beakers
self.beaker.liquid.Color = self.beakers[num_beakers].color
self.beakers[num_beakers].obj = self.beaker

If you’re centering around a primary part then your formula will be mostly the same, you’ll just want to apply it differently

for i=1,beakerNum do
    local startPoint = initialCFrame - cfDirection*(tableLength/2)
    -- initialCFrame is the middle of the table
    local beaker = beakers[i]
    beaker.CFrame = startPoint + (i-1)*beakerSpacing*cfDirection
end
1 Like

thanks a lot man, I’m not very good at CFrame math and such, so this was a big help.

1 Like