Ejemplo n.º 1
0
        public void Weld(MyPhysicsBody other, bool recreateShape = true)
        {
            if (other.WeldInfo.Parent == this) //already welded to this
            {
                return;
            }

            if (other.IsWelded && !IsWelded)
            {
                other.Weld(this);
                return;
            }

            if (IsWelded)
            {
                WeldInfo.Parent.Weld(other);
                return;
            }
            if (other.WeldInfo.Children.Count > 0)
            {
                Debug.Fail("Welding other welded gorup");
                other.UnweldAll(false); //they should end in same group and get welded
            }
            ProfilerShort.Begin("Weld");
            HkShape thisShape;
            bool    firstWelded = WeldInfo.Children.Count == 0;

            if (firstWelded)
            {
                //RemoveConstraints(RigidBody);
                WeldedRigidBody = RigidBody;
                thisShape       = RigidBody.GetShape();
                if (HavokWorld != null)
                {
                    HavokWorld.RemoveRigidBody(WeldedRigidBody);
                }
                RigidBody = HkRigidBody.Clone(WeldedRigidBody);
                if (HavokWorld != null)
                {
                    HavokWorld.AddRigidBody(RigidBody);
                }
                HkShape.SetUserData(thisShape, RigidBody);
                Entity.OnPhysicsChanged += WeldedEntity_OnPhysicsChanged;
                WeldInfo.UpdateMassProps(RigidBody);
                //Entity.OnClose += Entity_OnClose;
            }
            else
            {
                thisShape = GetShape();
            }

            other.Deactivate();

            var transform = other.Entity.WorldMatrix * Entity.WorldMatrixInvScaled;

            other.WeldInfo.Transform = transform;
            other.WeldInfo.UpdateMassProps(other.RigidBody);
            Debug.Assert(other.WeldedRigidBody == null);
            other.WeldedRigidBody = other.RigidBody;
            other.RigidBody       = RigidBody;
            other.WeldInfo.Parent = this;
            other.ClusterObjectID = ClusterObjectID;
            WeldInfo.Children.Add(other);

            //if(recreateShape)
            //    RecreateWeldedShape(thisShape);

            ProfilerShort.BeginNextBlock("OnPhysicsChanged");
            //(other.Entity as MyEntity).RaisePhysicsChanged();
            //other.Entity.OnPhysicsChanged += WeldedEntity_OnPhysicsChanged;
            //Debug.Assert(other.m_constraints.Count == 0, "Constraints left in welded body");
            ProfilerShort.BeginNextBlock("RemoveConstraints");
            ProfilerShort.End();
            OnWelded(other);
            other.OnWelded(this);
        }
Ejemplo n.º 2
0
        public void Unweld(MyPhysicsBody other, bool insertToWorld = true, bool recreateShape = true)
        {
            Debug.Assert(other.IsWelded && RigidBody != null && other.WeldedRigidBody != null, "Invalid welding state!");

            if (IsWelded)
            {
                WeldInfo.Parent.Unweld(other, insertToWorld, recreateShape);
                Debug.Assert(other.IsWelded);
                return;
            }

            if (other.IsInWorld || RigidBody == null || other.WeldedRigidBody == null)
            {
                WeldInfo.Children.Remove(other);
                return;
            }
            var rbWorldMatrix = RigidBody.GetRigidBodyMatrix();

            //other.Entity.OnPhysicsChanged -= WeldedEntity_OnPhysicsChanged;

            other.WeldInfo.Parent = null;
            Debug.Assert(WeldInfo.Children.Contains(other));
            WeldInfo.Children.Remove(other);

            var body = other.RigidBody;

            Debug.Assert(body == RigidBody);
            other.RigidBody       = other.WeldedRigidBody;
            other.WeldedRigidBody = null;
            if (!other.RigidBody.IsDisposed)
            {
                other.RigidBody.SetWorldMatrix(other.WeldInfo.Transform * rbWorldMatrix);
                other.RigidBody.LinearVelocity      = body.LinearVelocity;
                other.WeldInfo.MassElement.Tranform = Matrix.Identity;
                other.WeldInfo.Transform            = Matrix.Identity;
            }
            else
            {
                Debug.Fail("Disposed welded body");
            }
            //RemoveConstraints(other.RigidBody);

            other.ClusterObjectID = MyHavokCluster.CLUSTERED_OBJECT_ID_UNITIALIZED;
            if (insertToWorld)
            {
                other.Activate();
                other.OnMotion(other.RigidBody, 0);
            }

            if (WeldInfo.Children.Count == 0)
            {
                recreateShape                   = false;
                Entity.OnPhysicsChanged        -= WeldedEntity_OnPhysicsChanged;
                Entity.OnClose                 -= Entity_OnClose;
                WeldedRigidBody.LinearVelocity  = RigidBody.LinearVelocity;
                WeldedRigidBody.AngularVelocity = RigidBody.AngularVelocity;
                if (HavokWorld != null)
                {
                    HavokWorld.RemoveRigidBody(RigidBody);
                }
                RigidBody.Dispose();
                RigidBody       = WeldedRigidBody;
                WeldedRigidBody = null;
                RigidBody.SetWorldMatrix(rbWorldMatrix);
                WeldInfo.Transform = Matrix.Identity;
                if (HavokWorld != null)
                {
                    HavokWorld.AddRigidBody(RigidBody);
                }
                else if (!Entity.MarkedForClose)
                {
                    Activate();
                }

                if (RigidBody2 != null)
                {
                    RigidBody2.SetShape(RigidBody.GetShape());
                }
            }
            if (RigidBody != null && recreateShape)
            {
                RecreateWeldedShape(GetShape());
            }
            OnUnwelded(other);
            other.OnUnwelded(this);
            Debug.Assert(!other.IsWelded);
        }