示例#1
0
        public HkMassProperties UpdateMass()
        {
            HkMassElement element = new HkMassElement {
                Tranform = Matrix.Identity
            };
            bool flag = false;

            foreach (Vector3I vectori in base.DirtyCells)
            {
                MySparseGrid <HkMassElement, MassCellData> .Cell cell;
                if (!base.TryGetCell(vectori, out cell))
                {
                    flag = true;
                    continue;
                }
                float num = 0f;
                foreach (KeyValuePair <Vector3I, HkMassElement> pair in cell.Items)
                {
                    TmpElements.Add(pair.Value);
                    num += pair.Value.Properties.Mass;
                }
                if (Math.Abs((float)(1f - (cell.CellData.LastMass / num))) > this.m_updateThreshold)
                {
                    element.Properties        = InertiaComputer.CombineMassPropertiesInstance(TmpElements);
                    cell.CellData.MassElement = element;
                    cell.CellData.LastMass    = num;
                    flag = true;
                }
                TmpElements.Clear();
            }
            base.UnmarkDirtyAll();
            if (flag)
            {
                foreach (KeyValuePair <Vector3I, MySparseGrid <HkMassElement, MassCellData> .Cell> pair2 in this)
                {
                    TmpElements.Add(pair2.Value.CellData.MassElement);
                }
                this.m_massProperties = (TmpElements.Count <= 0) ? new HkMassProperties() : InertiaComputer.CombineMassPropertiesInstance(TmpElements);
                TmpElements.Clear();
            }
            return(this.m_massProperties);
        }
示例#2
0
 public HkMassProperties CombineMassProperties(List <HkMassElement> elements) =>
 InertiaComputer.CombineMassPropertiesInstance(elements);
        public HkMassProperties UpdateMass()
        {
            HkMassElement element = new HkMassElement {
                Tranform = Matrix.Identity
            };
            bool massChanged = false;

            ProfilerShort.Begin("UpdateDirty", DirtyCells.Count);
            //TODO:we can limit # of recalculated dirty cells per update for perf++
            foreach (var dirtyCell in DirtyCells)
            {
                MySparseGrid <HkMassElement, MassCellData> .Cell cell;
                if (TryGetCell(dirtyCell, out cell))
                {
                    float cellMass = 0;
                    foreach (var item in cell.Items)
                    {
                        TmpElements.Add(item.Value);
                        cellMass += item.Value.Properties.Mass;
                    }

                    //Ignore "unsignificant" changes
                    if (Math.Abs(1 - cell.CellData.LastMass / cellMass) > m_updateThreshold)
                    {
                        element.Properties        = InertiaComputer.CombineMassPropertiesInstance(TmpElements);
                        cell.CellData.MassElement = element;
                        cell.CellData.LastMass    = cellMass;
                        massChanged = true;
                    }
                    TmpElements.Clear();
                }
                else
                {
                    //we have lost a cell & we dont know how much it contributed so update mass
                    massChanged = true;
                }
            }
            ProfilerShort.End();
            UnmarkDirtyAll();

            if (!massChanged)
            {
                return(m_massProperties);
            }

            foreach (var kv in this)
            {
                TmpElements.Add(kv.Value.CellData.MassElement);
            }

            //Debug.Assert(Count > 0, "Mass can't be zero, in that case, grid should not be created");

            // HACK: this prevents crash, but it's generally on wrong place, but we don't know how to handle it higher on call stack
            if (TmpElements.Count > 0)
            {
                m_massProperties = InertiaComputer.CombineMassPropertiesInstance(TmpElements);
            }
            else
            {
                m_massProperties = default(HkMassProperties);
            }
            TmpElements.Clear();
            return(m_massProperties);
        }