Esempio n. 1
0
    private void UpdateRigidbodyReferences()
    {
        GameObject[] gameObjects = UnityEngine.SceneManagement.SceneManager.GetActiveScene().GetRootGameObjects();
        if (gameObjects.Length != _previousGameObjectCount)
        {
            ResetRigidbodies();

            List <Rigidbody> rigidbodies = new List <Rigidbody>();
            for (int i = 0; i < gameObjects.Length; ++i)
            {
                Rigidbody rigidbody = gameObjects[i].GetComponentInChildren <Rigidbody>(false);
                if (rigidbody != null)
                {
                    rigidbodies.Add(rigidbody);
                }
            }

            _rigidbodyData = new RigidbodyData[rigidbodies.Count];
            for (int i = 0; i < rigidbodies.Count; ++i)
            {
                _rigidbodyData[i] = new RigidbodyData(rigidbodies[i]);
            }
            _previousGameObjectCount = gameObjects.Length;
        }
    }
    public void Resume()
    {
        Cursor.visible = false;
        PauseScreen.SetActive(false);
        for (int i = 0; i < m_NavMeshes.Count; i++)
        {
            m_NavMeshes[i].speed = 2f;
        }
        for (int r = 0; r < m_Rigidbodies.Count; r++)
        {
            // Fetch data
            RigidbodyData _data = m_RigidbodyData.Find(i => i.InstanceID == m_Rigidbodies[r].GetInstanceID());

            // Set data back
            m_Rigidbodies[r].velocity    = _data.Velocity;
            m_Rigidbodies[r].drag        = _data.Drag;
            m_Rigidbodies[r].angularDrag = _data.AngularDrag;

            // Resume physics
            m_Rigidbodies[r].isKinematic = false;
            m_Rigidbodies[r].WakeUp();
        }
        for (int i = 0; i < m_Walkers.Count; i++)
        {
            m_Walkers[i].enabled = true;
        }
        m_RigidbodyData.Clear();
        IsPaused = false;
    }
Esempio n. 3
0
    public void Pause()
    {
        for (int r = 0; r < m_Rigidbodies.Count; r++)
        {
            // Store backup data
            RigidbodyData _data = new RigidbodyData()
            {
                InstanceID  = m_Rigidbodies[r].GetInstanceID(),
                Velocity    = m_Rigidbodies[r].velocity,
                Drag        = m_Rigidbodies[r].drag,
                AngularDrag = m_Rigidbodies[r].angularDrag
            };
            m_RigidbodyData.Add(_data);

            // Reset values
            m_Rigidbodies[r].velocity    = Vector3.zero;
            m_Rigidbodies[r].drag        = 0;
            m_Rigidbodies[r].angularDrag = 0;

            // Paused
            m_Rigidbodies[r].isKinematic = true;
            m_Rigidbodies[r].Sleep();
        }

        IsPaused = true;
    }
Esempio n. 4
0
    ApplyLerp(Component targetComponent, ComponentData rightData, float factor)
    {
        Rigidbody     body  = (Rigidbody)targetComponent;
        RigidbodyData right = (RigidbodyData)rightData;

        body.transform.position = this.position.Lerp(right.position, factor);
        body.transform.rotation = this.rotation.Lerp(right.rotation, factor);
    }
    public void SetFromState(RigidbodyData data)
    {
        //DLog.Log("setting from state1: frame: " + data.frame + " - pos: " + data.position);

        r.velocity        = data.velocity;
        r.position        = data.position;
        r.rotation        = data.rotation;
        r.angularVelocity = data.angularVelocity;

        this.transform.position = data.position;
        this.transform.rotation = data.rotation;
    }
    private void Awake()
    {
        if (ship)
        {
            data = GameSettings.Instance.GetShipRigidbodyData(shipType);
        }
        else
        {
            data = GameSettings.Instance.GetProjectileRigidbodyData(shooterType);
        }

        rb             = GetComponent <Rigidbody2D>();
        rb.mass        = data.Mass;
        rb.drag        = data.LinearDrag;
        rb.angularDrag = data.AngularDrag;
    }
    public bool StatesMatch(RigidbodyData a, RigidbodyData b)
    {
        //just do a position distance check for now..
        float distanceThreshold = 1f;
        float dt = Vector3.Distance(a.position, b.position);

        DLog.Log("--- StatesMatch:: " + (dt <= distanceThreshold));
        DLog.Log("local: " + a.position.ToString());
        DLog.Log("validated: " + b.position.ToString());
        if (dt <= distanceThreshold)
        {
            return(true);
        }
        else
        {
            return(false);
        }
    }
Esempio n. 8
0
    public void Resume()
    {
        for (int r = 0; r < m_Rigidbodies.Count; r++)
        {
            // Fetch data
            RigidbodyData _data = m_RigidbodyData.Find(i => i.InstanceID == m_Rigidbodies[r].GetInstanceID());

            // Set data back
            m_Rigidbodies[r].velocity    = _data.Velocity;
            m_Rigidbodies[r].drag        = _data.Drag;
            m_Rigidbodies[r].angularDrag = _data.AngularDrag;

            // Resume physics
            m_Rigidbodies[r].isKinematic = false;
            m_Rigidbodies[r].WakeUp();
        }

        m_RigidbodyData.Clear();
        IsPaused = false;
    }
    public void Pause()
    {
        Cursor.visible = true;
        PauseScreen.SetActive(true);
        for (int i = 0; i < m_NavMeshes.Count; i++)
        {
            m_NavMeshes[i].speed = 0;
        }
        for (int r = 0; r < m_Rigidbodies.Count; r++)
        {
            // Store backup data
            RigidbodyData _data = new RigidbodyData()
            {
                InstanceID  = m_Rigidbodies[r].GetInstanceID(),
                Velocity    = m_Rigidbodies[r].velocity,
                Drag        = m_Rigidbodies[r].drag,
                AngularDrag = m_Rigidbodies[r].angularDrag
            };
            m_RigidbodyData.Add(_data);

            // Reset values
            m_Rigidbodies[r].velocity    = Vector3.zero;
            m_Rigidbodies[r].drag        = 0;
            m_Rigidbodies[r].angularDrag = 0;

            // Paused
            m_Rigidbodies[r].isKinematic = true;
            m_Rigidbodies[r].Sleep();
        }
        for (int i = 0; i < m_Walkers.Count; i++)
        {
            m_Walkers[i].enabled = false;
        }

        IsPaused = true;
    }
Esempio n. 10
0
    /// <summary>
    /// Server sends this once this frame has been simulated, along with inputs.  This is the truth at this frame with all inputs applied up to this point
    /// Use this as a base and simulate forward to current time to get local predictions
    /// </summary>
    /// <param name="e"></param>
    public void ReceiveValidation(RigidbodyDataEvent e)
    {
        PhysicsManager.instance.t1.text = "";
        DLog.Log("ReceivedValidationEvent for frame: " + e.frame + " current frame: " + BoltNetwork.serverFrame);
        PhysicsManager.instance.t1.text = "Validation for frame: " + e.frame + " - currentFrame: " + BoltNetwork.serverFrame;
        if (BoltNetwork.isServer)
        {
            return;                      //we don't need to validate server stuff, so we can skip all this jumk
        }
        if (e.frame > BoltNetwork.serverFrame)
        {
            //this can happen because BoltNetwork.serverFrame isn't exact on clients.
            //was happening with 1000ms pings
            DLog.Log("ReceivedValidationEvent for a frame that hasn't been simulated locally yet??");
        }

        //we can go through and discard any state date older than this validation


        validatedStateFrame = e.frame;
        validatedStateData  = new RigidbodyData()
        {
            frame           = e.frame,
            position        = e.position,
            rotation        = e.rotation,
            velocity        = e.velocity,
            angularVelocity = e.angularVelocity
        };
        //probably a better way to do this. Lazy
        SortedDictionary <int, RigidbodyData> temp = new SortedDictionary <int, RigidbodyData>();

        foreach (KeyValuePair <int, RigidbodyData> d in data)
        {
            if (d.Value.frame >= e.frame)
            {
                temp.Add(d.Key, d.Value);
            }
        }
        data = temp;

        NetworkedBubbleControllerBehaviour c = this.GetComponent <NetworkedBubbleControllerBehaviour>(); //this should be generalized ControllableRigidbody

        if (c != null)
        {
            c.CleanupOldInputs(e.frame);
        }

        ////when we receive a validation, check that against our local - stored values to see if our past frame was close enough to it.
        ////if we are not, we need to resync and resim.

        ////turning this on here just applies the states late as we get them, without any simulation
        //SetFromState(validatedStates[e.frame]);

        //do we even have a local frame data for this frame?
        bool hasLocalData = data.ContainsKey(e.frame);

        if (hasLocalData)
        {
            //is localData CLOSE to validatedData?
            if (StatesMatch(data[e.frame], validatedStateData))
            {
                //don't do anythign, we're mostly accurate ?
                //if we ARE close though, could we not just snap anyways because
                //1) We're close, so it won't be an abrupt snap
                //2) It will make the next step be more likely to be similar
                //like fixing the small drifts rather than fixing the big ones every once in a while?
                //DLog.Log("PRD::RecValidation - Close enough!");
                PhysicsManager.instance.NeedsLocalResim(e.frame);
            }
            else
            {
                //too far apart, fix this.
                DLog.Log("PRD::RecValidation - states too far apart, marking for resim");
                PhysicsManager.instance.NeedsLocalResim(e.frame);
            }
        }
        else
        {
            DLog.Log("PRD::RecValidation - don't have frame data, marking for resim");
            //don't even have data, so we need to resim anyways.
            PhysicsManager.instance.NeedsLocalResim(e.frame);
        }
    }