- Home /
Move camera using rule of thirds
I am working on a camera control system for cut scenes. I want to write a function that will take the position of two game objects and rotate/move the camera such that the two objects will appear on the left and right third of the screen width according to the rule of thirds.
So far I have been able to get the position of the game objects on the screen using Camera.WorldToScreenPoint and I have been able to calculate where the thirds fall using Screen.width.
I have been toying with the idea of calculating an offset needed to rotate the camera so that the two items are equally offset from their respective third line and then moving the camera either forward or back until each item is on a third line, but I am not having much luck. I can't seem to get the camera to rotate to the correct spot so that the game objects are equally offset.
To clarify what I am working for, please look at the following pictures:
Start:
From School StuffEnd:
From School Stuff[1]: https://picasaweb.google.com/lh/photo/CjVYNQfPblCr9-TkIpOvEg?feat=directlink
I managed to figure out the math for a perpendicular (90 degree) angle:
Let us have points x1 and x2 as the points we want on the thirds. Let w be the 3*distance between x1 and x2. Let y be the midpoint between x1 and x2. Let C be our camera and phi be half its fov. The distance from C to y is (1/2 * w)/tan(phi).
This is ok, but I was hoping to be able to use the angle the camera already has. I want to move the camera as little as possible to achieve this effect.
Ok, I may be on to something:
Imagine a triangel consisting of the camera as one point and the side opposite the camera is the line that passes throught p1 and p2. The other two points that make up the triangle are the left most and right most points viewable on the screen that lie on the line opposite the camera.
W = distance between the left and right most points
J = distance from camera to left most point on line
$$anonymous$$ = distance from camera to right most point on line
theta = fov of camera (C angle)
mid = midpoint between two objects we are interested in
angle = angle opposite of J
L = distance between mid and where we need to move the camera to
Using the law of sines and cosines I get: L = $$anonymous$$^2 + (W/2)^2 - 2$$anonymous$$(W/2) cos( Asin( J * (sin(theta)/W)))
While I think this should work, in practice it does not.
$$anonymous$$oving the camera as little as possible will need to be a heuristic. Ex: if the camera is near or on the line between the two of them: if it's to either side, they may appear on top of each other or slightly separated. If on top of each other, you have to move it regardless. If slightly offset, you'd have to move the camera probably too close to one of them. if the camera is between them, you have to move it away anyway. So you may want a variable that weighs how much to move vs how much to rotate, or perhaps $$anonymous$$/max on move and rotate, to help choose just about where you'll be moving to.
Answer by DaveA · Jul 20, 2011 at 08:25 PM
Sounds like basic trig. It would be interesting to have the camera 'find' it's way to the correct spot(s), or you could calculate it and lerp to it or just jump there.
I don't have the formula for you (yet), but maybe this will help you: you have the world distance between the objects. You bisect that and get the perpendicular (normal) to that, and that's the line you'll move the camera back on. The thing that makes it really interesting is that this depends on FOV. A smaller FOV means moving the camera further away. Also the perpendicular bisector means that the camera may move in one of two directions, so you'll need to determine which one by which object you want on the left or right.
I think Viewport coordinates will be helpful: http://unity3d.com/support/documentation/ScriptReference/Camera.ViewportToWorldPoint.html
Your answer
Follow this Question
Related Questions
Rotate Camera to the object 0 Answers
Multitouch problem 0 Answers
Camera movement doesn't work in Unity (but does in builds) 0 Answers
How move camera without move selection? 0 Answers
Clamping rotation of an object 0 Answers