internal void Detach() { if (m_constraint == null) { return; } Debug.Assert(m_constraint != null); Debug.Assert(m_topGrid != null); Debug.Assert(m_topBlock != null); var tmpTopGrid = m_topGrid; m_fixedData.Dispose(); m_fixedData = null; CubeGrid.Physics.RemoveConstraint(m_constraint); m_constraint.Dispose(); m_constraint = null; m_topGrid = null; if (m_topBlock != null) { m_topBlock.Detach(); } tmpTopGrid.OnPhysicsChanged -= CubeGrid_OnPhysicsChanged; m_conveyorEndpoint.Detach(m_topBlock.ConveyorEndpoint as MyAttachableConveyorEndpoint); m_topBlock = null; OnConstraintRemoved(GridLinkTypeEnum.Physical, tmpTopGrid); OnConstraintRemoved(GridLinkTypeEnum.Logical, tmpTopGrid); UpdateText(); }
protected override bool Attach(MyAttachableTopBlockBase topBlock, bool updateGroup = true) { Debug.Assert(topBlock != null, "Top block cannot be null!"); if (CubeGrid == topBlock.CubeGrid) { return(false); } MyPistonTop pistonTop = topBlock as MyPistonTop; if (pistonTop != null && base.Attach(topBlock, updateGroup)) { Debug.Assert(m_constraint == null, "Contraint already attached, call detach first!"); Debug.Assert(m_connectionState.Value.TopBlockId.HasValue && (m_connectionState.Value.TopBlockId.Value == 0 || m_connectionState.Value.TopBlockId.Value == topBlock.EntityId), "m_topBlockId must be set prior calling Attach!"); UpdateAnimation(); var matAD = MatrixD.CreateWorld(Vector3D.Transform(Vector3D.Transform(m_constraintBasePos, Subpart3.WorldMatrix), CubeGrid.PositionComp.WorldMatrixNormalizedInv), PositionComp.LocalMatrix.Forward, PositionComp.LocalMatrix.Up); var matBD = MatrixD.CreateWorld(m_topBlock.Position * m_topBlock.CubeGrid.GridSize, m_topBlock.PositionComp.LocalMatrix.Forward, m_topBlock.PositionComp.LocalMatrix.Up); var matA = (Matrix)matAD; var matB = (Matrix)matBD; m_fixedData = new HkFixedConstraintData(); m_fixedData.SetInertiaStabilizationFactor(10); m_fixedData.SetSolvingMethod(HkSolvingMethod.MethodStabilized); m_fixedData.SetInBodySpace(matA, matB, CubeGrid.Physics, m_topGrid.Physics); //Dont dispose the fixed data or we wont have access to them m_constraint = new HkConstraint(CubeGrid.Physics.RigidBody, topBlock.CubeGrid.Physics.RigidBody, m_fixedData); m_constraint.WantRuntime = true; CubeGrid.Physics.AddConstraint(m_constraint); if (!m_constraint.InWorld) { Debug.Fail("Constraint was not added to world"); CubeGrid.Physics.RemoveConstraint(m_constraint); m_constraint.Dispose(); m_constraint = null; m_fixedData = null; return(false); } m_constraint.Enabled = true; m_topBlock = topBlock; m_topGrid = topBlock.CubeGrid; topBlock.Attach(this); m_isAttached = true; if (updateGroup) { m_conveyorEndpoint.Attach(pistonTop.ConveyorEndpoint as MyAttachableConveyorEndpoint); } UpdateText(); return(true); } return(false); }
public void Attach(MyPistonTop topBlock) { if (CubeGrid == topBlock.CubeGrid) { return; } Debug.Assert(topBlock != null, "Top block cannot be null!"); Debug.Assert(m_constraint == null, "Contraint already attached, call detach first!"); Debug.Assert(m_topBlockId.Value == topBlock.EntityId, "m_topBlockId must be set prior calling Attach!"); UpdateAnimation(); if (CubeGrid.Physics != null && CubeGrid.Physics.Enabled && topBlock.CubeGrid.Physics != null) { m_topBlock = topBlock; m_topGrid = m_topBlock.CubeGrid; var rotorBody = m_topGrid.Physics.RigidBody; var matAD = MatrixD.CreateWorld(Vector3D.Transform(Vector3D.Transform(m_constraintBasePos, Subpart3.WorldMatrix), CubeGrid.PositionComp.WorldMatrixNormalizedInv), PositionComp.LocalMatrix.Forward, PositionComp.LocalMatrix.Up); var matBD = MatrixD.CreateWorld(m_topBlock.Position * m_topBlock.CubeGrid.GridSize, m_topBlock.PositionComp.LocalMatrix.Forward, m_topBlock.PositionComp.LocalMatrix.Up); var matA = (Matrix)matAD; var matB = (Matrix)matBD; m_fixedData = new HkFixedConstraintData(); m_fixedData.SetInertiaStabilizationFactor(10); m_fixedData.SetSolvingMethod(HkSolvingMethod.MethodStabilized); m_fixedData.SetInBodySpace(matA, matB, CubeGrid.Physics, m_topGrid.Physics); var breakData = new HkBreakableConstraintData(m_fixedData); //Dont dispose the fixed data or we wont have access to them breakData.Threshold = BreakOffTreshold; breakData.ReapplyVelocityOnBreak = true; breakData.RemoveFromWorldOnBrake = false; m_constraint = new HkConstraint(CubeGrid.Physics.RigidBody, rotorBody, breakData); m_constraint.WantRuntime = true; CubeGrid.Physics.AddConstraint(m_constraint); if (!m_constraint.InWorld) { Debug.Fail("Constraint was not added to world"); CubeGrid.Physics.RemoveConstraint(m_constraint); m_constraint.Dispose(); m_constraint = null; m_fixedData.Dispose(); m_fixedData = null; return; } m_constraint.Enabled = true; m_topBlock.Attach(this); m_topGrid.OnPhysicsChanged += CubeGrid_OnPhysicsChanged; OnConstraintAdded(GridLinkTypeEnum.Physical, m_topGrid); OnConstraintAdded(GridLinkTypeEnum.Logical, m_topGrid); m_conveyorEndpoint.Attach(topBlock.ConveyorEndpoint as MyAttachableConveyorEndpoint); UpdateText(); } }
protected override void DisposeConstraint() { if (m_constraint != null) { CubeGrid.Physics.RemoveConstraint(m_constraint); m_constraint.Dispose(); m_constraint = null; } }
private void Detach() { if (CubeGrid.Physics == null || m_attachedTo == null) { return; } if (Sync.IsServer && m_attachedTo is MyCubeGrid) { (m_attachedTo as MyCubeGrid).OnGridSplit -= CubeGrid_OnGridSplit; } m_lockModeSync.Value = LandingGearMode.Unlocked; var attachedTo = m_attachedTo; if (m_attachedTo != null) { m_attachedTo.OnPhysicsChanged -= m_physicsChangedHandler; } this.OnPhysicsChanged -= m_physicsChangedHandler; m_attachedTo = null; m_attachedEntityId = null; if (m_converted) { CubeGrid.Physics.ConvertToDynamic(CubeGrid.GridSizeEnum == MyCubeSize.Large); m_converted = false; } if (MyFakes.WELD_LANDING_GEARS && MyWeldingGroups.Static.LinkExists(EntityId, CubeGrid, (MyEntity)m_attachedTo)) { MyWeldingGroups.Static.BreakLink(EntityId, CubeGrid, (MyEntity)attachedTo); } else { if (m_constraint != null) { CubeGrid.Physics.RemoveConstraint(m_constraint); m_constraint.Dispose(); m_constraint = null; } OnConstraintRemoved(GridLinkTypeEnum.NoContactDamage, attachedTo); } OnConstraintRemoved(GridLinkTypeEnum.Physical, attachedTo); if (!m_needsToRetryLock && !MarkedForClose) { StartSound(m_unlockSound); } var handle = StateChanged; if (handle != null) { handle(false); } }
protected void DisposeSubpartConstraint(ref HkConstraint constraint, ref HkFixedConstraintData constraintData) { if (constraint != null) { base.CubeGrid.Physics.RemoveConstraint(constraint); constraint.Dispose(); constraint = null; constraintData = null; } }
private void DisposeSubpartsConstraint() { if (m_subPartContraintInScene) { m_subPartContraintInScene = false; CubeGrid.Physics.RemoveConstraint(m_subpartsConstraint); } m_subpartsConstraint.Dispose(); m_subpartsConstraint = null; m_subpartsFixedData = null; }
private void DisposeSubpartsPhysics() { if (m_subpartsConstraint != null) { m_subpartsFixedData.Dispose(); m_subpartsFixedData = null; if (m_subPartContraintInScene) { m_subPartContraintInScene = false; CubeGrid.Physics.RemoveConstraint(m_subpartsConstraint); } m_subpartsConstraint.Dispose(); m_subpartsConstraint = null; } if (m_subpart1 != null && m_subpart1.Physics != null) { m_subpart1.Physics.Enabled = false; m_subpart1.Physics.Close(); m_subpart1.Physics = null; } }
protected override bool Attach(MyAttachableTopBlockBase rotor, bool updateGroup = true) { if (rotor is MyMotorRotor && base.Attach(rotor, updateGroup)) { Debug.Assert(rotor != null, "Rotor cannot be null!"); Debug.Assert(m_constraint == null, "Already attached, call detach first!"); Debug.Assert(m_connectionState.Value.TopBlockId == 0 || m_connectionState.Value.TopBlockId == rotor.EntityId, "m_rotorBlockId must be set prior calling Attach"); var rotorBody = m_topGrid.Physics.RigidBody; var data = new HkLimitedHingeConstraintData(); m_motor = new HkVelocityConstraintMotor(1.0f, 1000000f); data.SetSolvingMethod(HkSolvingMethod.MethodStabilized); data.Motor = m_motor; data.DisableLimits(); var posA = DummyPosition; var posB = rotor.Position * rotor.CubeGrid.GridSize; var axisA = PositionComp.LocalMatrix.Up; var axisAPerp = PositionComp.LocalMatrix.Forward; var axisB = rotor.PositionComp.LocalMatrix.Up; var axisBPerp = rotor.PositionComp.LocalMatrix.Forward; data.SetInBodySpace(posA, posB, axisA, axisB, axisAPerp, axisBPerp, CubeGrid.Physics, m_topGrid.Physics); m_constraint = new HkConstraint(CubeGrid.Physics.RigidBody, rotorBody, data); m_constraint.WantRuntime = true; CubeGrid.Physics.AddConstraint(m_constraint); if (!m_constraint.InWorld) { CubeGrid.Physics.RemoveConstraint(m_constraint); m_constraint.Dispose(); m_constraint = null; return(false); } m_constraint.Enabled = true; SetAngleToPhysics(); m_topBlock.Attach(this); m_isAttached = true; UpdateText(); return(true); } return(false); }
protected void RemoveConstraintInBoth() { if (this.HasConstraint) { m_other.RemoveConstraint(); CubeGrid.Physics.RemoveConstraint(m_constraint); m_constraint.Dispose(); RemoveConstraint(); HasConstraint = false; } else if (m_other != null) { m_other.RemoveConstraintInBoth(); } }
private void DisposeSubpartsConstraint() { if (m_subPartContraintInScene) { m_subPartContraintInScene = false; CubeGrid.Physics.RemoveConstraint(m_subpartsConstraint); } m_subpartsConstraint.Dispose(); m_subpartsConstraint = null; if (Sync.IsServer) { // On server, contraint contains breakable data, fixed data must be disposed manually // On client, contraint contains fixed data, which are disposed with constraint m_subpartsFixedData.Dispose(); } m_subpartsFixedData = null; }
public virtual bool Detach(bool updateGroup = true, bool reattach = true) { if (m_constraint == null) { return(false); } Debug.Assert(m_constraint != null); Debug.Assert(m_rotorGrid != null); Debug.Assert(m_rotorBlock != null); var tmpRotorGrid = m_rotorGrid; CubeGrid.Physics.RemoveConstraint(m_constraint); m_constraint.Dispose(); m_constraint = null; m_rotorGrid = null; if (m_rotorBlock != null) { m_rotorBlock.Detach(); } m_rotorBlock = null; // The following line is commented out on purpose! If you move the motor between grids (e.g. after splitting), // you have to remember the attached rotor somehow. This rotorBlockId is how it's remembered. //m_rotorBlockId = 0; if (updateGroup) { OnConstraintRemoved(GridLinkTypeEnum.Physical, tmpRotorGrid); OnConstraintRemoved(GridLinkTypeEnum.Logical, tmpRotorGrid); } if (reattach) { // Try to reattach, if the block will still live next frame. This fixes missing attachments when splitting grids NeedsUpdate |= MyEntityUpdateEnum.BEFORE_NEXT_FRAME; } if (tmpRotorGrid != null) { tmpRotorGrid.OnPhysicsChanged -= cubeGrid_OnPhysicsChanged; } return(true); }
internal void Detach(bool updateGroups = true) { if (m_constraint == null) { return; } Debug.Assert(m_constraint != null); Debug.Assert(m_topGrid != null); Debug.Assert(m_topBlock != null); var tmpTopGrid = m_topGrid; if (m_constraint.InWorld && (m_constraint.ConstraintData as HkBreakableConstraintData).getIsBroken(m_constraint)) { m_topBlockId = 0; } m_fixedData.Dispose(); m_fixedData = null; CubeGrid.Physics.RemoveConstraint(m_constraint); m_constraint.Dispose(); m_constraint = null; m_topGrid = null; if (m_topBlock != null) { m_topBlock.Detach(); } m_conveyorEndpoint.Detach(m_topBlock.ConveyorEndpoint as MyAttachableConveyorEndpoint); m_topBlock = null; if (updateGroups) { OnConstraintRemoved(GridLinkTypeEnum.Physical, tmpTopGrid); OnConstraintRemoved(GridLinkTypeEnum.Logical, tmpTopGrid); } // Try to reattach, if the block will still live next frame. This fixes missing attachments when splitting grids }
protected override bool CreateConstraint(MyAttachableTopBlockBase topBlock) { if (!base.CreateConstraint(topBlock)) { return(false); } var matAD = MatrixD.CreateWorld( Vector3D.Transform(Vector3D.Transform(m_constraintBasePos, Subpart3.WorldMatrix), CubeGrid.PositionComp.WorldMatrixNormalizedInv), PositionComp.LocalMatrix.Forward, PositionComp.LocalMatrix.Up); var matBD = MatrixD.CreateWorld(TopBlock.Position * TopBlock.CubeGrid.GridSize, TopBlock.PositionComp.LocalMatrix.Forward, TopBlock.PositionComp.LocalMatrix.Up); var matA = (Matrix)matAD; var matB = (Matrix)matBD; m_fixedData = new HkFixedConstraintData(); m_fixedData.SetInertiaStabilizationFactor(10); m_fixedData.SetSolvingMethod(HkSolvingMethod.MethodStabilized); m_fixedData.SetInBodySpace(matA, matB, CubeGrid.Physics, TopGrid.Physics); //Dont dispose the fixed data or we wont have access to them m_constraint = new HkConstraint(CubeGrid.Physics.RigidBody, topBlock.CubeGrid.Physics.RigidBody, m_fixedData); m_constraint.WantRuntime = true; CubeGrid.Physics.AddConstraint(m_constraint); if (!m_constraint.InWorld) { Debug.Fail("Constraint was not added to world"); CubeGrid.Physics.RemoveConstraint(m_constraint); m_constraint.Dispose(); m_constraint = null; m_fixedData = null; return(false); } m_constraint.Enabled = true; return(true); }
protected override bool CreateConstraint(MyAttachableTopBlockBase rotor) { if (!base.CreateConstraint(rotor)) { return(false); } Debug.Assert(rotor != null, "Rotor cannot be null!"); Debug.Assert(m_constraint == null, "Already attached, call detach first!"); Debug.Assert( m_connectionState.Value.TopBlockId == 0 || m_connectionState.Value.TopBlockId == rotor.EntityId, "m_rotorBlockId must be set prior calling Attach"); var rotorBody = TopGrid.Physics.RigidBody; var data = new HkLimitedHingeConstraintData(); m_motor = new HkVelocityConstraintMotor(1.0f, 1000000f); data.SetSolvingMethod(HkSolvingMethod.MethodStabilized); data.Motor = m_motor; data.DisableLimits(); SetConstraintPosition(rotor, data); m_constraint = new HkConstraint(CubeGrid.Physics.RigidBody, rotorBody, data); m_constraint.WantRuntime = true; CubeGrid.Physics.AddConstraint(m_constraint); if (!m_constraint.InWorld) { CubeGrid.Physics.RemoveConstraint(m_constraint); m_constraint.Dispose(); m_constraint = null; return(false); } m_constraint.Enabled = true; SetAngleToPhysics(); return(true); }
public virtual bool Detach(bool updateGroup = true) { if (m_isWelding == false) { UnweldGroup(); } var tmpRotorGrid = m_rotorGrid; if (updateGroup) { OnConstraintRemoved(GridLinkTypeEnum.Physical, tmpRotorGrid); OnConstraintRemoved(GridLinkTypeEnum.Logical, tmpRotorGrid); } if (m_constraint == null) { return(false); } Debug.Assert(m_constraint != null); Debug.Assert(m_rotorGrid != null); Debug.Assert(m_rotorBlock != null); CubeGrid.Physics.RemoveConstraint(m_constraint); m_constraint.Dispose(); m_constraint = null; m_rotorGrid = null; if (m_rotorBlock != null) { m_rotorBlock.Detach(m_welded || m_isWelding); } m_rotorBlock = null; m_isAttached = false; UpdateText(); return(true); }
private void RemoveConstraint(MyShipConnector otherConnector, HkConstraint constraint) { if (this.HasConstraint) { CubeGrid.Physics.RemoveConstraint(constraint); HasConstraint = false; } else { otherConnector.CubeGrid.Physics.RemoveConstraint(constraint); otherConnector.HasConstraint = false; } constraint.Dispose(); }
private void RemoveConstraint(MyShipConnector otherConnector, HkConstraint constraint) { if (this.m_hasConstraint) { if (CubeGrid.Physics != null) CubeGrid.Physics.RemoveConstraint(constraint); m_hasConstraint = false; } else { if (otherConnector.CubeGrid.Physics != null) otherConnector.CubeGrid.Physics.RemoveConstraint(constraint); otherConnector.m_hasConstraint = false; } constraint.Dispose(); }
public override bool Attach(MyMotorRotor rotor, bool updateGroup = true) { Debug.Assert(rotor != null, "Rotor cannot be null!"); Debug.Assert(m_constraint == null, "Already attached, call detach first!"); Debug.Assert(m_rotorBlockId.Value.OtherEntityId == 0 || m_rotorBlockId.Value.OtherEntityId == rotor.EntityId, "m_rotorBlockId must be set prior calling Attach"); if (rotor == null || rotor.CubeGrid == null || MarkedForClose || Closed || CubeGrid.MarkedForClose || CubeGrid.Closed || rotor.MarkedForClose || rotor.Closed || rotor.CubeGrid.MarkedForClose || rotor.CubeGrid.Closed) { return(false); } if (CubeGrid.Physics != null && CubeGrid.Physics.Enabled) { m_rotorBlock = rotor; m_rotorGrid = m_rotorBlock.CubeGrid; if (m_rotorGrid.Physics == null) { return(false); } if (CubeGrid.Physics.RigidBody == m_rotorGrid.Physics.RigidBody) { if (updateGroup && m_welded) { OnConstraintAdded(GridLinkTypeEnum.Physical, m_rotorGrid); OnConstraintAdded(GridLinkTypeEnum.Logical, m_rotorGrid); } m_isAttached = true; return(true); } var rotorBody = m_rotorGrid.Physics.RigidBody; var data = new HkLimitedHingeConstraintData(); m_motor = new HkVelocityConstraintMotor(1.0f, 1000000f); data.SetSolvingMethod(HkSolvingMethod.MethodStabilized); data.Motor = m_motor; data.DisableLimits(); var posA = DummyPosition; var posB = rotor.Position * rotor.CubeGrid.GridSize; var axisA = PositionComp.LocalMatrix.Up; var axisAPerp = PositionComp.LocalMatrix.Forward; var axisB = rotor.PositionComp.LocalMatrix.Up; var axisBPerp = rotor.PositionComp.LocalMatrix.Forward; data.SetInBodySpace(posA, posB, axisA, axisB, axisAPerp, axisBPerp, CubeGrid.Physics, m_rotorGrid.Physics); m_constraint = new HkConstraint(CubeGrid.Physics.RigidBody, rotorBody, data); m_constraint.WantRuntime = true; CubeGrid.Physics.AddConstraint(m_constraint); if (!m_constraint.InWorld) { CubeGrid.Physics.RemoveConstraint(m_constraint); m_constraint.Dispose(); m_constraint = null; return(false); } m_constraint.Enabled = true; SetAngleToPhysics(); m_rotorBlock.Attach(this); if (updateGroup) { OnConstraintAdded(GridLinkTypeEnum.Physical, m_rotorGrid); OnConstraintAdded(GridLinkTypeEnum.Logical, m_rotorGrid); m_rotorGrid.OnPhysicsChanged += cubeGrid_OnPhysicsChanged; } m_isAttached = true; UpdateText(); return(true); } return(false); }
public void Attach(MyPistonTop topBlock) { if (CubeGrid == topBlock.CubeGrid) return; Debug.Assert(topBlock != null, "Top block cannot be null!"); Debug.Assert(m_constraint == null, "Contraint already attached, call detach first!"); Debug.Assert(m_topBlockId.Value == topBlock.EntityId, "m_topBlockId must be set prior calling Attach!"); UpdateAnimation(); if (CubeGrid.Physics != null && CubeGrid.Physics.Enabled && topBlock.CubeGrid.Physics != null) { m_topBlock = topBlock; m_topGrid = m_topBlock.CubeGrid; var rotorBody = m_topGrid.Physics.RigidBody; var matAD = MatrixD.CreateWorld(Vector3D.Transform(Vector3D.Transform(m_constraintBasePos, Subpart3.WorldMatrix), CubeGrid.PositionComp.WorldMatrixNormalizedInv), PositionComp.LocalMatrix.Forward, PositionComp.LocalMatrix.Up); var matBD = MatrixD.CreateWorld(m_topBlock.Position * m_topBlock.CubeGrid.GridSize, m_topBlock.PositionComp.LocalMatrix.Forward, m_topBlock.PositionComp.LocalMatrix.Up); var matA = (Matrix)matAD; var matB = (Matrix)matBD; m_fixedData = new HkFixedConstraintData(); m_fixedData.SetInertiaStabilizationFactor(10); m_fixedData.SetSolvingMethod(HkSolvingMethod.MethodStabilized); m_fixedData.SetInBodySpace(matA, matB, CubeGrid.Physics, m_topGrid.Physics); var breakData = new HkBreakableConstraintData(m_fixedData); //Dont dispose the fixed data or we wont have access to them breakData.Threshold = BreakOffTreshold; breakData.ReapplyVelocityOnBreak = true; breakData.RemoveFromWorldOnBrake = false; m_constraint = new HkConstraint(CubeGrid.Physics.RigidBody, rotorBody, breakData); m_constraint.WantRuntime = true; CubeGrid.Physics.AddConstraint(m_constraint); if(!m_constraint.InWorld) { Debug.Fail("Constraint was not added to world"); CubeGrid.Physics.RemoveConstraint(m_constraint); m_constraint.Dispose(); m_constraint = null; m_fixedData.Dispose(); m_fixedData = null; return; } m_constraint.Enabled = true; m_topBlock.Attach(this); m_topGrid.OnPhysicsChanged += CubeGrid_OnPhysicsChanged; OnConstraintAdded(GridLinkTypeEnum.Physical, m_topGrid); OnConstraintAdded(GridLinkTypeEnum.Logical, m_topGrid); m_conveyorEndpoint.Attach(topBlock.ConveyorEndpoint as MyAttachableConveyorEndpoint); UpdateText(); } }
public void StopManipulation() { if (m_state != MyState.NONE && Owner != null) { var characterMovementState = Owner.GetCurrentMovementState(); switch (characterMovementState) { case MyCharacterMovementEnum.Walking: case MyCharacterMovementEnum.BackWalking: case MyCharacterMovementEnum.WalkingLeftFront: case MyCharacterMovementEnum.WalkingRightFront: case MyCharacterMovementEnum.WalkingLeftBack: case MyCharacterMovementEnum.WalkingRightBack: case MyCharacterMovementEnum.WalkStrafingLeft: case MyCharacterMovementEnum.WalkStrafingRight: case MyCharacterMovementEnum.Running: case MyCharacterMovementEnum.Backrunning: case MyCharacterMovementEnum.RunStrafingLeft: case MyCharacterMovementEnum.RunStrafingRight: case MyCharacterMovementEnum.RunningRightFront: case MyCharacterMovementEnum.RunningRightBack: case MyCharacterMovementEnum.RunningLeftFront: case MyCharacterMovementEnum.RunningLeftBack: Owner.PlayCharacterAnimation("WalkBack", true, MyPlayAnimationMode.Immediate | MyPlayAnimationMode.Play, 0.2f, 1f); break; case MyCharacterMovementEnum.Standing: case MyCharacterMovementEnum.RotatingLeft: case MyCharacterMovementEnum.RotatingRight: case MyCharacterMovementEnum.Flying: Owner.PlayCharacterAnimation("Idle", true, MyPlayAnimationMode.Immediate | MyPlayAnimationMode.Play, 0.2f, 1f); break; } } if (m_constraint != null) { if (Owner != null && Owner.VirtualPhysics != null) { Owner.VirtualPhysics.RemoveConstraint(m_constraint); } m_constraint.Dispose(); m_constraint = null; } if (m_fixedConstraintData != null) { if (!m_fixedConstraintData.IsDisposed) { m_fixedConstraintData.Dispose(); } m_fixedConstraintData = null; } m_headLocalPivotMatrix = Matrix.Zero; m_otherLocalPivotMatrix = Matrix.Zero; if (m_otherEntity != null) { SetTransparent(m_otherEntity); SetTransparent(m_otherEntity); if (m_state == MyState.HOLD) { SetMotionOnClient(m_otherEntity, HkMotionType.Keyframed); } m_manipulatedEntitites.Remove(m_otherEntity); m_otherEntity.SyncFlag = true; if (m_otherEntity.Physics != null && m_otherRigidBody != null && !m_otherRigidBody.IsDisposed) { SetManipulated(m_otherEntity, false); //m_otherRigidBody.AngularDamping = m_otherAngularDamping; //m_otherRigidBody.LinearDamping = m_otherLinearDamping; m_otherRigidBody.Restitution = m_otherRestitution; m_otherRigidBody.MaxLinearVelocity = m_otherMaxLinearVelocity; m_otherRigidBody.MaxAngularVelocity = m_otherMaxAngularVelocity; if (m_massChange != null) { m_massChange.Remove(); } m_massChange = null; // Clamp output velocity m_otherRigidBody.LinearVelocity = Vector3.Clamp(m_otherRigidBody.LinearVelocity, -2 * Vector3.One, 2 * Vector3.One); m_otherRigidBody.AngularVelocity = Vector3.Clamp(m_otherRigidBody.AngularVelocity, -Vector3.One * (float)Math.PI, Vector3.One * (float)Math.PI); if (!m_otherRigidBody.IsActive) { m_otherRigidBody.Activate(); } m_otherRigidBody.EnableDeactivation = false; m_otherRigidBody.EnableDeactivation = true; //resets deactivation counter m_otherRigidBody = null; } m_otherEntity.OnClosing -= OtherEntity_OnClosing; m_otherEntity = null; } m_constraintInitialized = false; if (Owner != null) { Owner.ManipulatedEntity = null; } m_state = MyState.NONE; }
protected override bool Attach(MyAttachableTopBlockBase topBlock, bool updateGroup = true) { Debug.Assert(topBlock != null, "Top block cannot be null!"); if (CubeGrid == topBlock.CubeGrid) return false; MyPistonTop pistonTop = topBlock as MyPistonTop; if (pistonTop != null && base.Attach(topBlock, updateGroup)) { Debug.Assert(m_constraint == null, "Contraint already attached, call detach first!"); Debug.Assert(m_connectionState.Value.TopBlockId.HasValue && (m_connectionState.Value.TopBlockId.Value == 0 || m_connectionState.Value.TopBlockId.Value == topBlock.EntityId), "m_topBlockId must be set prior calling Attach!"); UpdateAnimation(); var matAD = MatrixD.CreateWorld(Vector3D.Transform(Vector3D.Transform(m_constraintBasePos, Subpart3.WorldMatrix), CubeGrid.PositionComp.WorldMatrixNormalizedInv), PositionComp.LocalMatrix.Forward, PositionComp.LocalMatrix.Up); var matBD = MatrixD.CreateWorld(m_topBlock.Position * m_topBlock.CubeGrid.GridSize, m_topBlock.PositionComp.LocalMatrix.Forward, m_topBlock.PositionComp.LocalMatrix.Up); var matA = (Matrix)matAD; var matB = (Matrix)matBD; m_fixedData = new HkFixedConstraintData(); m_fixedData.SetInertiaStabilizationFactor(10); m_fixedData.SetSolvingMethod(HkSolvingMethod.MethodStabilized); m_fixedData.SetInBodySpace(matA, matB, CubeGrid.Physics, m_topGrid.Physics); //Dont dispose the fixed data or we wont have access to them m_constraint = new HkConstraint(CubeGrid.Physics.RigidBody, topBlock.CubeGrid.Physics.RigidBody, m_fixedData); m_constraint.WantRuntime = true; CubeGrid.Physics.AddConstraint(m_constraint); if (!m_constraint.InWorld) { Debug.Fail("Constraint was not added to world"); CubeGrid.Physics.RemoveConstraint(m_constraint); m_constraint.Dispose(); m_constraint = null; m_fixedData = null; return false; } m_constraint.Enabled = true; m_topBlock = topBlock; m_topGrid = topBlock.CubeGrid; topBlock.Attach(this); m_isAttached = true; if (updateGroup) { m_conveyorEndpoint.Attach(pistonTop.ConveyorEndpoint as MyAttachableConveyorEndpoint); } UpdateText(); return true; } return false; }
protected override bool CreateConstraint(MyAttachableTopBlockBase rotor) { if (!base.CreateConstraint(rotor)) return false; Debug.Assert(rotor != null, "Rotor cannot be null!"); Debug.Assert(m_constraint == null, "Already attached, call detach first!"); Debug.Assert( m_connectionState.Value.TopBlockId == 0 || m_connectionState.Value.TopBlockId == rotor.EntityId, "m_rotorBlockId must be set prior calling Attach"); var rotorBody = TopGrid.Physics.RigidBody; var data = new HkLimitedHingeConstraintData(); m_motor = new HkVelocityConstraintMotor(1.0f, 1000000f); data.SetSolvingMethod(HkSolvingMethod.MethodStabilized); data.Motor = m_motor; data.DisableLimits(); SetConstraintPosition(rotor, data); m_constraint = new HkConstraint(CubeGrid.Physics.RigidBody, rotorBody, data); m_constraint.WantRuntime = true; CubeGrid.Physics.AddConstraint(m_constraint); if (!m_constraint.InWorld) { CubeGrid.Physics.RemoveConstraint(m_constraint); m_constraint.Dispose(); m_constraint = null; return false; } m_constraint.Enabled = true; SetAngleToPhysics(); return true; }
protected override bool Attach(MyAttachableTopBlockBase rotor, bool updateGroup = true) { if (rotor is MyMotorRotor && base.Attach(rotor, updateGroup)) { Debug.Assert(rotor != null, "Rotor cannot be null!"); Debug.Assert(m_constraint == null, "Already attached, call detach first!"); Debug.Assert(m_connectionState.Value.TopBlockId == 0 || m_connectionState.Value.TopBlockId == rotor.EntityId, "m_rotorBlockId must be set prior calling Attach"); var rotorBody = m_topGrid.Physics.RigidBody; var data = new HkLimitedHingeConstraintData(); m_motor = new HkVelocityConstraintMotor(1.0f, 1000000f); data.SetSolvingMethod(HkSolvingMethod.MethodStabilized); data.Motor = m_motor; data.DisableLimits(); var posA = DummyPosition; var posB = rotor.Position * rotor.CubeGrid.GridSize; var axisA = PositionComp.LocalMatrix.Up; var axisAPerp = PositionComp.LocalMatrix.Forward; var axisB = rotor.PositionComp.LocalMatrix.Up; var axisBPerp = rotor.PositionComp.LocalMatrix.Forward; data.SetInBodySpace(posA, posB, axisA, axisB, axisAPerp, axisBPerp, CubeGrid.Physics, m_topGrid.Physics); m_constraint = new HkConstraint(CubeGrid.Physics.RigidBody, rotorBody, data); m_constraint.WantRuntime = true; CubeGrid.Physics.AddConstraint(m_constraint); if (!m_constraint.InWorld) { CubeGrid.Physics.RemoveConstraint(m_constraint); m_constraint.Dispose(); m_constraint = null; return false; } m_constraint.Enabled = true; SetAngleToPhysics(); m_topBlock.Attach(this); m_isAttached = true; UpdateText(); return true; } return false; }
public void StopManipulation() { if (m_state != MyState.NONE && Owner != null) { var characterMovementState = Owner.GetCurrentMovementState(); switch (characterMovementState) { case MyCharacterMovementEnum.Walking: case MyCharacterMovementEnum.BackWalking: case MyCharacterMovementEnum.WalkingLeftFront: case MyCharacterMovementEnum.WalkingRightFront: case MyCharacterMovementEnum.WalkingLeftBack: case MyCharacterMovementEnum.WalkingRightBack: case MyCharacterMovementEnum.WalkStrafingLeft: case MyCharacterMovementEnum.WalkStrafingRight: case MyCharacterMovementEnum.Running: case MyCharacterMovementEnum.Backrunning: case MyCharacterMovementEnum.RunStrafingLeft: case MyCharacterMovementEnum.RunStrafingRight: case MyCharacterMovementEnum.RunningRightFront: case MyCharacterMovementEnum.RunningRightBack: case MyCharacterMovementEnum.RunningLeftFront: case MyCharacterMovementEnum.RunningLeftBack: Owner.PlayCharacterAnimation("WalkBack", MyBlendOption.Immediate, MyFrameOption.Loop, 0.2f, 1f); break; case MyCharacterMovementEnum.Standing: case MyCharacterMovementEnum.RotatingLeft: case MyCharacterMovementEnum.RotatingRight: case MyCharacterMovementEnum.Flying: Owner.PlayCharacterAnimation("Idle", MyBlendOption.Immediate, MyFrameOption.Loop, 0.2f, 1f); break; } } if (m_constraint != null) { if (OwnerVirtualPhysics != null) { OwnerVirtualPhysics.RemoveConstraint(m_constraint); } m_constraint.Dispose(); m_constraint = null; } if (m_fixedConstraintData != null) { if (!m_fixedConstraintData.IsDisposed) { m_fixedConstraintData.Dispose(); } m_fixedConstraintData = null; } m_headLocalPivotMatrix = Matrix.Zero; m_otherLocalPivotMatrix = Matrix.Zero; if (m_otherEntity != null) { SetTransparent(m_otherEntity); if (m_state == MyState.HOLD) { SetMotionOnClient(m_otherEntity, HkMotionType.Dynamic); // Do not send when disconnecting if (IsOwnerLocalPlayer() && !MyEntities.CloseAllowed && !(m_otherEntity is MyCharacter)) { Sync.Players.RemoveControlledEntity(m_otherEntity); } } m_manipulatedEntitites.Remove(m_otherEntity); var handler = ManipulationStopped; if (handler != null) { handler(m_otherEntity); } m_otherEntity.SyncFlag = true; if (m_otherEntity.Physics != null && m_otherRigidBody != null && !m_otherRigidBody.IsDisposed) { SetManipulated(m_otherEntity, false); //m_otherRigidBody.AngularDamping = m_otherAngularDamping; //m_otherRigidBody.LinearDamping = m_otherLinearDamping; if (m_otherEntity is MyCharacter) { m_otherEntity.Physics.SetRagdollDefaults(); } else { m_otherRigidBody.Restitution = m_otherRestitution; m_otherRigidBody.MaxLinearVelocity = m_otherMaxLinearVelocity; m_otherRigidBody.MaxAngularVelocity = m_otherMaxAngularVelocity; if (m_massChange != null) { m_massChange.Remove(); } m_massChange = null; // Clamp output velocity m_otherRigidBody.LinearVelocity = Vector3.Clamp(m_otherRigidBody.LinearVelocity, -2 * Vector3.One, 2 * Vector3.One); m_otherRigidBody.AngularVelocity = Vector3.Clamp(m_otherRigidBody.AngularVelocity, -Vector3.One * (float)Math.PI, Vector3.One * (float)Math.PI); if (!m_otherRigidBody.IsActive) { m_otherRigidBody.Activate(); } m_otherRigidBody.EnableDeactivation = false; m_otherRigidBody.EnableDeactivation = true; //resets deactivation counter } m_otherRigidBody = null; } m_otherEntity.OnClosing -= OtherEntity_OnClosing; m_otherEntity = null; } m_constraintInitialized = false; RemoveOwnerVirtualPhysics(); if (Owner != null) { Owner.ManipulatedEntity = null; } m_state = MyState.NONE; }
protected override bool CreateConstraint(MyAttachableTopBlockBase topBlock) { if (!base.CreateConstraint(topBlock)) return false; var matAD = MatrixD.CreateWorld( Vector3D.Transform(Vector3D.Transform(m_constraintBasePos, Subpart3.WorldMatrix), CubeGrid.PositionComp.WorldMatrixNormalizedInv), PositionComp.LocalMatrix.Forward, PositionComp.LocalMatrix.Up); var matBD = MatrixD.CreateWorld(TopBlock.Position*TopBlock.CubeGrid.GridSize, TopBlock.PositionComp.LocalMatrix.Forward, TopBlock.PositionComp.LocalMatrix.Up); var matA = (Matrix) matAD; var matB = (Matrix) matBD; m_fixedData = new HkFixedConstraintData(); m_fixedData.SetInertiaStabilizationFactor(10); m_fixedData.SetSolvingMethod(HkSolvingMethod.MethodStabilized); m_fixedData.SetInBodySpace(matA, matB, CubeGrid.Physics, TopGrid.Physics); //Dont dispose the fixed data or we wont have access to them m_constraint = new HkConstraint(CubeGrid.Physics.RigidBody, topBlock.CubeGrid.Physics.RigidBody, m_fixedData); m_constraint.WantRuntime = true; CubeGrid.Physics.AddConstraint(m_constraint); if (!m_constraint.InWorld) { Debug.Fail("Constraint was not added to world"); CubeGrid.Physics.RemoveConstraint(m_constraint); m_constraint.Dispose(); m_constraint = null; m_fixedData = null; return false; } m_constraint.Enabled = true; return true; }
public override bool Attach(MyMotorRotor rotor, bool updateGroup = true) { Debug.Assert(rotor != null, "Rotor cannot be null!"); Debug.Assert(m_constraint == null, "Already attached, call detach first!"); Debug.Assert(m_rotorBlockId.Value.OtherEntityId == 0 || m_rotorBlockId.Value.OtherEntityId == rotor.EntityId, "m_rotorBlockId must be set prior calling Attach"); if (rotor == null || MarkedForClose || Closed || rotor.MarkedForClose || rotor.Closed || CubeGrid.MarkedForClose || CubeGrid.Closed) { return false; } if (CubeGrid.Physics != null && CubeGrid.Physics.Enabled) { m_rotorBlock = rotor; m_rotorGrid = m_rotorBlock.CubeGrid; if (m_rotorGrid.Physics == null) return false; if (CubeGrid.Physics.RigidBody == m_rotorGrid.Physics.RigidBody) { if (updateGroup && m_welded) { OnConstraintAdded(GridLinkTypeEnum.Physical, m_rotorGrid); OnConstraintAdded(GridLinkTypeEnum.Logical, m_rotorGrid); } m_isAttached = true; return true; } var rotorBody = m_rotorGrid.Physics.RigidBody; var data = new HkLimitedHingeConstraintData(); m_motor = new HkVelocityConstraintMotor(1.0f, 1000000f); data.SetSolvingMethod(HkSolvingMethod.MethodStabilized); data.Motor = m_motor; data.DisableLimits(); var posA = DummyPosition; var posB = rotor.Position * rotor.CubeGrid.GridSize; var axisA = PositionComp.LocalMatrix.Up; var axisAPerp = PositionComp.LocalMatrix.Forward; var axisB = rotor.PositionComp.LocalMatrix.Up; var axisBPerp = rotor.PositionComp.LocalMatrix.Forward; data.SetInBodySpace(posA, posB, axisA, axisB, axisAPerp, axisBPerp, CubeGrid.Physics, m_rotorGrid.Physics); m_constraint = new HkConstraint(CubeGrid.Physics.RigidBody, rotorBody, data); m_constraint.WantRuntime = true; CubeGrid.Physics.AddConstraint(m_constraint); if(!m_constraint.InWorld) { CubeGrid.Physics.RemoveConstraint(m_constraint); m_constraint.Dispose(); m_constraint = null; return false; } m_constraint.Enabled = true; SetAngleToPhysics(); m_rotorBlock.Attach(this); if (updateGroup) { OnConstraintAdded(GridLinkTypeEnum.Physical, m_rotorGrid); OnConstraintAdded(GridLinkTypeEnum.Logical, m_rotorGrid); m_rotorGrid.OnPhysicsChanged += cubeGrid_OnPhysicsChanged; } m_isAttached = true; UpdateText(); return true; } return false; }