XNA Creators Club Online
Page 1 of 1 (5 items)
Sort Posts: Previous Next

Camera like in Smash Brothers

Last post 3/20/2009 4:36 PM by Christian Herzog. 4 replies.
  • 3/19/2009 4:50 PM

    Camera like in Smash Brothers

    Hi!

    Lets say I got the following situation: a game like Smash Brothers, and I want to reimplement a camera behaviour like that. In case someone doesn’t know it, here’s a short description: I got up to four players in a 2D beat ’em up with 3D graphics. The camera should dynamically zoom out and in as the players move further apart / get closer together, so that no screen space is wasted…

    My algorithm to center the camera along X and Y (Y is up, Z of players is always 0) is, that I calculate a bounding rectangle (containing all players) and place the camera at the center of that.

    My main problem now is, how can I calculate the corresponding zoom factor, so that with a given position (X & Y), a Z is calculated so that all players are on screen?

    I tried it with something like:
    foreach( Target target in Targets ) 
      //target not in Frustum, meaning zoom out: 
      while( Renderer.CameraFrustum.Contains( target.Position ) ==   ContainmentType.Disjoint ) 
      { 
        Position.Z += 0.1; 
      } 

    Which works, but I guess is quite time consuming.
    Leading back to the big question: does anybody know a formula how I can calculate the new Z -position in one step, and not with this sort of trial and error method?

    Thanks!
    Chris

  • 3/19/2009 5:43 PM In reply to

    Re: Camera like in Smash Brothers

    I've never done this before, so this is sort of me just thinking aloud... but if you calculated the difference between the two furtherest apart characters (you may have to do two calculations though, furthest apart X and furthest apart Y), you could then center the camera (x,y) between them and you'd just have to figure out how far back to move the camera (z) to get them all in... if you're lucky, you may be able to multiple that difference times a linear value otherwise you may have to use some sort of curve (hope that makes sense, I'm more a 2D than 3D guy).

    In my head, that seems like it would work. Maybe someone else that has done this before has a better idea though.
  • 3/20/2009 7:57 AM In reply to

    Re: Camera like in Smash Brothers

    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.

  • 3/20/2009 12:05 PM In reply to

    Re: Camera like in Smash Brothers

    Actually, how to do this is shown in the documentation of XNA. Look under XNA Game STudio 3.0 - Programming Guide - Math - How To: Position the Camera to View all Objects in a Scene.
    here's the url: http://msdn.microsoft.com/en-us/library/bb197900.aspx

    you can download the sample code at http://go.microsoft.com/fwlink/?LinkId=129872&clcid=0x409
  • 3/20/2009 4:36 PM In reply to

    Re: Camera like in Smash Brothers

    That's awesome! I'll look at it as soon as I get home :)
    Thanks!
Page 1 of 1 (5 items) Previous Next
var gDomain='m.webtrends.com'; var gDcsId='dcschd84w10000w4lw9hcqmsz_8n3x'; var gTrackEvents=1; var gFpc='WT_FPC'; /*<\/scr"+"ipt>");} /*]]>*/
DCSIMG