r/howdidtheycodeit • u/Xarjy • Mar 24 '22
Question Restricting camera movement to a character radius with multiple characters (like in Kenshi), how did they do it?
In the game Kenshi you can move the camera around using WASD parallel to the ground like an RTS, and the camera is restricted to a radius around your playable character. I already understand this portion is done by clamping the camera rig to a radius using the character as the center point.
However, in Kenshi you end up with multiple playable characters. When the characters are fairly spaced out, there's no jittering like it's jumping from one character's radius to another.
How do you think this is efficiently achieved? Maybe the movement radius is just swapped out with a list of the playable characters, and it just calculates its distance based on closest character?
7
u/Exonicreddit Mar 24 '22
It probably is jumping but seamlessly. That's how I would do it, calculate the world offset to local offsets and move the camera between the two local positions in the same frame so it's seamless.
6
u/Perse95 Mar 24 '22
Depends on the number of concurrent playable characters how you'd implement this. Not sure exactly how it works in Kenshi (not too familiar with the game), but if the number of characters is on the order of, say, 10, then you can calculate the future camera position as the inverse distance weighted average of the clamped camera position for each character.
Let Pc be your camera position, ∆Pc be the player input, Ci the position of character i, Ri the radial distance of character i that you want to limit camera distance to, and Di the distance of the camera from character i, and μ a control parameter in the interval [0, ∞).
First compute Pcf = Pc + ∆Pc, then find Pi for each character which is the position Pcf clamped to distance Ri from character i, then calculate the final camera position as the mean Pi weighted by 1.0/Diμ (so that closer characters influence more than distant and μ controls the relative strength of each character position). Now you're guaranteed that your camera will always stay within some radius you defined.
2
u/foonix Mar 24 '22
I've played a lot of Kenshi. To me it just feels like it is bounded by the closest character. Try playing around with situations like to characters close to each other and then move them both away. The direction the camera goes depends on which one happens to be closer.
So I think it just runs through the list of characters, does a distance calc on each, and then if none of the characters are close enough to the camera it uses the closest one. If any one of the characters is close enough, then the loop can be short circuited.
Doing 40 - 100 distance calcs isn't actually that bad, but there is plenty of room to optimize. For example the "distance squared" optimization can probably used in the whole process right up until it has to actually move the camera, and at that point it only has to one square root.
2
u/GarethIW Mar 24 '22 edited Mar 24 '22
I've not played Kenshi, but I dug into my past New Unity Projects as I've previously made a camera that clamps to x number of referenced transforms (players). This might give you a place to start at least?
https://gist.github.com/GarethIW/e9900a45f535fa78016c05d1b0ee8105
I added a video link in the comments. You could potentially have the BaseOffset controlled with WASD and clamped to a sphere to allow movement around the centre point.
Edit: Oh okay I just looked at some Kenshi gameplay and it's nothing like this. Oh well, might be useful to someone 😅
3
u/private_birb Mar 24 '22 edited Mar 24 '22
Probably just averaging the position of the characters and then a constant radius that's adjusted for how far apart the characters are.
That, or it uses the average of the x closest characters to smooth it, then just a constant radius from that.
I'm sure a lot of solutions will get pretty similar or decent results, so I don't think you need to overthink it. It's not difficult to change how to calculate it, so you can really just play around and see what feels good.
1
u/Sandalmoth Mar 24 '22
What about first calculating a convex polygon with a character on each corner, and then clamping the camera to that? The camera could then be placed anywhere between the characters, but not too far out from the group. With a sweep line algorithm the bounding polygon of the group should be pretty quick to find.
2
u/Sandalmoth Mar 24 '22
So, i think a convex hull of n points can be found in n*log(n) time, so that scales decently. And unless the characters move very quickly, it could probably be calculated every couple of timesteps with maybe some interpolation between them for smoothness. But yeah, it probably isn't the fastest method, but I think it's impossibly costly to calculate.