Esempio n. 1
0
        /// <summary>
        /// Adds the child.
        /// </summary>
        /// <param name="child">The child.</param>
        /// <param name="preserveWorldPos">if set to <c>true</c> [preserve absolute position].</param>
        public void AddChild(Sandbox.ModAPI.IMyEntity child, bool preserveWorldPos = false, bool insertIntoSceneIfNeeded = true)
        {
            //MyEntities.Remove(child);  // if it's already in the world, remove it

            child.Hierarchy.Parent = this;

            if (preserveWorldPos)
            {
                var tmpWorldMatrix = child.WorldMatrix;

                this.Children.Add(child.Hierarchy);

                child.WorldMatrix = tmpWorldMatrix;
            }
            else
            {
                this.Children.Add(child.Hierarchy);
                var m = Entity.PositionComp.WorldMatrix;
                child.PositionComp.UpdateWorldMatrix(ref m);
            }

            if (Entity.InScene && !child.InScene && insertIntoSceneIfNeeded)
            {
                child.OnAddedToScene(this.Entity);
            }
        }
Esempio n. 2
0
 void PhysicsChanged(Sandbox.ModAPI.IMyEntity entity)
 {
     if (entity.Physics == null)
     {
         Detach();
     }
     else if (LockMode == LandingGearMode.Locked)
     {
         m_needsToRetryLock = true;
     }
 }
Esempio n. 3
0
        public void AddChildWithMatrix(Sandbox.ModAPI.IMyEntity child, ref Matrix childLocalMatrix, bool insertIntoSceneIfNeeded = true)
        {
            child.Hierarchy.Parent = this;

            Children.Add(child.Hierarchy);
            child.WorldMatrix = (MatrixD)childLocalMatrix * Entity.PositionComp.WorldMatrix;

            if (Entity.InScene && !child.InScene && insertIntoSceneIfNeeded)
            {
                child.OnAddedToScene(this);
            }
        }
        private void UpdateHitEntity()
        {
            Debug.Assert(m_raycastCollisionResults.Count == 0);

            MatrixD pasteMatrix = GetPasteMatrix();

            MyPhysics.CastRay(pasteMatrix.Translation, pasteMatrix.Translation + pasteMatrix.Forward * m_dragDistance, m_raycastCollisionResults);

            m_closestHitDistSq = float.MaxValue;
            m_hitPos           = new Vector3(0.0f, 0.0f, 0.0f);
            m_hitNormal        = new Vector3(1.0f, 0.0f, 0.0f);
            m_hitEntity        = null;

            foreach (var hit in m_raycastCollisionResults)
            {
                if (hit.HkHitInfo.Body == null)
                {
                    continue;
                }
                MyPhysicsBody body = (MyPhysicsBody)hit.HkHitInfo.Body.UserObject;
                if (body == null)
                {
                    continue;
                }
                Sandbox.ModAPI.IMyEntity entity = body.Entity;
                if ((entity is MyVoxelMap) || (entity is MyCubeGrid && entity.EntityId != PreviewGrids[0].EntityId))
                {
                    if (PreviewGrids[0].GridSizeEnum == MyCubeSize.Large && (entity is MyCubeGrid) && (entity as MyCubeGrid).GridSizeEnum == MyCubeSize.Small)
                    {
                        continue;
                    }

                    float distSq = (float)(hit.Position - pasteMatrix.Translation).LengthSquared();
                    if (distSq < m_closestHitDistSq)
                    {
                        m_closestHitDistSq = distSq;
                        m_hitPos           = hit.Position;
                        m_hitNormal        = hit.HkHitInfo.Normal;
                        m_hitEntity        = entity;
                    }
                }
            }

            m_raycastCollisionResults.Clear();
        }
Esempio n. 5
0
        private void Detach()
        {
            if (m_constraint == null)
            {
                return;
            }

            this.OnPhysicsChanged -= m_physicsChangedHandler;

            var tmpAttachedEntity = m_attachedTo;

            Debug.Assert(m_attachedTo != null, "Attached entity is null");
            if (m_attachedTo != null)
            {
                m_attachedTo.OnPhysicsChanged -= m_physicsChangedHandler;
            }
            m_attachedTo = null;

            if (!m_needsToRetryLock && !MarkedForClose)
            {
                StartSound(m_unlockSound);
            }

            CubeGrid.Physics.RemoveConstraint(m_constraint);

            m_constraint.Dispose();
            m_constraint = null;

            LockMode = LandingGearMode.Unlocked;
            OnConstraintRemoved(GridLinkTypeEnum.Physical, tmpAttachedEntity);
            OnConstraintRemoved(GridLinkTypeEnum.NoContactDamage, tmpAttachedEntity);

            var handle = StateChanged;

            if (handle != null)
            {
                handle(false);
            }
        }
Esempio n. 6
0
        /// <summary>
        /// Adds the child.
        /// </summary>
        /// <param name="child">The child.</param>
        /// <param name="preserveWorldPos">if set to <c>true</c> [preserve absolute position].</param>
        public void RemoveChild(Sandbox.ModAPI.IMyEntity child, bool preserveWorldPos = false)
        {
            if (preserveWorldPos)
            {
                var tmpWorldMatrix = child.WorldMatrix;

                this.Children.Remove(child.Hierarchy);

                child.WorldMatrix = tmpWorldMatrix;
            }
            else
            {
                this.Children.Remove(child.Hierarchy);
            }

            child.Hierarchy.Parent = null;

            if (child.InScene)
            {
                child.OnRemovedFromScene(this);
            }
        }
Esempio n. 7
0
        private static bool SetContainerType()
        {
            MyCharacter character = MySession.LocalCharacter;

            if (character == null)
            {
                return(false);
            }

            Matrix headMatrix             = character.GetHeadMatrix(true);
            List <MyPhysics.HitInfo> hits = new List <MyPhysics.HitInfo>();

            Sandbox.Engine.Physics.MyPhysics.CastRay(headMatrix.Translation, headMatrix.Translation + headMatrix.Forward * 100.0f, hits);

            if (hits.Count() == 0)
            {
                return(false);
            }

            var hit = hits.FirstOrDefault();

            if (hit.HkHitInfo.Body == null)
            {
                return(false);
            }
            Sandbox.ModAPI.IMyEntity entity = hit.HkHitInfo.Body.GetEntity();

            if (!(entity is MyCargoContainer))
            {
                return(false);
            }

            var dialog = new MyGuiScreenDialogContainerType(entity as MyCargoContainer);

            MyGuiSandbox.AddScreen(dialog);
            return(true);
        }
        private void Detach()
        {
            if (m_constraint == null)
                return;

            this.OnPhysicsChanged -= m_physicsChangedHandler;

            var tmpAttachedEntity = m_attachedTo;

            Debug.Assert(m_attachedTo != null, "Attached entity is null");
            if (m_attachedTo != null)
            {
                m_attachedTo.OnPhysicsChanged -= m_physicsChangedHandler;
            }
            m_attachedTo = null;

            if (!m_needsToRetryLock && !MarkedForClose)
                StartSound(m_unlockSound);

            CubeGrid.Physics.RemoveConstraint(m_constraint);

            m_constraint.Dispose();
            m_constraint = null;

            LockMode = LandingGearMode.Unlocked;
            OnConstraintRemoved(GridLinkTypeEnum.Physical, tmpAttachedEntity);
            OnConstraintRemoved(GridLinkTypeEnum.NoContactDamage, tmpAttachedEntity);

            var handle = StateChanged;
            if (handle != null) handle(false);
        }
        private void Attach(HkRigidBody body, Vector3 gearSpacePivot, Matrix otherBodySpacePivot)
        {
            if (CubeGrid.Physics.Enabled)
            {
                var entity = body.GetEntity();
                Debug.Assert(m_attachedTo == null, "Already attached");
                Debug.Assert(entity != null, "Landing gear is attached to body which has no entity");
                Debug.Assert(m_constraint == null);

                if (m_attachedTo != null || entity == null || m_constraint != null)
                    return;

                body.Activate();
                CubeGrid.Physics.RigidBody.Activate();

                m_attachedTo = entity;

                if (entity != null)
                {
                    entity.OnPhysicsChanged += m_physicsChangedHandler;
                }

                this.OnPhysicsChanged += m_physicsChangedHandler;

                Matrix gearLocalSpacePivot = Matrix.Identity;
                gearLocalSpacePivot.Translation = gearSpacePivot;
                
                var fixedData = new HkFixedConstraintData();
                if (MyFakes.OVERRIDE_LANDING_GEAR_INERTIA)
                {
                    fixedData.SetInertiaStabilizationFactor(MyFakes.LANDING_GEAR_INTERTIA);
                }
                else
                {
                    fixedData.SetInertiaStabilizationFactor(1);
                }

                fixedData.SetSolvingMethod(HkSolvingMethod.MethodStabilized);
                fixedData.SetInBodySpace(ref gearLocalSpacePivot, ref otherBodySpacePivot);

                HkConstraintData data = fixedData;

                if (MyFakes.LANDING_GEAR_BREAKABLE && BreakForce < MaxSolverImpulse)
                {
                    var breakData = new HkBreakableConstraintData(fixedData);
                    fixedData.Dispose();

                    breakData.Threshold = BreakForce;
                    breakData.ReapplyVelocityOnBreak = true;
                    breakData.RemoveFromWorldOnBrake = true;

                    data = breakData;
                }

                if (!m_needsToRetryLock)
                    StartSound(m_lockSound);

                m_constraint = new HkConstraint(CubeGrid.Physics.RigidBody, body, data);
                CubeGrid.Physics.AddConstraint(m_constraint);
                m_constraint.Enabled = true;

                LockMode = LandingGearMode.Locked;
                if (CanAutoLock)
                    ResetAutolock();

                OnConstraintAdded(GridLinkTypeEnum.Physical, entity);
                OnConstraintAdded(GridLinkTypeEnum.NoContactDamage, entity);

                var handle = StateChanged;
                if (handle != null) handle(true);
            }
        }
 public MyComponentContainer(Sandbox.ModAPI.IMyEntity entity)
 {
     Entity = entity;
 }
Esempio n. 11
0
        private void Attach(HkRigidBody body, Vector3 gearSpacePivot, Matrix otherBodySpacePivot)
        {
            if (CubeGrid.Physics.Enabled)
            {
                var entity = body.GetEntity();
                Debug.Assert(m_attachedTo == null, "Already attached");
                Debug.Assert(entity != null, "Landing gear is attached to body which has no entity");
                Debug.Assert(m_constraint == null);

                if (m_attachedTo != null || entity == null || m_constraint != null)
                {
                    return;
                }

                body.Activate();
                CubeGrid.Physics.RigidBody.Activate();

                m_attachedTo = entity;

                if (entity != null)
                {
                    entity.OnPhysicsChanged += m_physicsChangedHandler;
                }

                this.OnPhysicsChanged += m_physicsChangedHandler;

                Matrix gearLocalSpacePivot = Matrix.Identity;
                gearLocalSpacePivot.Translation = gearSpacePivot;

                var fixedData = new HkFixedConstraintData();
                if (MyFakes.OVERRIDE_LANDING_GEAR_INERTIA)
                {
                    fixedData.SetInertiaStabilizationFactor(MyFakes.LANDING_GEAR_INTERTIA);
                }
                else
                {
                    fixedData.SetInertiaStabilizationFactor(1);
                }

                fixedData.SetSolvingMethod(HkSolvingMethod.MethodStabilized);
                fixedData.SetInBodySpace(ref gearLocalSpacePivot, ref otherBodySpacePivot);

                HkConstraintData data = fixedData;

                if (MyFakes.LANDING_GEAR_BREAKABLE && BreakForce < MaxSolverImpulse)
                {
                    var breakData = new HkBreakableConstraintData(fixedData);
                    fixedData.Dispose();

                    breakData.Threshold = BreakForce;
                    breakData.ReapplyVelocityOnBreak = true;
                    breakData.RemoveFromWorldOnBrake = true;

                    data = breakData;
                }

                if (!m_needsToRetryLock)
                {
                    StartSound(m_lockSound);
                }

                m_constraint = new HkConstraint(CubeGrid.Physics.RigidBody, body, data);
                CubeGrid.Physics.AddConstraint(m_constraint);
                m_constraint.Enabled = true;

                LockMode = LandingGearMode.Locked;
                if (CanAutoLock)
                {
                    ResetAutolock();
                }

                OnConstraintAdded(GridLinkTypeEnum.Physical, entity);
                OnConstraintAdded(GridLinkTypeEnum.NoContactDamage, entity);

                var handle = StateChanged;
                if (handle != null)
                {
                    handle(true);
                }
            }
        }
        public static Sandbox.ModAPI.IMyEntity GetOtherEntity(this HkContactPointEvent eventInfo, Sandbox.ModAPI.IMyEntity sourceEntity)
        {
            var entityA = eventInfo.Base.BodyA.GetEntity();
            var entityB = eventInfo.Base.BodyB.GetEntity();

            if (sourceEntity == entityA)
            {
                return(entityB);
            }
            else
            {
                //Debug.Assert(sourceEntity == entityB);
                return(entityA);
            }
        }
 public VisitorData(DateTime time, Sandbox.ModAPI.IMyEntity entity)
 {
     this.time   = time;
     this.entity = entity;
 }