# Scaling & Distance

**URL:** <https://forum.babylonjs.com/t/scaling-distance/19489>\
**Category:** Questions\
**Created:** [March 26, 2021, 6:02pm UTC](https://forum.babylonjs.com/t/scaling-distance/19489 "2021-03-26T18:02:21Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Adrianlines](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/adrianlines/32/38386_2.png) [@Adrianlines](https://forum.babylonjs.com/u/Adrianlines)\
**Post date:** [March 26, 2021, 6:02pm UTC](https://forum.babylonjs.com/t/scaling-distance/19489/1 "2021-03-26T18:02:21Z")

</div>

I’ve been scratching my head on this problem all day.  
I want to be able to calculate how far away from a mesh I need to stand to see the whole of the mesh.  
The mesh is box with an image on one face. The image can be in either landscape or portrait aspect.  
The problem is that the user can also have their browser window in any aspect ratio.  
I worked out the scale based on the ratio of the original image compared to the ratio of the browser window, but I cant workout how to translate that into a distance from the mesh.

---

<div class="post-metadata">

**Author:** ![Adrianlines](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/adrianlines/32/38386_2.png) [@Adrianlines](https://forum.babylonjs.com/u/Adrianlines)\
**Post date:** [March 27, 2021, 3:59pm UTC](https://forum.babylonjs.com/t/scaling-distance/19489/3 "2021-03-27T15:59:26Z")

</div>

Sorry, I couldn’t work out how to relate the example to my issue.  
Also camera.zoomOn([mesh]) throws an unknown function error outside of the PG.

---

<div class="post-metadata">

**Author:** ![adam](https://avatars.discourse-cdn.com/v4/letter/a/4af34b/32.png) [@adam](https://forum.babylonjs.com/u/adam)\
**Post date:** [March 27, 2021, 6:32pm UTC](https://forum.babylonjs.com/t/scaling-distance/19489/4 "2021-03-27T18:32:44Z")

</div>

This doesn’t answer your question. I just want to make sure that you know about camera framing behavior if that’s what you’re trying to do.

> **[Camera Behaviors | Babylon.js Documentation](https://doc.babylonjs.com/divingDeeper/behaviors/cameraBehaviors#framing-behavior)**
>
> Everything you want to know about camera behaviors.

---

<div class="post-metadata">

**Author:** ![samevision](https://avatars.discourse-cdn.com/v4/letter/s/73ab20/32.png) [@samevision](https://forum.babylonjs.com/u/samevision)\
**Post date:** [March 27, 2021, 6:49pm UTC](https://forum.babylonjs.com/t/scaling-distance/19489/5 "2021-03-27T18:49:46Z")

</div>

Something like?:

const bb = object.getHierarchyBoundingVectors()  
const size = bb.max.subtract(bb.min)  
camera.radius = Math.max(size.x, size,y, size.z)

---

<div class="post-metadata">

**Author:** ![Adrianlines](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/adrianlines/32/38386_2.png) [@Adrianlines](https://forum.babylonjs.com/u/Adrianlines)\
**Post date:** [March 29, 2021, 2:55pm UTC](https://forum.babylonjs.com/t/scaling-distance/19489/7 "2021-03-29T14:55:52Z")

</div>

I’ve copied the relevant script into the playground.  
I need to be able to calculate the distance so that the mesh just fits within the frustum.  
[https://playground.babylonjs.com/#CVIF83#26](https://playground.babylonjs.com/#CVIF83#26)  
I’m old and maths was never my strong point 🙂

---

<div class="post-metadata">

**Author:** ![Adrianlines](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/adrianlines/32/38386_2.png) [@Adrianlines](https://forum.babylonjs.com/u/Adrianlines)\
**Post date:** [March 30, 2021, 8:08pm UTC](https://forum.babylonjs.com/t/scaling-distance/19489/8 "2021-03-30T20:08:32Z")

</div>

Framing Behaviour only seems to be relevant to Arc Cameras. I’m currently moving around the scene using a universal camera. Should I convert the universal to an arc to frame the mesh, and then convert back to a universal ? … I feel like there is a mathematical solution. I can query the mesh dimensions, and the scene window dimensions, it’s just then the mathematics to determine the distance from the mesh, to width either the maximum height or width depending on which is the largest.

---

<div class="post-metadata">

**Author:** ![Knar](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/knar/32/14900_2.png) [@Knar](https://forum.babylonjs.com/u/Knar)\
**Post date:** [March 31, 2021, 12:54am UTC](https://forum.babylonjs.com/t/scaling-distance/19489/9 "2021-03-31T00:54:27Z")

</div>

@Adrianlines What problem you are trying to solve with this, maybe there’s an easier way? This is how I would approach the problem:

 ![image](https://us1.discourse-cdn.com/flex024/uploads/babylonjs/original/2X/3/359228eb0fef6fe7701a956257a2965e0e4e576c.png)  
If your mesh is 200 units tall, and your camera FOV is default (.8 radians or ~46 degrees), your distance to fill the height would be 100 / tan(23) = 235.6

Camera FOV is bound to the height of the viewport by default (fovMode = FOVMODE\_VERTICAL\_FIXED) with the horizontal FOV stretching to fill the screen. There is probably a way to figure out the angle for the width but I’m not sure how you would do that, if you can figure that part out you could calculate the distance in the same way and pick the smaller one.
