Pages

Saturday, September 7, 2013

[TUT] Camera control

My knowledge about camera is limited, i will show in this tutorial what i know about cameras in GTA IV.

Download the source code used here (mirror)

First of all we have direct access to the actual active camera using the following object:

Game.CurrentCamera

With this object we can obtain useful info about actual active camera:



Other way to obtain access to actual game camera is using this object:

Game.DefaultCamera

The problem with this object is that it disappear (= nothing) when an ped is set as targeted by player when player is unarmed, so, if you want use this camera be careful and make the Exists(Game.DefaultCamera) check before use.

GET_GAME_CAM
This native method will return the game camera, very useful when we need to find the actual game camera doesn't matter if we have a gun or not:




Creating a camera

To create a camera we need an object of type camera and we need to create a new instance of it:



Now to see the view of this camera we need to Activate it:


To demonstrate some camera control let's create an free camera with keyboard control, when i press move keys the camera will move forward and backward, when i press + and - camera will zoom in and out and when i press left/right/up/down camera will change direction:


Basically what i'm changing here is Position, Rotation and FOV.

An interesting method to use is the LookAt, we can set the camera to look at a Ped, Object, Vehicle or a single position, when we set this we can't control the camera direction or rotation anymore:


To obtain control over Direction and Rotation again we need to call UNPOINT_CAM method:


Another cool method to use is the SET_CAM_SHAKE:


First param is the camera, we can shake the game camera too but before we need to call CAM_PROCESS and specify game.default camera as the param.

Second param seems to be the level of movement, from 1 to 8

Third param is the time in milliseconds, -1 will result in a shake of approximated 1 second

This is what you need to do to shake the game default camera:




Attaching a camera

We can attach camera using the following methods:

ATTACH_CAM_TO_OBJECT
ATTACH_CAM_TO_PED
ATTACH_CAM_TO_VEHICLE
ATTACH_CAM_TO_VIEWPORT

After attaching the camera we can set the offset from the attach point using this method:

SET_CAM_ATTACH_OFFSET

To make the attach be relative to the object we need to call this method:

SET_CAM_ATTACH_OFFSET_IS_RELATIVE

To remove the attach we use UNATTACH_CAM

Let's make an example of this attach, let's attach the camera to player current vehicle, if player aren't in a car let's attach the camera to him:


Now, in a tick we need to set the camera direction or rotation:



With some coding and imagination you can easily create a basic first person camera.