Пример #1
0
    // [uint16(length) MessagePayload uint8(checksum)]
    // MessagePayload = { uint8(WORLD_STATE) {[R,V] ...} }

    void OnCollisionEnter(Collision collision)
    {
        if (m_subscribeCollisionBegin)
        {
            int         ct = 0;
            byte[]      temp;
            WorldObject wo = collision.gameObject.GetComponent <WorldObject>();
            string      id2;
            if (wo != null)
            {
                id2 = wo.m_id;
            }
            else
            {
                id2 = GetParentId(collision.gameObject.transform.parent);
            }

            Vector3 point;
            Vector3 normal           = collision.contacts[0].normal;
            Vector3 relativeVelocity = collision.relativeVelocity;

            // R = uint8(COLLISION_BEGIN)
            // V = [(len string) (len string) (numContacts = 1) 3*single 3*single]
            temp = new byte[sizeof(byte) + (1 + m_id.Length) + (1 + id2.Length) + 2 * sizeof(byte) + 9 * sizeof(float)];

            temp[ct] = Convert.ToByte(WorldInterface.ReportType.COLLISION_BEGIN);
            ct++;

            temp[ct] = Convert.ToByte(m_id.Length);
            ct++;
            for (int i = 0; i < m_id.Length; i++)
            {
                temp[ct + i] = Convert.ToByte(m_id[i]);
            }
            ct = ct + m_id.Length;

            temp[ct] = Convert.ToByte(id2.Length);
            ct++;
            for (int i = 0; i < id2.Length; i++)
            {
                temp[ct + i] = Convert.ToByte(id2[i]);
            }
            ct = ct + id2.Length;

            temp[ct] = 1;       // number of contacts is fixed at 1
            ct       = ct + sizeof(byte);

            if (m_worldCoordinates)
            {
                point = collision.contacts[0].point;
            }
            else
            {
                point = this.transform.InverseTransformPoint(collision.contacts[0].point);
            }

            // Negate x to convert to a right handed coordinate frame.
            Array.Copy(BitConverter.GetBytes(-point.x), 0, temp, ct, sizeof(float));
            ct = ct + sizeof(float);
            Array.Copy(BitConverter.GetBytes(point.y), 0, temp, ct, sizeof(float));
            ct = ct + sizeof(float);
            Array.Copy(BitConverter.GetBytes(point.z), 0, temp, ct, sizeof(float));
            ct = ct + sizeof(float);

            temp[ct] = 1;       // number of contacts is fixed at 1
            ct       = ct + sizeof(byte);

            // Negate x to convert to a right handed coordinate frame.
            Array.Copy(BitConverter.GetBytes(-normal.x), 0, temp, ct, sizeof(float));
            ct = ct + sizeof(float);
            Array.Copy(BitConverter.GetBytes(normal.y), 0, temp, ct, sizeof(float));
            ct = ct + sizeof(float);
            Array.Copy(BitConverter.GetBytes(normal.z), 0, temp, ct, sizeof(float));
            ct = ct + sizeof(float);

            // Negate x to convert to a right handed coordinate frame.
            Array.Copy(BitConverter.GetBytes(-relativeVelocity.x), 0, temp, ct, sizeof(float));
            ct = ct + sizeof(float);
            Array.Copy(BitConverter.GetBytes(relativeVelocity.y), 0, temp, ct, sizeof(float));
            ct = ct + sizeof(float);
            Array.Copy(BitConverter.GetBytes(relativeVelocity.z), 0, temp, ct, sizeof(float));
            ct = ct + sizeof(float);

            m_worldIface.AppendWorldStateMessage(temp);
        }
    }