public static GameObject createJettisonedObject(GameObject toJettison, Vector3 velocity, Vector3 force, float mass) { GameObject jettisonedObject = new GameObject("JettisonedDebris"); Transform parent = toJettison.transform.parent; if (parent != null) { jettisonedObject.transform.position = parent.position; jettisonedObject.transform.rotation = parent.rotation; } else { jettisonedObject.transform.position = toJettison.transform.position; } toJettison.transform.NestToParent(jettisonedObject.transform); physicalObject po = jettisonedObject.AddComponent <physicalObject>(); Rigidbody rb = jettisonedObject.AddComponent <Rigidbody>(); po.rb = rb; rb.velocity = velocity; rb.mass = mass; rb.AddForce(force); rb.useGravity = false; return(jettisonedObject); }
public void Detach() { if (detached) { return; } GameObject gameObject = rootTransform.gameObject; physicalObject physObj = physicalObject.ConvertToPhysicalObject(parent.part, rootTransform.gameObject); Rigidbody rigidBody = physObj.rb; rigidBody.useGravity = true; rigidBody.mass = Mass; rigidBody.centerOfMass = CalculateCoM(); rigidBody.drag = parent.part.Rigidbody.drag / parent.numSlices; rigidBody.angularDrag = parent.part.Rigidbody.angularDrag; rigidBody.angularVelocity = parent.part.Rigidbody.angularVelocity; rigidBody.maxAngularVelocity = PhysicsGlobals.MaxAngularVelocity; rigidBody.velocity = parent.part.Rigidbody.velocity + Vector3.Cross(parent.part.Rigidbody.worldCenterOfMass - parent.vessel.CurrentCoM, parent.vessel.angularVelocity); Vector3 planeNormal = parent.part.partTransform.TransformDirection(parent.axis); Vector3 centerOfMassDirection = (rigidBody.worldCenterOfMass - parent.part.Rigidbody.worldCenterOfMass).normalized; Vector3 outDirection = Vector3.ProjectOnPlane(centerOfMassDirection, planeNormal).normalized; Vector3 forceDirection = (planeNormal * 0.5f + outDirection).normalized; Vector3 torqueDirection = Vector3.Cross(planeNormal, outDirection); rigidBody.AddForce(forceDirection * parent.deploySpeed, ForceMode.VelocityChange); rigidBody.AddTorque(torqueDirection * parent.deployAngularSpeed, ForceMode.VelocityChange); detached = true; }
private void Deploy() { if (deployed) { return; } OnMoving.Fire(0f, 1f); if (UIPartActionController.Instance is UIPartActionController partActionController && partActionController.GetItem(part, false) is UIPartActionWindow window) { window.displayDirty = true; } foreach (FairingSlice slice in slices) { GameObject gameObject = slice.SliceRootObject; physicalObject physObj = physicalObject.ConvertToPhysicalObject(part, gameObject); Rigidbody rigidBody = physObj.rb; rigidBody.useGravity = true; rigidBody.mass = slice.Mass; rigidBody.centerOfMass = slice.CalculateCoM(); rigidBody.drag = part.Rigidbody.drag / numSlices; rigidBody.angularDrag = part.Rigidbody.angularDrag; rigidBody.angularVelocity = part.Rigidbody.angularVelocity; rigidBody.maxAngularVelocity = PhysicsGlobals.MaxAngularVelocity; rigidBody.velocity = part.Rigidbody.velocity + Vector3.Cross(part.Rigidbody.worldCenterOfMass - vessel.CurrentCoM, vessel.angularVelocity); Vector3 planeNormal = part.partTransform.TransformDirection(axis); Vector3 centerOfMassDirection = (rigidBody.worldCenterOfMass - part.Rigidbody.worldCenterOfMass).normalized; Vector3 outDirection = Vector3.ProjectOnPlane(centerOfMassDirection, planeNormal).normalized; Vector3 forceDirection = (planeNormal * 0.5f + outDirection).normalized; Vector3 torqueDirection = Vector3.Cross(planeNormal, outDirection); rigidBody.AddForce(forceDirection * deploySpeed, ForceMode.VelocityChange); rigidBody.AddTorque(torqueDirection * deployAngularSpeed, ForceMode.VelocityChange); } slices.Clear(); part.ModifyCoM(); needsNotifyFARToRevoxelize = true; NotifyFARToRevoxelize(); part.DragCubes.SetCubeWeight(DRAG_CUBE_NAME_CLOSED, 0); part.DragCubes.SetCubeWeight(DRAG_CUBE_NAME_DEPLOYED, 1); part.DragCubes.ForceUpdate(weights: true, occlusion: true); OnStop.Fire(1f); HideDeployEvent(); deployed = true; GameEvents.onVesselWasModified.Fire(vessel); }
public virtual void Destroy() { if (!isBreakable) { return; } if (!hasBreakingTransform || deployState == DeployState.BROKEN || CheatOptions.NoCrashDamage) { return; } if (hasAnimation && anim.isPlaying) { anim.Stop(animationName); } GameObject breakGameObject = Instantiate(breakTransform.gameObject, breakTransform.position, breakTransform.rotation, null); List <Transform> childs = breakGameObject.GetComponentsInChildren <Transform>(false).Where(t => t.name != breakName).ToList(); breakTransform.gameObject.SetActive(false); foreach (Transform child in childs) { physicalObject physicBreakObject = physicalObject.ConvertToPhysicalObject(part, child.gameObject); Vector3 randomAngularVelocity = new Vector3(UnityEngine.Random.Range(0.0f, 3.5f), UnityEngine.Random.Range(0.0f, 3.5f), UnityEngine.Random.Range(0.0f, 3.5f)); Vector3 randomVelocity = new Vector3(UnityEngine.Random.Range(0.0f, 3.0f), UnityEngine.Random.Range(0.0f, 3.0f), UnityEngine.Random.Range(0.0f, 3.0f)); physicBreakObject.rb.mass = 0.02f; physicBreakObject.rb.useGravity = false; physicBreakObject.rb.velocity = part.Rigidbody.velocity + randomVelocity; physicBreakObject.rb.angularVelocity = part.Rigidbody.angularVelocity + randomAngularVelocity; physicBreakObject.rb.maxAngularVelocity = PhysicsGlobals.MaxAngularVelocity; physicBreakObject.origDrag = 0.02f; StartCoroutine(RemovePhysicalObject(child.gameObject)); } deployState = DeployState.BROKEN; statusDisplay = Localizer.Format("#LOC_B10_MMSEV_Status_Broken"); part.ResetCollisions(); Events["RetractEvent"].active = false; Events["ExtendEvent"].active = false; Events["RepairEvent"].active = true; }
public void Jettison() { //Debug.Log("Jettison called on DecouplerShroud of "+part.name); if (segments < 2 || !HighLogic.LoadedSceneIsFlight) { return; } Events[nameof(Jettison)].guiActive = false; jettisoned = true; for (int i = 0; i < shroudGO.transform.childCount; i++) { GameObject c = shroudGO.transform.GetChild(i).gameObject; //c.layer = 19; physicalObject ph = c.AddComponent <physicalObject>(); ph.rb = c.AddComponent <Rigidbody>(); float ang = 2 * Mathf.PI * (i + .5f) / (float)segments; ph.rb.AddRelativeForce(new Vector3(Mathf.Cos(ang), 0, Mathf.Sin(ang)) * jettisonVelocity, ForceMode.VelocityChange); } shroudGO.transform.DetachChildren(); }
//[KSPEvent(guiActive = true, active = true, guiName = "Break")] //public void breakStuff() //{ // onBreak(); //} //[KSPEvent(guiActive = true, active = true)] //public void breakAdd() //{ // breakingForce += 0.05f; //} //[KSPEvent(guiActive = true, active = true)] //public void breakMinus() //{ // breakingForce -= 0.05f; //} private void breakObjects() { if (broken) { return; } if (part.packed) { return; } DMUtils.DebugLog("Breaking Objects..."); getGameObjects(); for (int i = 0; i < breakableObjects.Count; i++) { GameObject o = breakableObjects[i]; if (o == null) { continue; } Rigidbody r = o.AddComponent <Rigidbody>(); if (r == null) { continue; } DMUtils.DebugLog("Breaking Object [{0}]...", o.name); Rigidbody partRigid = part.GetComponent <Rigidbody>(); Vector3 randomAngular = new Vector3((float)DMUtils.rand.NextDouble() * 3, (float)DMUtils.rand.NextDouble() * 3, (float)DMUtils.rand.NextDouble() * 3); DMUtils.DebugLog("Random Angular: [{0:F4}]", randomAngular); DMUtils.DebugLog("Old Angular: [{0:F4}]", r.angularVelocity); r.angularVelocity = partRigid.angularVelocity + randomAngular; DMUtils.DebugLog("New Angular: [{0:F4}]", r.angularVelocity); Vector3 randomVel = new Vector3(((float)DMUtils.rand.NextDouble() * 8) - 4, ((float)DMUtils.rand.NextDouble() * 8) - 4, ((float)DMUtils.rand.NextDouble() * 8) - 4); DMUtils.DebugLog("Random Velocity: [{0:F4}]", randomVel); Vector3 localCOM = vessel.findWorldCenterOfMass() - partRigid.worldCenterOfMass; DMUtils.DebugLog("Old Velocity: [{0:F4}]", r.velocity); r.velocity = partRigid.velocity + randomVel + Vector3.Cross(localCOM, r.angularVelocity); DMUtils.DebugLog("New Velocity: [{0:F4}]", r.velocity); r.mass = componentMass; r.useGravity = false; o.transform.parent = null; physicalObject p = o.AddComponent <physicalObject>(); r.drag = componentDrag; var colliders = o.GetComponentsInChildren <Collider>(); if (colliders.Length <= 0) { continue; } for (int j = 0; j < colliders.Length; j++) { Collider c = colliders[j]; if (c == null) { continue; } if (!c.enabled) { continue; } DMUtils.DebugLog("Setting Collider [{0}] Inactive", c.name); c.enabled = false; } } StartCoroutine(breakablePartsRemove()); if (IsDeployed && oneShot && !oneWayAnimation) { isLocked = false; base.Events["retractEvent"].active = true; } }
//[KSPEvent(guiActive = true, active = true, guiName = "Break")] //public void breakStuff() //{ // onBreak(); //} //[KSPEvent(guiActive = true, active = true)] //public void breakAdd() //{ // breakingForce += 0.05f; //} //[KSPEvent(guiActive = true, active = true)] //public void breakMinus() //{ // breakingForce -= 0.05f; //} private void breakObjects() { if (broken) { return; } if (part.packed) { return; } DMUtils.DebugLog("Breaking Objects..."); getGameObjects(); for (int i = 0; i < breakableObjects.Count; i++) { GameObject o = breakableObjects[i]; if (o == null) { continue; } physicalObject pO = physicalObject.ConvertToPhysicalObject(part, o); if (pO == null || pO.rb == null) { continue; } //DMUtils.DebugLog("Breaking Object [{0}]...", o.name); pO.rb.maxAngularVelocity = PhysicsGlobals.MaxAngularVelocity; Vector3 randomAngular = new Vector3(((float)DMUtils.rand.NextDouble() * 6) - 3, ((float)DMUtils.rand.NextDouble() * 6) - 3, ((float)DMUtils.rand.NextDouble() * 6) - 3); //DMUtils.DebugLog("Random Angular: [{0:F4}]", randomAngular); //DMUtils.DebugLog("Old Angular: [{0:F4}]", pO.rb.angularVelocity); pO.rb.angularVelocity = part.Rigidbody.angularVelocity + randomAngular; //DMUtils.DebugLog("New Angular: [{0:F4}]", pO.rb.angularVelocity); Vector3 randomVel = new Vector3(((float)DMUtils.rand.NextDouble() * 8) - 4, ((float)DMUtils.rand.NextDouble() * 8) - 4, ((float)DMUtils.rand.NextDouble() * 8) - 4); //DMUtils.DebugLog("Random Velocity: [{0:F4}]", randomVel); Vector3 localCOM = vessel.CurrentCoM - part.Rigidbody.worldCenterOfMass; //DMUtils.DebugLog("Old Velocity: [{0:F4}]", pO.rb.velocity); pO.rb.velocity = part.Rigidbody.velocity + randomVel + Vector3.Cross(localCOM, pO.rb.angularVelocity); //DMUtils.DebugLog("New Velocity: [{0:F4}]", pO.rb.velocity); pO.rb.mass = componentMass; pO.rb.useGravity = false; o.transform.parent = null; pO.origDrag = componentDrag; var colliders = o.GetComponentsInChildren <Collider>(); if (colliders.Length <= 0) { continue; } for (int j = 0; j < colliders.Length; j++) { Collider c = colliders[j]; if (c == null) { continue; } if (!c.enabled) { continue; } DMUtils.DebugLog("Setting Collider [{0}] Inactive", c.name); c.enabled = false; } } StartCoroutine(breakablePartsRemove()); if (IsDeployed && oneShot && !oneWayAnimation) { isLocked = false; base.Events["retractEvent"].active = true; } }
public void Deploy() { //determine if in editor if (HighLogic.LoadedSceneIsFlight) { if (fairingTransform_1.gameObject != null && fairingTransform_2.gameObject != null) { //convert fairings to physical objects Rigidbody rigidbody = base.part.Rigidbody; if (rigidbody != null) { Vector3 heading_1 = fairingDeployPos_1 - fairingTransform_1.localPosition; Vector3 heading_2 = fairingDeployPos_2 - fairingTransform_2.localPosition; float step = 0f; while (fairingTransform_1.localPosition != heading_1) { step += TimeWarp.fixedDeltaTime; fairingTransform_1.localPosition = Vector3.MoveTowards(fairingTransform_1.localPosition, heading_1, step); fairingTransform_2.localPosition = Vector3.MoveTowards(fairingTransform_2.localPosition, heading_2, step); Debug.Log(header + "fairingTransform_1.localPosition = " + fairingTransform_1.localPosition); } physicalObject physicalObject_1 = physicalObject.ConvertToPhysicalObject(base.part, fairingTransform_1.gameObject); physicalObject physicalObject_2 = physicalObject.ConvertToPhysicalObject(base.part, fairingTransform_2.gameObject); Rigidbody rb_1 = physicalObject_1.rb; Rigidbody rb_2 = physicalObject_2.rb; rb_1.maxAngularVelocity = PhysicsGlobals.MaxAngularVelocity; rb_2.maxAngularVelocity = PhysicsGlobals.MaxAngularVelocity; rb_1.angularVelocity = rigidbody.angularVelocity; rb_2.angularVelocity = rigidbody.angularVelocity; Vector3 lhs = base.vessel.CurrentCoM - rigidbody.worldCenterOfMass; rb_1.velocity = rigidbody.velocity + Vector3.Cross(lhs, rb_1.angularVelocity); rb_2.velocity = rigidbody.velocity + Vector3.Cross(lhs, rb_2.angularVelocity); rb_1.mass = fairingMass; rb_2.mass = fairingMass; rb_1.useGravity = true; rb_2.useGravity = true; rb_1.detectCollisions = false; rb_1.AddForceAtPosition(fairingDeployPos_1 * ejectionForce * 0.5f, base.transform.position, ForceMode.Force); rb_2.AddForceAtPosition(fairingDeployPos_2 * ejectionForce * 0.5f, base.transform.position, ForceMode.Force); base.part.AddForceAtPosition(-fairingDeployPos_1 * ejectionForce * 0.5f, base.transform.position); base.part.AddForceAtPosition(-fairingDeployPos_2 * ejectionForce * 0.5f, base.transform.position); fairingTemperatureRenderer_1 = null; fairingTemperatureRenderer_2 = null; deploying = true; base.Events["Deploy"].active = false; base.Actions["DeployAction"].active = false; } } else { Debug.Log(header + " fairingTransform_1.gameObject or fairingTransform_2.gameObject is null."); } } else if (HighLogic.LoadedSceneIsEditor) { //editor deploy } }