Example #1
0
    static public ViQuaternion FromAxisAngle(ViVector3 axis, float radians)
    {
        axis.Normalize();
        ViVector3 scaledAxis = axis * (float)Math.Sin(radians * 0.5f);

        return(new ViQuaternion(scaledAxis.x, scaledAxis.y, scaledAxis.z, (float)Math.Cos(radians * 0.5f)));
    }
Example #2
0
    public override void _Update(float deltaTime, ViVector3 target)
    {
        ViVector3 dir = target - Translate;

        dir.Normalize();
        _velocity  = dir * _speed;
        _direction = dir;
    }
Example #3
0
    public override void _Update(float deltaTime, ViVector3 target)
    {
        ViVector3 diffDir = target - Translate;

        diffDir.Normalize();
        if (_direction == diffDir)
        {
            _velocity = _direction * _speed;
        }
        else
        {
            ViVector3 rotateAxis = ViVector3.Cross(_direction, diffDir);
            rotateAxis.Normalize();
            const float STABLE = 0.0001f;
            // 计算公式与变量定义
            // V 线速度
            // W 角速度
            // A 侧向加速度
            // R 运动半径
            // W = V/R;
            // A = (V*V)/R = W*W*R = V*W;
            float angleDiff        = ViVector3.Angle(diffDir, _direction);
            float destW            = 4.0f * Math.Abs((angleDiff + STABLE) / (_duration + STABLE));
            float destA            = destW * Speed;
            float destLateralAngle = (float)Math.Atan2(destA, _gravity);
            //
            _rollSpd = 3.0f * (destLateralAngle - _lateralAngle + STABLE) / (_duration + STABLE);
            if (destLateralAngle > _lateralAngle)
            {
                _lateralAngle = ViMathDefine.MoveTowards(_lateralAngle, destLateralAngle, _rollSpd * deltaTime);
            }
            else
            {
                _lateralAngle = destLateralAngle;
            }
            float currentA   = (float)Math.Tan(_lateralAngle) * _gravity;
            float currentW   = currentA / Speed;
            float deltaAngle = currentW * deltaTime;
            //
            ViQuaternion rotateQuat = ViQuaternion.FromAxisAngle(rotateAxis, deltaAngle);
            ViVector3    newDir     = rotateQuat * _direction;
            newDir.Normalize();
            _velocity = (newDir + _direction) * _speed * 0.5f;
            if (ViVector3.Dot(ViVector3.Cross(_direction, newDir), ViVector3.Cross(newDir, diffDir)) < 0.0f)            // 插值抖动
            {
                _lateralSign = 0.0f;
                _direction   = diffDir;
            }
            else
            {
                _direction   = newDir;
                _lateralSign = (rotateAxis.z > 0.0f) ? 1.0f : -1.0f;
            }
        }
    }
Example #4
0
    public static void Test()
    {
        ViDirectionCursor cursor = new ViDirectionCursor();

        cursor.Direction = new ViVector3(0, 0, 1);
        ViVector3 newDir = new ViVector3(-1, 0, 1);

        newDir.Normalize();
        while (cursor.Update(0.03f, newDir) == true)
        {
            //Debug.Print("Direction >> " + cursor.Direction);
        }
    }
Example #5
0
 public void GetAxisAngle(out ViVector3 axis, out float radians)
 {
     radians = (float)Math.Acos(this.w);
     if (radians != 0)
     {
         axis  = new ViVector3(this.x, this.y, this.z);
         axis /= (float)Math.Sin(radians);
         axis.Normalize();
         radians *= 2;
     }
     else
     {
         axis = ViVector3.UNIT_X;
     }
 }
Example #6
0
    public static void Convert(ViVector3 diretion, float roll, out ViVector3 horizDir, out ViVector3 normal)
    {
        horizDir   = diretion;
        horizDir.z = 0.0f;
        horizDir.Normalize();
        diretion.Normalize();
        ViVector3 rotateAxis = ViVector3.Cross(diretion, ViVector3.UNIT_Z);

        rotateAxis.Normalize();
        ViQuaternion verticalRotateQuat = ViQuaternion.FromAxisAngle(rotateAxis, ViMathDefine.PI_HALF);

        normal = verticalRotateQuat * diretion;
        ViQuaternion rollRotateQuat = ViQuaternion.FromAxisAngle(diretion, roll);

        normal = rollRotateQuat * normal;
    }
Example #7
0
    public bool Update(float deltaTime, ViVector3 newDir)
    {
        if (_curDiretion == newDir)
        {
            return(false);
        }
        float angleDiff = ViVector3.Angle(newDir, _curDiretion);
        float rotAngle  = _rotSpd * deltaTime;

        if (angleDiff <= rotAngle)
        {
            _curDiretion = newDir;
        }
        else
        {
            ViVector3 rotateAxis = ViVector3.Cross(_curDiretion, newDir);
            rotateAxis.Normalize();
            ViQuaternion rotateQuat = ViQuaternion.FromAxisAngle(rotateAxis, rotAngle);
            _curDiretion = rotateQuat * _curDiretion;
        }
        return(true);
    }
Example #8
0
    public override void _Update(float deltaTime, ViVector3 target)
    {
        ViDebuger.AssertWarning(deltaTime > 0.0f);
        ViDebuger.AssertWarning(_speed > 0.0f);
        //
        //
        ViVector3 targetPos = Target.Value;
        float     distanceH = ViMath2D.Length(targetPos.x, targetPos.y, Translate.x, Translate.y);
        float     distanceV = targetPos.z - Translate.z;
        float     time      = distanceH / _speed;

        m_GravityAcc = -2.0f * (distanceV / (time * time) - _velocity.z / time);
        m_GravityAcc = ViMathDefine.Clamp(m_GravityAcc, -GRAVITY, 5.0f * GRAVITY);
        ViVector3 kDir = targetPos - Translate;

        kDir.z = 0.0f;
        kDir.Normalize();
        _velocity.x  = kDir.x * _speed;
        _velocity.y  = kDir.y * _speed;
        _velocity.z -= m_GravityAcc * deltaTime;
        _direction   = _velocity;
        _direction.Normalize();
    }