Exemplo n.º 1
0
/*! SLCamera::camUpdate does the smooth transition for the walk animation. It
is called in every frame. It moves the camera after the key was released and
smoothly stops the motion by decreasing the speed every frame.
*/
SLbool SLCamera::camUpdate(SLfloat elapsedTimeMS)
{  
    if (_velocity == SLVec3f::ZERO && _moveDir == SLVec3f::ZERO)
        return false;

    SLfloat dtS = elapsedTimeMS * 0.001f;

    SLbool braking = false;
    if (_moveDir != SLVec3f::ZERO)
    {   
        // x and z movement direction vector should be projected on the x,z plane while
        // but still in local space
        // the y movement direction should alway be in world space
        SLVec3f f = forward();
        f.y = 0;
        f.normalize();

        SLVec3f r = right();
        r.y = 0;
        r.normalize();

        _acceleration = f * -_moveDir.z + r * _moveDir.x;
        _acceleration.y = _moveDir.y;
        _acceleration.normalize();
        _acceleration *= _moveAccel;

    } // accelerate in the opposite velocity to brake
    else
    {  
        _acceleration = -_velocity.normalized() * _brakeAccel;
        braking = true;
    }
    
    // accelerate
    SLfloat velMag = _velocity.length();
    SLVec3f increment = _acceleration * dtS; // all units in m/s, convert MS to S
    
    // early out if we're braking and the velocity would fall < 0
    if (braking && increment.lengthSqr() > _velocity.lengthSqr())
    {
        _velocity.set(SLVec3f::ZERO);
        return false;
    }

    _velocity += increment - _drag * _velocity * dtS; 
    velMag = _velocity.length();

    // don't go over max speed
    if (velMag > _maxSpeed)
        _velocity = _velocity.normalized() * _maxSpeed;

    // final delta movement vector
    SLVec3f delta = _velocity * dtS;

    // adjust for scaling (if the character is shrinked or enlarged)
    delta *= _unitScaling;

    translate(delta, TS_World);
    
    return true;
    
    //SL_LOG("cs: %3.2f | %3.2f, %3.2f, %3.2f\n", _velocity.length(), _acceleration.x, _acceleration.y, _acceleration.z);

    /* OLD CODE BELOW
    // ToDo: The recursive update traversal is not yet implemented
    if (_maxSpeed != SLVec3f::ZERO || _curSpeed != SLVec3f::ZERO)
    {  
        // delta speed during acceleration/slow down
        SLfloat ds = _speedLimit / 20.0f;
      
        // Accelerate
        if (_maxSpeed.x>0 && _curSpeed.x<_maxSpeed.x) _curSpeed.x += ds; else
        if (_maxSpeed.x<0 && _curSpeed.x>_maxSpeed.x) _curSpeed.x -= ds;
        if (_maxSpeed.y>0 && _curSpeed.y<_maxSpeed.y) _curSpeed.y += ds; else
        if (_maxSpeed.y<0 && _curSpeed.y>_maxSpeed.y) _curSpeed.y -= ds;      
        if (_maxSpeed.z>0 && _curSpeed.z<_maxSpeed.z) _curSpeed.z += ds; else
        if (_maxSpeed.z<0 && _curSpeed.z>_maxSpeed.z) _curSpeed.z -= ds;
      
        if (_curSpeed.z == 0.0f) {
            int i = 0;
        }

        // Slow down
        if (_maxSpeed.z == 0)
        {   if (_curSpeed.z > 0) 
            {  _curSpeed.z -= ds;
                if (_curSpeed.z < 0) _curSpeed.z = 0.0f;
            } else
            if (_curSpeed.z < 0) 
            {  _curSpeed.z += ds;
                if (_curSpeed.z > 0) _curSpeed.z = 0.0f;
            }
        }
        if (_maxSpeed.x == 0)
        {   if (_curSpeed.x < 0) 
            {  _curSpeed.x += ds;
                if (_curSpeed.x > 0) _curSpeed.x = 0.0f;
            } else 
            if (_curSpeed.x > 0) 
            {  _curSpeed.x -= ds;
                if (_curSpeed.x < 0) _curSpeed.x = 0.0f;
            }
        }
        if (_maxSpeed.y == 0)
        {   if (_curSpeed.y < 0) 
            {  _curSpeed.y += ds;
                if (_curSpeed.y > 0) _curSpeed.y = 0.0f;
            } else 
            if (_curSpeed.y > 0) 
            {  _curSpeed.y -= ds;
                if (_curSpeed.y < 0) _curSpeed.y = 0.0f;
            }
        }
      
        SL_LOG("cs: %3.1f, %3.1f, %3.1f\n", _curSpeed.x, _curSpeed.y, _curSpeed.z);
        SLfloat temp = _curSpeed.length();

        _curSpeed = updateAndGetWM().mat3() * _curSpeed;
        _curSpeed.y = 0;
        _curSpeed.normalize();
        _curSpeed *= temp;

        forward();

        SLVec3f delta(_curSpeed * elapsedTimeMS / 1000.0f);

        translate(delta, TS_World);
      
        return true;
    }
    return false;*/
}