# Scaling model for desired distance in between parts

Hi
Let’s say I have this model:

It’s just a model with some parts inside it.
The distance between every closest part is now exactly 1 stud.

In my program, I want to be able to change that distance in between every part. Let’s say I want it to increase to 3 studs. How could I do that in the most efficient manner possible? (I need to do this for a few thousand parts).

What I have thought about, is to just move every part to the left, right, up or down depending on where it is located relative to it’s neighbours. How could I code something like this efficiently. It should ideally be ran while the game is running.
A problem I can immediately see with this option is how I can know where each parts is located. aka, in what direction it needs to be moved.

Another possible solution would be to scale the entire model by small increments and do checks to see if the desired distance has been reached. This sounds like it would be very inefficient however.

What I’m looking for is a possible formula that can predict the size of the entire model so that the distance in between every part will be this same value.

1 Like

That is no problem.

This is the “hard” part. There’s no way to calculate that, because literally any answer is valid. You’ll have to decide what is right in your case. You could scale every model from its “center”, e.g. by using the bounding box of all the parts in the model. Or you could have an Attachment in every models PrimaryPart that tells you where the model should be scaled from (could even be from outside the model). Or if you’re trying to scale up a whole map with many models, you can scale all the models from the same point in the world.

Anyway, here’s how you can do it in a general manner:

``````function scaleParts(parts, scalingCenter, scalingAmount)
for _, part in pairs(parts) do
part.Size *= scalingAmount
part.Position = scalingCenter + (part.Position - scalingCenter) * scalingAmount
end
end
``````

If you know the distance is 1 stud before scaling and you want it to be 3 studs, you can call the function like so:

``````local spacingCurrent = 1
local spacingTarget = 3
scaleParts(daModel:GetChildren(), daModel.PrimaryPart.Position, spacingTarget / spacingCurrent)
``````

Edit: here’s an illustration of how the math works:

1 Like

I don’t need to scale the parts so I removed that part.

``````function scaleParts(parts, scalingCenter, scalingAmount)
for _, part in pairs(parts) do
part.Position = scalingCenter + (part.Position - scalingCenter) * scalingAmount
end
end
``````

Thanks for the visual demo.

The issue with that function is that it mainly works due to the `scalingAmount` variable. And thus it requires me to know the original distance between the parts.

I should have mentioned this in my original post, but I’m looking for a solution that can basically ensure every part is ordered, with the same distance in between them. Even if they originally were unsorted or offset by small amounts.

1 Like

I don’t understand why this would ever come up? Could you tell us about your use case. Why don’t you know the distance between parts beforehand, and why don’t you just scale everything up like you want in Studio? That’d make it easier to help you

1 Like

I’m making a procedurally animated ocean using skinned meshes. The parts in the example would be bones of the mesh.

I want to add an option to players that will increase/decrease the distance between every bone. To increase/decrease performance in lower end devices.

When the distance between every bone changes the area the mesh spans will change to. When it spans a larger area the performance will improve. At the cost of lower resolution.

I could probably use the scale multiplier you proposed.

If you can find any two adjacent bones, you can calculate the horizontal distance between them and use that in the scaling function?

1 Like