# How to keep all objects within camera view by moving it forward and back?

This topic is 4442 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I want to make my camera move forwards and backwards to keep all my space ships within the camera's view and be centred on the average position of all the objects. eg if an object is outside the camera's view, it moves backwards till its back in view, or if all the objects are within view, then move forwards as much as it can while keeping all the objects within view still. How would I go about this, centering the view I can do with the average, but with moving in and out, I'm not really sure. Maybe something to do with calculating 3d to 2d maybe?

##### Share on other sites
Do you remember year 9 trig?

SOH CAH TOA?

you know the angle because its half your field of view. What you want is the adjacent length (how far away your cam should be from the average point) and you can work out the opposite length. Get the average point of all your objects you want in view (add all vectors, divide by number of them). Then find the max distance between the objects and that center point. Then your opposite is half that length.

camDist = oppositeLength / Tan(fov/2);

Or something like that :D I HIGHLY recomend you actually draw it out on a piece of paper - make a nice triangl etc then make sure my logic is all correct.

##### Share on other sites
hmm.....
looks like making a never before 3rd person camera control.

although i know only about first person camera control, but i guess this will work.

firt calculate the farthest ships in your scene(just a little vector maths required for that), then calculate the distance between them(again evil math).

this is the distance where the camera will be positioned.

oK... i am comming on to the orientation also,

calculate the midpoint of the distance between the farthest plane, and them move in the opposite direction of the ship's direction vector.(and yes make sure that that point is empty or not, if not then move to the nearest empty point i.e. mean don't end up ur camera in alien ship.)

although i agree that this will work when the ships are facing the same direction but same can be adopted for the away facing ships also.

bye -
RK - the one

1. 1
Rutin
40
2. 2
3. 3
4. 4
5. 5

• 18
• 19
• 12
• 14
• 9
• ### Forum Statistics

• Total Topics
633363
• Total Posts
3011509
• ### Who's Online (See full list)

There are no registered users currently online

×