se1:Matrix-Style Camera

From SeriousEngine.com

Jump to: navigation, search

im the juggernaut bitch video angeles equipment los rental video ancient civilizations for children video series authentic costume pirate adult male movie stars ologetaca Author: Chao "EricCartman"
Email: ouabout@yahoo.com


Contents

What is Matrix Style Cam?

Well, if you have seen the film "Matrix", you know what I am talking about. It's still frame where everything freezes while the camera spins around the character 360 degrees allowing the audience to see what’s going around at that instance of time. This tutorial shows how to implement this effect.

Image:Tutorialcodingmatrixcamericcartmanmatrixcam.jpg

The Code

This code is wrtten based on Zip’s Still-Frame Effect tutorial. If there’s anything I didn’t explain clearly, you can also refer to that article for some hints, it explains some of the Camera principles pretty well.

All changes will be made in camera.cpp (in the Game project). You will also need to modify a control file (Common.ctl) to bind a key to toggle the cam on and off. All the changes are in *BOLD*.


(1) In camera.cpp (the one Game project, NOT the one in Entity project), add these codes on top:


//Matrix Cam Variable Declaration
Int     iMatrixCamAngle = 0;
BOOL    _bMatrixCam = FALSE;
FLOAT3D CamTargetPos; 
ANGLE   CamTargetAngle;
Float   MatrixCamDis = 3.0f;
//End Declaration

(2) Now lets write a function (Cam_Matrix) to start the Matrix Cam, and a toggle function for toggling it on and off.

//Start Matrix Cam 
void Cam_Matrix(BOOL bMatrixCam)
{
   //set to our new state
   _bMatrixCam = bMatrixCam;
   if (_bMatrixCam)
   {
       //Freeze events
       _pShell->Execute("gam_fRealTimeFactor=0;");

       //Init camera
       _cp.cp_vPos = FLOAT3D(0,0,0);
       _cp.cp_aRot = ANGLE3D(0,0,0);
       _cp.cp_aFOV = 90.0f;
       _cp.cp_fSpeed = 1;
       _cp.cp_tmTick = 0.0f;
       iMatrixCamAngle = 0;
       //Let the render code know we want to reset the camera 
       //position to the player for the first frame
       cam_bResetToPlayer = TRUE;
   } else {
       //Back to normal
       _pShell->Execute("gam_fRealTimeFactor=1;");    
   }
}

// Toggle Matrix Cam
static void ToggleMatrixCam()
{
   Cam_Matrix (!_bMatrixCam);
}
//End Matrix Cam


Since we want to freeze the scene, we use *_pShell->Execute("gam_fRealTimeFactor=0;");* when *bMatrixCam* is FALSE, the normal time will be restored with *"gam_fRealTimeFactor=1;"*. Make sure that you insert these codes before the CAM_Init function.

(3) Now find the CAM_Init function, and declare our new symbol:


   //MatrixCam Effect
   _pShell->DeclareSymbol("user void ToggleMatrixCam();", &ToggleMatrixCam);
   //end Mod


This code allows "ToggleMatrixCam();" to become a console command, so whenever you type "ToggleMatrixCam();" in console, the ToggleMatrixCam function in our code is called.

(4) Now we need to tell the game that we should turn on camera mode


BOOL CAM_IsOn(void)
{
// Added _bMatrixCam
return _bCameraOn || _bMatrixCam;
}

(5) Now, every time the camera is ON, the engine will call the CAM_Render function. Let’s modify it to implement our Matrix Cam effect.

void CAM_Render(Centity *pen, CdrawPort *pdp)
{
   //Matrix Cam Effect, Mod by Chao, 06-02-01
   if (_bMatrixCam)
   {
       FLOATmatrix3D m;
       //Matrix style rotate around the player
       _cp.cp_aRot(1) = CamTargetAngle + iMatrixCamAngle;
       //restore target(the player)'s position to cam
       _cp.cp_vPos(1) = CamTargetPos(1);
       _cp.cp_vPos(2) = CamTargetPos(2); 
       _cp.cp_vPos(3) = CamTargetPos(3);
       // adjust camera position to Player's height
       _cp.cp_vPos(2) += 2.0f;
       MakeRotationMatrixFast(m, _cp.cp_aRot);
       FLOAT3D vZ;
       vZ(1) = m(1,3); vZ(2) = m(2,3); vZ(3) = m(3,3);
       //move camera back so we can see the player
       _cp.cp_vPos += vZ*MatrixCamDis ;
       //allow mouse tilt camera angle
       _cp.cp_aRot(2)-=_pInput->GetAxisValue(MOUSE_Y_AXIS)*0.5f;
       if (iMatrixCamAngle >= 360) {
           iMatrixCamAngle = 0;
       }
       else{
           iMatrixCamAngle ++; // increment camera angle
           // adjust cam distance
           if( _pShell->GetINDEX("ctl_bMoveForward")) { MatrixCamDis -= 0.2f; };
           if( _pShell->GetINDEX("ctl_bMoveBackward")) { MatrixCamDis += 0.2f; };
       }
       Clamp( _cp.cp_aFOV, 10.0f, 150.0f);
       //Set camera to players position
       if( cam_bResetToPlayer) {
           //store target(the player)'s position
           _cp.cp_vPos = pen->GetPlacement().pl_PositionVector;
           CamTargetPos(1) = _cp.cp_vPos(1); 
           CamTargetPos(2) = _cp.cp_vPos(2); 
           CamTargetPos(3) = _cp.cp_vPos(3); 
           //get direction the camera is facing 
           _cp.cp_aRot = pen->GetPlacement().pl_OrientationAngle;
           CamTargetAngle = _cp.cp_aRot(1);
           cam_bResetToPlayer = FALSE;
       }
   }
   //End Matrix Freeze
   else if( cam_bRecord) {

Basically what does is whenever _bMatrixCam is on, for the first frame it stores and position (CamTargetPos) and direction (CamTargetAngle)of the player. Pull’s back the camera (MatrixCamDis) allowing us to see the player, and then with each increment frame increases the camera angle (iMatrixCamAngle) with respect to the initial Target Angle, so that the camera fix on the object and spins around it. Coding done! Compile but DON’T run the game yet, we have to bind a key to toggle this Cam on and off. We don’t want to go to the console and type "ToggleMatrixCam();" every time we want to see the effect. So,<

(6) find *Common.ctl* in SeriousSam\Controls\System directory, add these lines to bind a key B to the console command.

Button
Name: TTRS ToggleFreeze
Key1: F10
Key2: B
Pressed:  ToggleMatrixCam();
Released:


Save the file. This Done and Done!

How to use

Run the game, whenever you press B, the game pauses and the camera spins around the player until B is pressed again. You can adjust the distance of the camera by press "W" or "S" (or which ever key you defined for "forward" and "backward"), and also tilt of the camera by moving the mouse. Now lay back enjoy the famous "Sam dodging rockets" scene.


Issues

One thing I couldn’t figure out is that the scene shakes a bit when I move the mouse up and down to tilt the camera. If anyone knows why (I am sure there are plenty of ppl who can figure it), please email me.


Final words

And problems, questions, comments? Mail me! Thx Zip for his excellent still-frame effect tutorial.

EricCartman – Lead Newbie, "Serious Engine, Editor, Modeler Club"

Personal tools