<commit_before>//
//  CameraFollow.cpp
//  CGP-Ex3
//
//  Created by <NAME> on 4/27/15.
//  Copyright (c) 2015 <NAME>. All rights reserved.
//

#include "CameraFollow.h"

#define MIN_CAMERA_DISTANCE (4.0f)
#define MAX_CAMERA_DISTANCE (10.0f)
#define MAX_LAG_SPEED (20.0f)
#define FOLLOW_PERCENT (0.05f)

CameraFollow::CameraFollow(Camera *camera, Ship *ship, SkyBox *skybox) :
_camera(camera), _ship(ship), _skybox(skybox)
{
}

CameraFollow::~CameraFollow()
{}

void CameraFollow::update(float dt)
{
    // Get current
    vec3 cameraPosition = _camera->getPosition();
    vec3 cameraDirection = _camera->getDirection();
    vec3 cameraUp = _camera->getUp();
    
    // Get target
    vec3 shipPosition = _ship->getPosition();
    vec3 shipForward = _ship->getForward();
    vec3 shipRight = _ship->getRight();
    
    vec3 cameraPositionTarget;
    vec3 cameraDirectionTarget;
    vec3 cameraUpTarget = cross(shipForward, shipRight);
    
    vec3 vecToShip = shipPosition - cameraPosition;
    float distanceFromShip = length(vecToShip);
    if (distanceFromShip > 0) {
        float cosCameraAngle = dot(shipForward, normalize(vecToShip));
        float relativeDistanceFromShip = distanceFromShip * cosCameraAngle;
        if (relativeDistanceFromShip < MIN_CAMERA_DISTANCE){
            cameraPositionTarget = shipPosition - shipForward * MIN_CAMERA_DISTANCE;
            cameraDirectionTarget = shipForward;
        } else {
            float desiredDistanceFromShip = mix(MIN_CAMERA_DISTANCE, MAX_CAMERA_DISTANCE, _ship->getSpeed() / _ship->getMaxSpeed());
            float distanceFromDesiredDistance = relativeDistanceFromShip - desiredDistanceFromShip;
            float distanceFromTarget = distanceFromDesiredDistance / FOLLOW_PERCENT;
            
            float targetOffset = distanceFromTarget - distanceFromShip;
            cameraPositionTarget = shipPosition + shipForward * targetOffset;
            cameraDirectionTarget = shipForward;
        }
    }
    
    
    // Interpolate
    vec3 cameraPositionInterpolated = mix(cameraPosition, cameraPositionTarget, FOLLOW_PERCENT);
    vec3 cameraDirectionInterpolated = mix(cameraDirection, cameraDirectionTarget, FOLLOW_PERCENT);
    vec3 cameraUpInterpolated = mix(cameraUp, cameraUpTarget, FOLLOW_PERCENT);
        
    _camera->setPosition(cameraPositionInterpolated);
    _camera->setDirection(cameraDirectionInterpolated);
    _camera->setUp(cameraUpInterpolated);
    
    // Also, move the skybox
    _skybox->setPosition(cameraPositionInterpolated);
}<commit_msg>You can never really be done with camerafollow...<commit_after>//
//  CameraFollow.cpp
//  CGP-Ex3
//
//  Created by <NAME> on 4/27/15.
//  Copyright (c) 2015 <NAME>. All rights reserved.
//

#include "CameraFollow.h"

#define MIN_CAMERA_DISTANCE (4.0f)
#define MAX_CAMERA_DISTANCE (10.0f)
#define MAX_LAG_SPEED (20.0f)
#define POSITION_FOLLOW_PERCENT (0.05f)
#define ROTATION_FOLLOW_PERCENT (0.07f)

CameraFollow::CameraFollow(Camera *camera, Ship *ship, SkyBox *skybox) :
_camera(camera), _ship(ship), _skybox(skybox)
{
}

CameraFollow::~CameraFollow()
{}

void CameraFollow::update(float dt)
{
    // Get current
    vec3 cameraPosition = _camera->getPosition();
    vec3 cameraDirection = _camera->getDirection();
    vec3 cameraUp = _camera->getUp();
    
    // Get target
    vec3 shipPosition = _ship->getPosition();
    vec3 shipForward = _ship->getForward();
    vec3 shipRight = _ship->getRight();
    
    vec3 cameraPositionTarget;
    vec3 cameraDirectionTarget;
    vec3 cameraUpTarget = cross(shipForward, shipRight);
    
    vec3 vecToShip = shipPosition - cameraPosition;
    float distanceFromShip = length(vecToShip);
    if (distanceFromShip > 0) {
        float cosCameraAngle = dot(shipForward, normalize(vecToShip));
        float relativeDistanceFromShip = distanceFromShip * cosCameraAngle;
        if (relativeDistanceFromShip < MIN_CAMERA_DISTANCE){
            cameraPositionTarget = shipPosition - shipForward * MIN_CAMERA_DISTANCE;
            cameraDirectionTarget = shipForward;
        } else {
            float desiredDistanceFromShip = mix(MIN_CAMERA_DISTANCE, MAX_CAMERA_DISTANCE, _ship->getSpeed() / _ship->getMaxSpeed());
            float distanceFromDesiredDistance = relativeDistanceFromShip - desiredDistanceFromShip;
            float distanceFromTarget = distanceFromDesiredDistance / POSITION_FOLLOW_PERCENT;
            
            float targetOffset = distanceFromTarget - distanceFromShip;
            cameraPositionTarget = shipPosition + shipForward * targetOffset;
            cameraDirectionTarget = shipForward;
        }
    }
    
    
    // Interpolate
    vec3 cameraPositionInterpolated = mix(cameraPosition, cameraPositionTarget, POSITION_FOLLOW_PERCENT);
    vec3 cameraDirectionInterpolated = mix(cameraDirection, cameraDirectionTarget, ROTATION_FOLLOW_PERCENT);
    vec3 cameraUpInterpolated = mix(cameraUp, cameraUpTarget, ROTATION_FOLLOW_PERCENT);
        
    _camera->setPosition(cameraPositionInterpolated);
    _camera->setDirection(cameraDirectionInterpolated);
    _camera->setUp(cameraUpInterpolated);
    
    // Also, move the skybox
    _skybox->setPosition(cameraPositionInterpolated);
}
{'fraction_non_alphanumeric': 0.09265298292729714, 'fraction_numerical': 0.014003452906196048, 'mean_word_length': 3.701532912533814, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 33, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7523025', 'n_tokens_mistral': 1579, 'n_tokens_neox': 1552, 'n_words': 306}