Jump to content
  • Advertisement
Sign in to follow this  
johnnyBravo

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.

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement
Do you remember year 9 trig?

SOH CAH TOA?

Tan = opposite over adjacent

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 this post


Link to post
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

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!