### Author Topic: position camera for object to fit in screen height  (Read 730 times)

#### aeroxr1

• int
• Posts: 82
##### position camera for object to fit in screen height
« on: September 24, 2014, 01:47:38 am »
Hi

I have an 3d model and I want to set my camera to fit all the object in screen height.

I did in this way .

I have calculated bounding box :
Code: [Select]
`protected float[] calcBoundingBox() { float[] box = null; for (Animated3D skin : modello) { float[] skinBB = skin.getMesh().getBoundingBox(); if (box == null) { box = skinBB; } else { // x box[0] = Math.min(box[0], skinBB[0]); box[1] = Math.max(box[1], skinBB[1]); // y box[2] = Math.min(box[2], skinBB[2]); box[3] = Math.max(box[3], skinBB[3]); // z box[4] = Math.min(box[4], skinBB[4]); box[5] = Math.max(box[5], skinBB[5]); } } return box; }`
and after :

Code: [Select]
`float[] bb = calcBoundingBox();box_h = (bb[3] - bb[2]); // model heightCamera camera = world.getCamera();if(width < height){ camera.setPosition(0,-box_h/2,0);camera.moveCamera(Camera.CAMERA_MOVEOUT, 70);  //la sposto indietrocamera.lookAt(new SimpleVector(0, -box_h/2, 0));}else{float fovy=camera.getYFOV();double cameraDistance = ( box_h / 2 ) / Math.tan( fovy / 2 );camera.setPosition(0,-box_h/2,(float)cameraDistance-100);camera.lookAt(new SimpleVector(0, -box_h/2, 0)); }`
With the -100 offset the 3d model correctly fits in both the portrait mode than landscape mode. But I didn't understand why I have to pur the negative offset -100..

Where did I do wrong ?