Ejemplo n.º 1
0
        private void SetMotionOnClient(MyEntity otherEntity, HkMotionType motion)
        {
            // PetrM:TODO: Make all client grids dynamic.
            if (!Sync.IsServer && !(otherEntity is MyCharacter && (otherEntity as MyCharacter).IsRagdollActivated))  // In case of picking up a ragdoll don't turn off and on the physics
            {
                otherEntity.Physics.Enabled = false;
                otherEntity.SyncFlag        = false;
                m_otherRigidBody.UpdateMotionType(motion);
                otherEntity.Physics.Enabled = true;

                // HACK: This is here only because disabling and enabling physics puts constraints into inconsistent state.
                otherEntity.RaisePhysicsChanged();
            }
        }
Ejemplo n.º 2
0
        public void StartManipulation(MyState state, MyEntity otherEntity, Vector3D hitPosition, ref MatrixD ownerWorldHeadMatrix)
        {
            Debug.Assert(m_constraintInitialized == false);

            // Commenting this out to allow picking up dead bodies and characters
            //if (otherEntity is MyCharacter)
            //    return;

            if (Owner == null || Owner.VirtualPhysics == null || !Owner.VirtualPhysics.RigidBody.InWorld)
            {
                return;
            }

            var ownerRigidBody = Owner.VirtualPhysics.RigidBody;

            if (otherEntity.Physics == null)
            {
                return;
            }

            m_otherRigidBody = otherEntity.Physics.RigidBody;

            if (otherEntity is MyCharacter)
            {
                if (!(otherEntity as MyCharacter).IsDead || !(otherEntity as MyCharacter).IsRagdollActivated)
                {
                    return;
                }

                m_otherRigidBody = (otherEntity as MyCharacter).Physics.Ragdoll.GetRootRigidBody();
            }
            // else (!otherEntity.Physics.RigidBody.InWorld) // Do we need to check if the body is in the world when this was returned byt RayCast ? Commenting this out for now..

            var characterMovementState = Owner.GetCurrentMovementState();

            if (!CanManipulate(characterMovementState))
            {
                return;
            }

            if (!CanManipulateEntity(otherEntity))
            {
                return;
            }

            // PetrM:TODO: Make all client grids dynamic.
            if (!(otherEntity is MyCharacter && (otherEntity as MyCharacter).IsRagdollActivated) && state == MyState.HOLD)  // In case of picking up a ragdoll don't turn off and on the physics
            {
                otherEntity.Physics.Enabled = false;
                otherEntity.SyncFlag        = false;
                m_otherRigidBody.UpdateMotionType(HkMotionType.Dynamic);
                otherEntity.Physics.Enabled = true;

                // HACK: This is here only because disabling and enabling physics puts constraints into inconsistent state.
                otherEntity.RaisePhysicsChanged();
            }

            Owner.VirtualPhysics.RigidBody.Activate();
            m_otherRigidBody.Activate();

            Vector3D hitUp = Vector3D.Up;
            Vector3D hitRight;
            double   dot = Vector3D.Dot(ownerWorldHeadMatrix.Forward, hitUp);

            if (dot == 1 || dot == -1)
            {
                hitRight = ownerWorldHeadMatrix.Right;
            }
            else
            {
                hitRight = Vector3D.Cross(ownerWorldHeadMatrix.Forward, hitUp);
                hitRight.Normalize();
            }

            Vector3D hitForward = Vector3D.Cross(hitUp, hitRight);

            hitForward.Normalize();

            // Matrix of constraint for other body in world
            MatrixD otherWorldMatrix = MatrixD.Identity;

            otherWorldMatrix.Forward     = hitForward;
            otherWorldMatrix.Right       = hitRight;
            otherWorldMatrix.Up          = hitUp;
            otherWorldMatrix.Translation = hitPosition;

            const float headPivotOffset = 1.5f;

            MatrixD headPivotWorldMatrix = ownerWorldHeadMatrix;

            headPivotWorldMatrix.Translation = ownerWorldHeadMatrix.Translation + headPivotOffset * ownerWorldHeadMatrix.Forward;
            //float distanceToHead = (float)(headPivotWorldMatrix.Translation - otherWorldMatrix.Translation).Length();
            //distanceToHead = MathHelper.Clamp(distanceToHead, 0.6f, 2.5f);

            // Matrix of constraint for other body in local
            m_otherLocalPivotMatrix = (Matrix)(otherWorldMatrix * otherEntity.PositionComp.WorldMatrixNormalizedInv);
            m_otherWorldPivotOrigin = otherWorldMatrix.Translation;

            MatrixD ownerWorldMatrixInverse = MatrixD.Normalize(MatrixD.Invert(ownerWorldHeadMatrix));

            m_headLocalPivotMatrix             = Matrix.Identity;
            m_headLocalPivotMatrix.Translation = Vector3D.Transform(headPivotWorldMatrix.Translation, ownerWorldMatrixInverse);

            HkConstraintData data;

            if (state == MyState.HOLD)
            {
                float mass = 0;
                if (otherEntity is MyCubeGrid)
                {
                    var group = MyCubeGridGroups.Static.Physical.GetGroup((otherEntity as MyCubeGrid));
                    foreach (var node in group.Nodes)
                    {
                        mass += node.NodeData.Physics.Mass;
                    }
                    mass = GetRealMass(mass);
                }
                else
                {
                    mass = GetRealMass(otherEntity.Physics.Mass);
                }

                // Player can hold large projectile (~222kg)
                if ((mass <= 210) || ((otherEntity is MyCharacter) && (otherEntity.Physics.Mass < 210)))
                {
                    data = CreateFixedConstraintData(ref m_otherLocalPivotMatrix, headPivotOffset);
                }
                else
                {
                    return;
                }

                if (otherEntity is MyCharacter)
                {
                    if (MyFakes.MANIPULATION_TOOL_VELOCITY_LIMIT)
                    {
                        HkMalleableConstraintData mcData = data as HkMalleableConstraintData;
                        mcData.Strength = 0.005f;
                    }
                    else
                    {
                        HkFixedConstraintData fcData = data as HkFixedConstraintData;
                        fcData.MaximumAngularImpulse = 2.0f;
                        fcData.MaximumLinearImpulse  = 2.0f;
                    }
                }
            }
            else
            {
                data = CreateBallAndSocketConstraintData(ref m_otherLocalPivotMatrix, ref m_headLocalPivotMatrix);
                if (otherEntity is MyCharacter)
                {
                    HkMalleableConstraintData mcData = data as HkMalleableConstraintData;
                    mcData.Strength = 0.005f;
                }
            }


            m_otherEntity            = otherEntity;
            m_otherEntity.OnClosing += OtherEntity_OnClosing;

            //m_otherAngularDamping = m_otherRigidBody.AngularDamping;
            //m_otherLinearDamping = m_otherRigidBody.LinearDamping;
            m_otherRestitution        = m_otherRigidBody.Restitution;
            m_otherMaxLinearVelocity  = m_otherRigidBody.MaxLinearVelocity;
            m_otherMaxAngularVelocity = m_otherRigidBody.MaxAngularVelocity;

            SetManipulated(m_otherEntity, true);
            if (state == MyState.HOLD)
            {
                m_massChange = HkMassChangerUtil.Create(m_otherRigidBody, int.MaxValue, 1, 0.001f);
                //m_otherRigidBody.AngularDamping = TARGET_ANGULAR_DAMPING;
                //m_otherRigidBody.LinearDamping = TARGET_LINEAR_DAMPING;
                m_otherRigidBody.Restitution        = TARGET_RESTITUTION;
                m_otherRigidBody.MaxLinearVelocity  = m_limitingLinearVelocity;
                m_otherRigidBody.MaxAngularVelocity = (float)Math.PI;
                if (m_otherEntity is MyCharacter)
                {
                    foreach (var body in m_otherEntity.Physics.Ragdoll.RigidBodies)
                    {
                        //body.AngularDamping = TARGET_ANGULAR_DAMPING;
                        //body.LinearDamping = TARGET_LINEAR_DAMPING;
                        body.Restitution        = TARGET_RESTITUTION;
                        body.MaxLinearVelocity  = m_limitingLinearVelocity;
                        body.MaxAngularVelocity = (float)Math.PI;
                    }
                }

                const float holdingTransparency = 0.4f;
                SetTransparent(otherEntity, holdingTransparency); //TODO jt: why it must be called twice?
                SetTransparent(otherEntity, holdingTransparency);
            }

            m_constraint = new HkConstraint(m_otherRigidBody, Owner.VirtualPhysics.RigidBody, data);

            Owner.VirtualPhysics.AddConstraint(m_constraint);

            m_constraintCreationTime = MySandboxGame.Static.UpdateTime;

            m_state = state;

            m_previousCharacterMovementState = Owner.GetCurrentMovementState();

            if (m_state == MyState.HOLD)
            {
                Owner.PlayCharacterAnimation("PickLumber", false, MyPlayAnimationMode.Immediate | MyPlayAnimationMode.Play, 0.2f, 1f);
            }
            else
            {
                Owner.PlayCharacterAnimation("PullLumber", false, MyPlayAnimationMode.Immediate | MyPlayAnimationMode.Play, 0.2f, 1f);
            }

            m_manipulatedEntitites.Add(m_otherEntity);

            Owner.ManipulatedEntity = m_otherEntity;
        }