Beispiel #1
0
        // Update is called once per frame
        void Update()
        {
            liveObjects.Clear();
            foreach (SynchronizedObject sync in synchronizedObjects)
            {
                if (string.IsNullOrEmpty(sync.label))
                {
                    Debug.LogWarning("Warning! There is an unlabeled object in the synchronized object pool.");
                    continue;
                }


                LiveObjectStorage storage = new LiveObjectStorage(sync.label);
                storage.position = sync.position;
                storage.rotation = sync.rotation;
                storage.bits     = sync.bits;
                storage.blob     = sync.blob;

                liveObjects.Add(storage.label, storage);
            }
        }
Beispiel #2
0
        //public Vector2 getLiveObjectAxisButton(string name, int index) {
        //     LiveObjectStorage storage;
        //     lock (lockObject) {
        //          if (!liveObjects.TryGetValue(name, out storage)) {
        //               //print ("Body not found: " + name);
        //               return Vector2.zero;
        //          }
        //     }
        //     return storage.axisButtons[index];
        //}

        ///////////////////////////////////////////////////////////////////////////
        //
        // Thread methods
        //

        // This thread handles incoming NatNet packets.
        private void run()
        {
            Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);

            socket.Bind(new IPEndPoint(IPAddress.Any, BLACK_BOX_CLIENT_PORT));
            socket.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.AddMembership, new MulticastOption(IPAddress.Parse("224.1.1.1")));

            int nBytesReceived = 0;

            while (receivingPackets)
            {
                nBytesReceived = socket.Receive(currentPacket.bytes);
                currentPacket.stream.Position = 0;

                update = Serializer.Deserialize <update_protocol_v3.Update>(new MemoryStream(currentPacket.bytes, 0, nBytesReceived));

                currentPacket.frame = update.mod_version;
                if (currentPacket.frame > previousPacket.frame)
                {
                    packetCount++;

                    previousPacket.stream.Position = 0;
                    currentPacket.stream.Position  = 0;
                    tempPacket.copyFrom(previousPacket);
                    previousPacket.copyFrom(currentPacket);
                    currentPacket.copyFrom(tempPacket);
                    for (int j = 0; j < update.live_objects.Count; j++)
                    {
                        LiveObject or    = update.live_objects[j];
                        string     label = or.label;

                        LiveObjectStorage ow;
                        lock (lockObject) {
                            //List<LiveObjectStorage> objectsToRemove = new List<LiveObjectStorage>();
                            //objectsToRemove.AddRange(liveObjects.Values);

                            //Add objects that exist
                            if (!liveObjects.TryGetValue(label, out ow))
                            {
                                ow = new LiveObjectStorage(label);
                                liveObjects[label] = ow;
                            }
                            else
                            {
                                ow = liveObjects[label];
                                //objectsToRemove.Remove(ow);
                            }
                            if (update.lhs_frame)
                            {
                                ow.position = new Vector3(-(float)or.x, (float)or.y, (float)or.z);
                                ow.rotation = new Quaternion(-(float)or.qx, (float)or.qy, (float)or.qz, -(float)or.qw);
                            }
                            else
                            {
                                ow.position = new Vector3((float)or.x, (float)or.y, (float)or.z);
                                ow.rotation = new Quaternion((float)or.qx, (float)or.qy, (float)or.qz, (float)or.qw);
                            }
                            ow.bits = or.button_bits;

                            //Remove objects from pool that aren't there.
                            //foreach (LiveObjectStorage missingObject in objectsToRemove) {
                            //     liveObjects.Remove(missingObject.key);
                            //}
                        }
                    }
                }

                if (!receivingPackets)
                {
                    socket.Close();
                    break;
                }
            }
        }