- Home /

# How to squash a rectangular prism without changing volume and expand the width and length by the same amount?

Ok so this is more of a math question than a unity question but here we go. My problem is I want to squash a rectangular prism (technically represented by a Vector3) without changing its volume. Shrink the height and expand the length and width to keep the volume the same, AND grow the length and width by an equal amount (if width goes up 0.5, length must go up 0.5) .

Additionally I say its a rectangular prism and not a cube because it will not have equal sides to begin with. Were it only that simple! I may be attempting to squash a rectangular prism with dimensions starting at width 2 height 6 and length 4 (with a volume of 48) and then attempting to squash that height to 5 and expand the width and length by X where (2 + X) · (4 + X) · 5 still equals 48.

I have been scouring the internet for a solution but so far I haven't found anything that isn't "You can find the volume by multiplying the height width and length!" Thank you internet, but that is not what I asked. "You can find the height with the volume width and length!" still not what I asked. Etc...

It is possible that I was not at all good at math in school, it is possible that its late and/or very early, it is possible I have been at this problem for four hours, it is even conceivable that all three are true so at this point I don't know if I can't find an answer to this problem online because it is remarkably simple, because it is impossible, because no one has asked it before, or I didn't use the right keywords.

Any help solving this and/or explaining why it cant't be solved would be greatly appreciated.

You could work the math out if the shapes are known beforehand. For example a rectangle that is 2 units high x 3 wide and 2 deep will have 12 units cubed of volume within it. Now if you know how squished you want it’s most squished State to be say 1 unit high, you can scale it to 1 unit high and since your original base had 6 units cubed within it, you have to figure out how to apply the remaining 6. That’s .5 in each direction. So if scaled down by 0.5 in the y axis, you scale up 1.5 in the x and 1.333 in the z. Now since you know the values, you can just use Vector3.lerp on the scale of the transform to do what you need.

**Answer** by elenzil
·
Mar 07 at 06:30 PM

```
/*
Let's say your rectangular prism starts off with L, W, H,
and you want to sqaush it to some new height h,
and calculate the new length and width l and w.
L and W change by the same amount, and the volume is unchanged.
so you want to find d, the delta in L and W:
setup:
1. l = L + d
2. w = W + d
3. lwh = LWH
solving:
4. (L + d) * (W + d) * h = LWH
5. LW + Ld + Wd + dd = LWH/h
6. dd + d(L + W) + LW = LWH/h
7. dd + d(L + W) + (LW - LWH/h) = 0
8. dd + d(L + W) + LW(1 - H/h) = 0
which is a quadratic equation where
A = 1
B = L + W
C = L * W * (1 - H/h)
Using the standard quadratic equation will give you 0, 1, or 2 real solutions.
If you get 2 solutions, just pick one.
*/
```

here's a demo of the squashing thing, plus an implementation of a quadratic solver.

### Your answer

### Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.