# One Camera With Multiple Targets

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

## Recommended Posts

I'm currently working on a 3D multiplayer game with a singular camera that tracks multiple targets. Right now it translates its position relative to the centroid. This causes issues when, for example, 3 players are in one corner and one player is in the other. Anyone have a relatively simple algorithm that properly weights how these targets contribute to the lookat/position of the camera and ensures no target ever goes off screen?

##### Share on other sites
My first thought is to define a function that, given the coordinates of the targets (after projection), computes a utility (how happy we are with the situation). You can then compute the gradient of this function with respect to the position parameters of the camera, and move the camera in the direction that maximizes the utility.

Does that make sense?

##### Share on other sites

My first thought is to define a function that, given the coordinates of the targets (after projection), computes a utility (how happy we are with the situation). You can then compute the gradient of this function with respect to the position parameters of the camera, and move the camera in the direction that maximizes the utility.

Does that make sense?

That makes sense, but what might the inputs/outputs be for a typical utility function? I get this is the general idea, but finding a way to process that utility function is the tricky part.

##### Share on other sites
You should also zoom in and out to match view size to target position; you are effectively trying to keep inside the view a composite target that, in addition to moving, becomes larger if the tracked characters move apart and smaller, up to the size of a single character, if they move closer to each other. In your example, you have to zoom out to include all "corners" over the course of many frames, as characters spread around.

Given a camera position, I'd compute a satisfactory frustum to frame every tracked target and merge them (leftmost left side, rightmost right side, etc.) to obtain a frustum that includes all targets, which translates easily to a camera transform matrix. Meanwhile, you can move the camera to a better place.

##### Share on other sites
Your utility function can be a sum of several terms. Make a soft function that rewards being near the center of the screen, and add that for each target. Then add something that rewards having the targets be separated on the screen, perhaps by pairs, or perhaps by the distance between the two most distant targets (in screen space).

You'll have to play around with it until you are happy with the results. If you are not happy with the results, think of what feature of the configuration doesn't make you happy, and tweak the utility function to represent that. Rinse. Repeat.

##### Share on other sites
If you want it simple, you could just average the coordinate positions of all targets that your camera needs to look at, then have your camera focus on the averaged position.

##### Share on other sites

If you want it simple, you could just average the coordinate positions of all targets that your camera needs to look at, then have your camera focus on the averaged position.

We've been doing that but, for example, when 3 are in one corner and 1 is in the other, the isolated target is not in the camera view at all.

##### Share on other sites
We've been doing that but, for example, when 3 are in one corner and 1 is in the other, the isolated target is not in the camera view at all.[/quote]
In some cases, it may be impossible to have every target on screen, unless your camera is reaaaally far away (in which cases the targets might be much too small to be seen).

##### Share on other sites

We've been doing that but, for example, when 3 are in one corner and 1 is in the other, the isolated target is not in the camera view at all.

In some cases, it may be impossible to have every target on screen, unless your camera is reaaaally far away (in which cases the targets might be much too small to be seen).
[/quote]

Perhaps you could transition to two cameras with a split screen in that case. It might work for some games, and it would be really cool if done well.

##### Share on other sites
Have you thought of something like one of the Lego games? I don't recall which one specifically, but Pirates may be what I am thinking of. If you had a second player, and the camera could not zoom out enough, instead of 'locking' the camera in the level to view the 2 players, and having one player 'push' the other one when they move further out of bounds, the screen would split itself diagonally, and if you moved further away from each other, the split would progressively align itself vertically.

I do not know how well it could translate to more than 4 players, maybe some dynamic viewport magic

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 10
• 11
• 13
• 9
• 11
• ### Forum Statistics

• Total Topics
634088
• Total Posts
3015451
×