AddPhysics on planet allows it to be deorbited #194

This commit is contained in:
Nick 2023-08-09 23:49:41 -04:00
parent 96339c8e21
commit 03455b5426
4 changed files with 68 additions and 10 deletions

View File

@ -1,15 +1,17 @@
using NewHorizons.External;
using NewHorizons.External.Configs;
using NewHorizons.Utility; using NewHorizons.Utility;
using NewHorizons.Utility.OWML;
using UnityEngine; using UnityEngine;
namespace NewHorizons.Builder.General namespace NewHorizons.Builder.General
{ {
public static class RigidBodyBuilder public static class RigidBodyBuilder
{ {
public static OWRigidbody Make(GameObject body, float sphereOfInfluence) public static OWRigidbody Make(GameObject body, float sphereOfInfluence, PlanetConfig config)
{ {
body.AddComponent<ProxyShadowCasterSuperGroup>()._bounds.radius = sphereOfInfluence; body.AddComponent<ProxyShadowCasterSuperGroup>()._bounds.radius = sphereOfInfluence;
Rigidbody rigidBody = body.AddComponent<Rigidbody>(); Rigidbody rigidBody = body.AddComponent<Rigidbody>();
rigidBody.mass = 10000;
rigidBody.drag = 0f; rigidBody.drag = 0f;
rigidBody.angularDrag = 0f; rigidBody.angularDrag = 0f;
rigidBody.useGravity = false; rigidBody.useGravity = false;
@ -17,18 +19,56 @@ namespace NewHorizons.Builder.General
rigidBody.interpolation = RigidbodyInterpolation.None; rigidBody.interpolation = RigidbodyInterpolation.None;
rigidBody.collisionDetectionMode = CollisionDetectionMode.Discrete; rigidBody.collisionDetectionMode = CollisionDetectionMode.Discrete;
KinematicRigidbody kinematicRigidBody = body.AddComponent<KinematicRigidbody>();
OWRigidbody owRigidBody = body.AddComponent<OWRigidbody>(); OWRigidbody owRigidBody = body.AddComponent<OWRigidbody>();
owRigidBody._kinematicSimulation = true;
owRigidBody._autoGenerateCenterOfMass = true; owRigidBody._autoGenerateCenterOfMass = true;
owRigidBody.SetIsTargetable(true); owRigidBody.SetIsTargetable(true);
owRigidBody._maintainOriginalCenterOfMass = true; owRigidBody._maintainOriginalCenterOfMass = true;
owRigidBody._rigidbody = rigidBody; owRigidBody._rigidbody = rigidBody;
owRigidBody._kinematicRigidbody = kinematicRigidBody;
owRigidBody._origParent = SearchUtilities.Find("SolarSystemRoot")?.transform; owRigidBody._origParent = SearchUtilities.Find("SolarSystemRoot")?.transform;
owRigidBody.EnableKinematicSimulation();
KinematicRigidbody kinematicRigidBody = body.AddComponent<KinematicRigidbody>();
owRigidBody._kinematicRigidbody = kinematicRigidBody;
owRigidBody._kinematicSimulation = true;
owRigidBody.MakeKinematic(); owRigidBody.MakeKinematic();
owRigidBody.EnableKinematicSimulation();
rigidBody.mass = 10000;
if (config.Base.addPhysics)
{
// hack: make all mesh colliders convex
// triggers are already convex
// prints errors for non readable meshes but whatever
foreach (var meshCollider in body.GetComponentsInChildren<MeshCollider>(true))
meshCollider.convex = true;
var shape = body.AddComponent<SphereShape>();
shape._collisionMode = Shape.CollisionMode.Detector;
shape._layerMask = (int)(Shape.Layer.Default | Shape.Layer.Gravity);
shape._radius = config.Base.surfaceSize;
var impactSensor = body.AddComponent<ImpactSensor>();
var audioSource = body.AddComponent<AudioSource>();
audioSource.maxDistance = 30;
audioSource.dopplerLevel = 0;
audioSource.rolloffMode = AudioRolloffMode.Custom;
audioSource.playOnAwake = false;
audioSource.spatialBlend = 1;
var owAudioSource = body.AddComponent<OWAudioSource>();
owAudioSource._audioSource = audioSource;
owAudioSource._track = OWAudioMixer.TrackName.Environment;
var objectImpactAudio = body.AddComponent<ObjectImpactAudio>();
objectImpactAudio._minPitch = 0.4f;
objectImpactAudio._maxPitch = 0.6f;
objectImpactAudio._impactSensor = impactSensor;
owRigidBody.MakeNonKinematic();
owRigidBody.DisableKinematicSimulation();
// Should make this number changeable
Delay.FireOnNextUpdate(() => owRigidBody.SetMass(0.001f));
}
return owRigidBody; return owRigidBody;
} }

View File

@ -17,7 +17,7 @@ namespace NewHorizons.Builder.Orbital
if (_dottedLineMaterial == null || _lineMaterial == null) return null; if (_dottedLineMaterial == null || _lineMaterial == null) return null;
GameObject orbitGO = new GameObject("Orbit"); var orbitGO = new GameObject("Orbit");
orbitGO.transform.parent = planetGO.transform; orbitGO.transform.parent = planetGO.transform;
orbitGO.transform.localPosition = Vector3.zero; orbitGO.transform.localPosition = Vector3.zero;
@ -83,6 +83,16 @@ namespace NewHorizons.Builder.Orbital
Delay.FireOnNextUpdate(orbitLine.InitializeLineRenderer); Delay.FireOnNextUpdate(orbitLine.InitializeLineRenderer);
// If the planet has physics and a regular orbit line, make sure that when it's bumped into the old orbit line vanishes
if (config.Base.addPhysics && !config.Orbit.trackingOrbitLine)
{
var impactSensor = planetGO.GetComponent<ImpactSensor>();
impactSensor.OnImpact += (ImpactData _) =>
{
orbitGO.SetActive(false);
};
}
return orbitLine; return orbitLine;
} }
} }

View File

@ -71,6 +71,14 @@ namespace NewHorizons.External.Modules
/// </summary> /// </summary>
[DefaultValue(0)] public int gravityVolumePriority = 0; [DefaultValue(0)] public int gravityVolumePriority = 0;
/// <summary>
/// Apply physics to this planet when you bump into it. Will have a spherical collider the size of surfaceSize.
/// For custom colliders they have to all be convex and you can leave surface size as 0.
/// This is meant for stuff like satellites which are relatively simple and can be de-orbited.
/// If you are using an orbit line but a tracking line, it will be removed when the planet is bumped in to.
/// </summary>
public bool addPhysics;
#region Obsolete #region Obsolete
[Obsolete("IsSatellite is deprecated, please use ShowMinimap instead")] [Obsolete("IsSatellite is deprecated, please use ShowMinimap instead")]

View File

@ -348,7 +348,7 @@ namespace NewHorizons.Handlers
const float sphereOfInfluence = 2000f; const float sphereOfInfluence = 2000f;
var owRigidBody = RigidBodyBuilder.Make(go, sphereOfInfluence); var owRigidBody = RigidBodyBuilder.Make(go, sphereOfInfluence, body.Config);
var ao = AstroObjectBuilder.Make(go, null, body.Config, false); var ao = AstroObjectBuilder.Make(go, null, body.Config, false);
var sector = SectorBuilder.Make(go, owRigidBody, sphereOfInfluence); var sector = SectorBuilder.Make(go, owRigidBody, sphereOfInfluence);
@ -423,7 +423,7 @@ namespace NewHorizons.Handlers
var sphereOfInfluence = GetSphereOfInfluence(body); var sphereOfInfluence = GetSphereOfInfluence(body);
var owRigidBody = RigidBodyBuilder.Make(go, sphereOfInfluence); var owRigidBody = RigidBodyBuilder.Make(go, sphereOfInfluence, body.Config);
var ao = AstroObjectBuilder.Make(go, primaryBody, body.Config, false); var ao = AstroObjectBuilder.Make(go, primaryBody, body.Config, false);
var sector = SectorBuilder.Make(go, owRigidBody, sphereOfInfluence * 2f); var sector = SectorBuilder.Make(go, owRigidBody, sphereOfInfluence * 2f);