welcome to linkAR technical documentation


Controlling the camera with the matrix


The following function allows us to set the camera rotation:

/** 1.- Set camera rotation in the scene*/
    [glView setCameraRotation:camPos];

The function parameter is an NSArray containing the rotation matrix (4×4).
This way we manage the camera orientation.

The behavior of the camera depends on which type of camera you have chosen.

FPS: The camera will rotate with static position.
Trackball: The camera will orbit around the target position.

An example of how to use this camera would be the following:

First, declare a CMMotionManager object:

CMMotionManager *motionManager;

Then, at the ViewDidLoad initialize it:

motionManager = [[CMMotionManager alloc] init]; // motionManager is an instance variable
    motionManager.accelerometerUpdateInterval = 0.01; // 100Hz
    [motionManager startDeviceMotionUpdates];
    [motionManager startDeviceMotionUpdatesUsingReferenceFrame:CMAttitudeReferenceFrameXMagneticNorthZVertical];

Finally set the camera rotation at the frameRendered method:

// Applies the model orientation depending on device position//
    CMAttitude *currentAttitude = motionManager.deviceMotion.attitude;
    // Quaternion
    CC3Vector4 v4;
    v4.x = currentAttitude.quaternion.x;
    v4.y = currentAttitude.quaternion.y;
    v4.z = currentAttitude.quaternion.z;
    v4.w = currentAttitude.quaternion.w;
    CC3Vector v;
    v.x = 0.0;
    v.y = 0.0;
    v.z = 0.0;
    CC3GLMatrix *modelMatrix = [CC3GLMatrix matrix];
    [modelMatrix populateFromTranslation:v];
    [modelMatrix rotateByQuaternion:v4];
    NSArray * m = [NSArray arrayWithObjects:
                   FBOX(modelMatrix.glMatrix[0]), FBOX(modelMatrix.glMatrix[1]), FBOX(modelMatrix.glMatrix[2]), FBOX(modelMatrix.glMatrix[3]),
                   FBOX(modelMatrix.glMatrix[4]), FBOX(modelMatrix.glMatrix[5]), FBOX(modelMatrix.glMatrix[6]), FBOX(modelMatrix.glMatrix[7]),
                   FBOX(modelMatrix.glMatrix[8]), FBOX(modelMatrix.glMatrix[9]), FBOX(modelMatrix.glMatrix[10]), FBOX(modelMatrix.glMatrix[11]),
                   FBOX(modelMatrix.glMatrix[12]), FBOX(modelMatrix.glMatrix[13]), FBOX(modelMatrix.glMatrix[14]), FBOX(modelMatrix.glMatrix[15]),
    if(![glView setCameraRotation:m])
        NSLog(@"Error setting camera position");