As for X and Y: I actually did it similar, just with a BoundingBox. I’ll sure try the Z thing out, cause it seems a bit less complicated (in sense of instructions ;) )
In the mean time I solved it by using the “similarity of triangles” rule (if you assume the frustum as two triangles: top and side view). I got the old state, meaning X,Y and Z from last the frame, I got the new X and Y and therefore can calculate the new Z for top and side view.
Then these two are combined to a final Z by using Pytagoras. Ok, this is not optimal as it uses a SQRT, but it works really well :) (with a bit of tweaking I can replace this calculation with some other though)
A different thing to consider might be some physics based movement of the camera (meaning acceleration / deceleration / spring approach, so that not every tiny theoretical position change results in a real camera position change… if you know what I mean)
So all in all, my code now looks like this, in case someone’s interested (I always miss the result post when somebody asks a question and then just writes e.g. “never mind, I solved it”):
| //Note: bounding box(BB) is updated every frame is the BB of all players. |
| X1 = (float)BB.Max.X - BB.Min.X; |
| Y1 = (float)BB.Max.Y - BB.Min.Y; |
| |
| //calc new Z based on similarity of triangles: |
| Z1X = (X1 * Z2X) / X2; |
| Z1Y = (Y1 * Z2Y) / Y2; |
| |
| //update position with new values, 0.5 = center to BB: |
| Position.X = (BB.Min.X + BB.Max.X) * 0.5f; |
| Position.Y = (BB.Min.Y + BB.Max.Y) * 0.5f; |
| |
| //Pytagoras: |
| Position.Z = Math.Sqrt( Z1X * Z1X + Z1Y * Z1Y ); |
| |
| //Other formula, no sqrt (space for tweaking!): |
| //Position.Z = Z1X * 0.8f + Z1Y * 0.3f; |
| |
| //Store values: |
| X2 = X1; |
| Y2 = Y1; |
| Z2X = Z1X; |
| Z2Y = Z1Y; |
I’ll show a video as soon as we got things a bit more improved (probably along with a playtest demo), but probably not before the next month.