Exemple #1
0
        private static Quaternion T3dRotatorToQuaternion(T3dRotator rotator)
        {
            // 227 variant:
            float cosp = Mathf.Cos(rotator.Pitch * 0.0000479369f);
            float cosy = Mathf.Cos(rotator.Yaw * 0.0000479369f);
            float cosr = Mathf.Cos(rotator.Roll * 0.0000479369f);
            float sinp = Mathf.Sin(rotator.Pitch * 0.0000479369f);
            float siny = Mathf.Sin(rotator.Yaw * 0.0000479369f);
            float sinr = Mathf.Sin(rotator.Roll * 0.0000479369f);

            Quaternion quaternion;

            quaternion.w = cosp * cosy * cosr + sinp * siny * sinr;
            quaternion.z = sinp * cosy * cosr + cosp * siny * sinr;
            quaternion.y = cosp * siny * cosr - sinp * cosy * sinr;
            quaternion.x = cosp * cosy * sinr - sinp * siny * cosr;

            float L = Mathf.Sqrt(Mathf.Pow(quaternion.w, 2) + Mathf.Pow(quaternion.x, 2) + Mathf.Pow(quaternion.y, 2) + Mathf.Pow(quaternion.z, 2));

            quaternion.w /= L;
            quaternion.x /= L;
            quaternion.y /= L;
            quaternion.z /= L;

            quaternion.z = -quaternion.z;

            return(quaternion);
        }
Exemple #2
0
        /// <summary>
        /// Tries to parse a property.
        /// </summary>
        /// <param name="line">The line to be parsed (e.g. 'MaxCarcasses=50').</param>
        /// <param name="key">The property key.</param>
        /// <param name="value">The property value.</param>
        /// <returns>True if successful else false.</returns>
        private bool TryParseProperty(string line, out string key, out object value)
        {
            key   = null;
            value = null;
            // find the first occurence of the '=' character.
            int splitIndex = line.IndexOf('=');

            if (splitIndex == -1)
            {
                return(false);                  // early exit.
            }
            // get key and the value (as string).
            key = line.Substring(0, splitIndex);
            if (key.Contains(' '))
            {
                return(false);                   // properties don't have spaces.
            }
            string rawvalue = line.Substring(splitIndex + 1);

            // determine and parse the value type.
            int   integer;
            float number;

            // string value has been detected.
            if (rawvalue[0] == '"')
            {
                // remove quotes.
                value = rawvalue.Substring(1, rawvalue.Length - 2);
                return(true);
            }
            // complex struct has been detected.
            else if (rawvalue[0] == '(')
            {
                // rotator has been detected.
                if (rawvalue[1] == 'P' || (rawvalue[1] == 'Y' && rawvalue[2] == 'a') || rawvalue[1] == 'R')
                {
                    int pi = rawvalue.IndexOf("Pitch=");
                    int yi = rawvalue.IndexOf("Yaw=");
                    int ri = rawvalue.IndexOf("Roll=");
                    if (pi == -1 && yi == -1 && ri == -1)
                    {
                        return(false);
                    }

                    int v1 = 0;
                    int v2 = 0;
                    int v3 = 0;

                    // pitch.
                    if (pi != -1)
                    {
                        int len = rawvalue.IndexOf(',', pi);
                        if (len == -1)
                        {
                            len = rawvalue.IndexOf(')', pi);
                        }
                        string xs = rawvalue.Substring(pi + 6, len - 1 - 6);
                        Int32.TryParse(xs, out v1);
                    }

                    // yaw.
                    if (yi != -1)
                    {
                        int len = rawvalue.IndexOf(',', yi);
                        if (len == -1)
                        {
                            len = rawvalue.IndexOf(')', yi);
                        }
                        string ys = rawvalue.Substring(yi + 4, len - yi - 4);
                        Int32.TryParse(ys, out v2);
                    }

                    // roll.
                    if (ri != -1)
                    {
                        int len = rawvalue.IndexOf(',', ri);
                        if (len == -1)
                        {
                            len = rawvalue.IndexOf(')', ri);
                        }
                        string zs = rawvalue.Substring(ri + 5, len - ri - 5);
                        Int32.TryParse(zs, out v3);
                    }

                    value = new T3dRotator(v1, v2, v3);
                    return(true);
                }
                // vector has been detected.
                if (rawvalue[1] == 'X' || rawvalue[1] == 'Y' || rawvalue[1] == 'Z')
                {
                    int xi = rawvalue.IndexOf("X=");
                    int yi = rawvalue.IndexOf("Y=");
                    int zi = rawvalue.IndexOf("Z=");
                    if (xi == -1 && yi == -1 && zi == -1)
                    {
                        return(false);
                    }

                    float v1 = 0.0f;
                    float v2 = 0.0f;
                    float v3 = 0.0f;

                    // x-coordinate.
                    if (xi != -1)
                    {
                        int len = rawvalue.IndexOf(',', xi);
                        if (len == -1)
                        {
                            len = rawvalue.IndexOf(')', xi);
                        }
                        string xs = rawvalue.Substring(xi + 2, len - 1 - 2);
                        float.TryParse(xs, out v1);
                    }

                    // y-coordinate.
                    if (yi != -1)
                    {
                        int len = rawvalue.IndexOf(',', yi);
                        if (len == -1)
                        {
                            len = rawvalue.IndexOf(')', yi);
                        }
                        string ys = rawvalue.Substring(yi + 2, len - yi - 2);
                        float.TryParse(ys, out v2);
                    }

                    // z-coordinate.
                    if (zi != -1)
                    {
                        int len = rawvalue.IndexOf(',', zi);
                        if (len == -1)
                        {
                            len = rawvalue.IndexOf(')', zi);
                        }
                        string zs = rawvalue.Substring(zi + 2, len - zi - 2);
                        float.TryParse(zs, out v3);
                    }

                    value = new T3dVector3(v1, v2, v3);
                    return(true);
                }
                // scale has been detected.
                if (rawvalue[1] == 'S' && rawvalue[2] != 'h')
                {
                    // (Scale=(X=0.173205,Y=0.150000,Z=0.125000),SheerAxis=SHEER_ZX)
                    string x = rawvalue.Replace("(Scale=", "X=").Replace(",SheerAxis=SHEER_ZX)", "");
                    string k;
                    object v;
                    if (TryParseProperty(x, out k, out v))
                    {
                        T3dVector3 vec = (T3dVector3)v;
                        vec.X = vec.X == 0.0f ? 1.0f : vec.X;
                        vec.Y = vec.Y == 0.0f ? 1.0f : vec.Y;
                        vec.Z = vec.Z == 0.0f ? 1.0f : vec.Z;
                        value = vec; // vector.
                        return(true);
                    }
                }
            }
            // floating point number has been detected.
            else if (rawvalue.Contains('.') && float.TryParse(rawvalue, out number))
            {
                value = number;
                return(true);
            }
            // integer number has been detected.
            else if (int.TryParse(rawvalue, out integer))
            {
                value = integer;
                return(true);
            }
            // constant enum has been detected.
            else
            {
                value = rawvalue;
                return(true);
            }

            return(false);
        }