I have two units belonging to two players respectively, and movement of the units is currently restricted to the YZ plane (see picture) and handled in a WASD like fashion. What I can't figure out is how to put a camera for both players such that it rests exactly between the two units at all times, while adjusting the zoom according to the distance between the two. I have a feeling it involves something rather simple with regions, but I've never really managed cameras before.
I dont know if there is an easy way of doing it, but we could do the hard way. First of all, you can set camera position to be center of the unit group consisting of the two units. Then you can set the camera height offset to the avrage distance from the ground + the base height you want to be at.
Just missing the distance then..
If you look at the triangle formed by the camera and the two units. Then emagine that triangle cut in half from the camera viewpoint to the center of your units.. Then you get two new triangles. One side in that triangle is the distance we want to find. The other is half the distance between your units. One of the angles is 90 degrees, so we just need to know one more thing about the triangle, and that is the angle at which the camera is watching (Field of view). we can look that up for our camera - its about 37. So using the sine relations a/sin(A)=b/sin(B) we get
cameraDistance = (Distance between units * sin(90 - camera field of view / 2)) / (2 * sin(camera field of view / 2))
You might want to calculate this for the y and z axis seperatly, and take the greatest distance of the two. Or you could just take the distance in two dimensions - it might zoom the camera a bit more out then required though.
Ofcourse this would need to be ajusted as the units move around (try to not update the camera when it is not needed, to reduce lag)
0_o" I had a feeling I needed to do something with triangles, and I appreciate that you've laid out the math here. Lemme flail about a bit with that equation and see what I can come up with.
Rollback Post to RevisionRollBack
To post a comment, please login or register a new account.
I have two units belonging to two players respectively, and movement of the units is currently restricted to the YZ plane (see picture) and handled in a WASD like fashion. What I can't figure out is how to put a camera for both players such that it rests exactly between the two units at all times, while adjusting the zoom according to the distance between the two. I have a feeling it involves something rather simple with regions, but I've never really managed cameras before.
Thanks in advance!
I dont know if there is an easy way of doing it, but we could do the hard way. First of all, you can set camera position to be center of the unit group consisting of the two units. Then you can set the camera height offset to the avrage distance from the ground + the base height you want to be at.
Just missing the distance then..
If you look at the triangle formed by the camera and the two units. Then emagine that triangle cut in half from the camera viewpoint to the center of your units.. Then you get two new triangles. One side in that triangle is the distance we want to find. The other is half the distance between your units. One of the angles is 90 degrees, so we just need to know one more thing about the triangle, and that is the angle at which the camera is watching (Field of view). we can look that up for our camera - its about 37. So using the sine relations a/sin(A)=b/sin(B) we get
cameraDistance = (Distance between units * sin(90 - camera field of view / 2)) / (2 * sin(camera field of view / 2))
You might want to calculate this for the y and z axis seperatly, and take the greatest distance of the two. Or you could just take the distance in two dimensions - it might zoom the camera a bit more out then required though.
Ofcourse this would need to be ajusted as the units move around (try to not update the camera when it is not needed, to reduce lag)
@SBeier: Go
0_o" I had a feeling I needed to do something with triangles, and I appreciate that you've laid out the math here. Lemme flail about a bit with that equation and see what I can come up with.