Lidgren.Network.NetBuffer.WriteSignedSingle C# (CSharp) Méthode

WriteSignedSingle() public méthode

Compress (lossy) a float in the range -1..1 using numberOfBits bits
public WriteSignedSingle ( float value, int numberOfBits ) : void
value float
numberOfBits int
Résultat void
        public void WriteSignedSingle(float value, int numberOfBits)
        {
            NetException.Assert(((value >= -1.0) && (value <= 1.0)), " WriteSignedSingle() must be passed a float in the range -1 to 1; val is " + value);

            float unit = (value + 1.0f) * 0.5f;
            int maxVal = (1 << numberOfBits) - 1;
            uint writeVal = (uint)(unit * (float)maxVal);

            Write(writeVal, numberOfBits);
        }

Usage Example

        /// <summary>
        /// Writes a unit vector (ie. a vector of length 1.0, for example a surface normal)
        /// using specified number of bits
        /// </summary>
        public static void WriteUnitVector3(this NetBuffer message, Vector3 unitVector, int numberOfBits)
        {
            float  x     = unitVector.x;
            float  y     = unitVector.y;
            float  z     = unitVector.z;
            double invPi = 1.0 / Math.PI;
            float  phi   = (float)(Math.Atan2(x, y) * invPi);
            float  theta = (float)(Math.Atan2(z, Math.Sqrt(x * x + y * y)) * (invPi * 2));

            int halfBits = numberOfBits / 2;

            message.WriteSignedSingle(phi, halfBits);
            message.WriteSignedSingle(theta, numberOfBits - halfBits);
        }
All Usage Examples Of Lidgren.Network.NetBuffer::WriteSignedSingle