public void RestartWheels(Part p) //Add the component back to wheels once the component has been deleted { foreach (Component c in p.gameObject.GetComponentsInChildren(typeof(Component))) { if (c.name == "wheel") { origin = PhysicsStarter.GetPos(c.transform); wheelPivot = c; } if (c.name == "WheelCollider") { origin = PhysicsStarter.GetPos(c.transform); wheelPivot = c; } else if (c.name == "foot") { origin = PhysicsStarter.GetPos(c.transform); wheelPivot = c; } else if (c.name == "leg_collider") { origin = PhysicsStarter.GetPos(c.transform); wheelPivot = c; } else if (c.GetType().Name == "KSPWheelBase") { origin = PhysicsStarter.GetPos(c.transform); wheelPivot = c; } else if (c is ModuleGroundSciencePart || c is ModuleGroundSciencePart || c is ModuleGroundPart || c is ModuleGroundExperiment || c is ModuleGroundCommsPart || c is ModulePhysicMaterial) { origin = PhysicsStarter.GetPos(c.transform); wheelPivot = c; } else { } } Debug.Log("[Parallax Collisions] Restarted wheels"); }
void Update() { if (FlightGlobals.currentMainBody == FlightGlobals.GetBodyByName("Minmus") && ParallaxSettings.flatMinmus == true) { DisablePlane(); return; } CheckIfEnabled(); if (wheelDeploy != null) { if ((wheelDeploy as ModuleWheelDeployment).deployedPosition == 1) { if ((wheelDeploy as ModuleWheelDeployment).Position < 1) { wheelsHaveBeenRetracted = true; DisablePlane(); return; } } else if ((wheelDeploy as ModuleWheelDeployment).deployedPosition == 0) { if ((wheelDeploy as ModuleWheelDeployment).Position > 0) { wheelsHaveBeenRetracted = true; DisablePlane(); return; } } if (wheelsHaveBeenRetracted == true) { RestartWheels(p); Debug.Log("Restarting after retract"); dampingFrames = 0; wheelsHaveBeenRetracted = false; } } if (wheelsHaveBeenRetracted == true) { DisablePlane(); return; } if (TimeWarp.CurrentRate > 1 && TimeWarp.WarpMode == TimeWarp.Modes.HIGH) { wheelsHaveBeenInWarp = true; DisablePlane(); return; } if ((wheelsHaveBeenInWarp == true && TimeWarp.CurrentRate == 1)) { RestartWheels(p); dampingFrames = dampingFrames - 10; wheelsHaveBeenInWarp = false; DisablePlane(); return; } if (ParallaxOnDemandLoader.finishedMainLoad == false) { DisablePlane(); return; } if (p.vessel.packed == true) //Vessel is time warping so we don't need to do anything here { DisablePlane(); packed = true; started = true; return; } if (packed == true) //Vessel has finished time warping and needs re-initializing { DisablePlane(); packed = false; Debug.Log("[Parallax Physics] Restarting after unpack..."); Destroy(plane); Start(); } //plane.layer = 0; //Layer has to be set to 0 to avoid raycast on layer 15 if (p == null) { DisablePlane(); return; } if (wheelPivot == null) { DisablePlane(); return; } if ((thisBody != FlightGlobals.currentMainBody.name) || started == false) //Body change, reassign textures { if (ParallaxShaderLoader.parallaxBodies.ContainsKey(FlightGlobals.currentMainBody.name) == false) { return; } Debug.Log("[Parallax Physics] Body changed! Getting new values..."); Material a = ParallaxShaderLoader.parallaxBodies[FlightGlobals.currentMainBody.name].ParallaxBodyMaterial.ParallaxMaterial; tex = PhysicsTexHolder.displacementTex; normalLow = PhysicsTexHolder.physicsTexLow; normalMid = PhysicsTexHolder.physicsTexMid; normalHigh = PhysicsTexHolder.physicsTexHigh; normalSteep = PhysicsTexHolder.physicsTexSteep; _ST = (a.GetTextureScale("_SurfaceTexture")); blendLowStart = a.GetFloat("_LowStart"); blendLowEnd = a.GetFloat("_LowEnd"); blendHighStart = a.GetFloat("_HighStart"); blendHighEnd = a.GetFloat("_HighEnd"); steepPower = a.GetFloat("_SteepPower"); planetOrigin = a.GetVector("_PlanetOrigin"); planetRadius = a.GetFloat("_PlanetRadius"); _Displacement_Scale = a.GetFloat("_displacement_scale"); _Displacement_Offset = a.GetFloat("_displacement_offset"); Debug.Log("[Parallax Physics] Success!"); thisBody = FlightGlobals.currentMainBody.name; RestartWheels(p); } origin = PhysicsStarter.GetPos(wheelPivot.transform); if (p.vessel.srf_velocity.magnitude < 0.025f && dampingFrames >= 150) //Vessel is "landed" { plane.layer = 15; started = true; return; } else { plane.layer = 15; } approximateRay.origin = origin; approximateRay.direction = -PhysicsStarter.terrainNormal; if (UnityEngine.Physics.Raycast(approximateRay, out hitApproximateRay, 5f, layerMask)) { samplePoint = hitApproximateRay.point; sampleNormal = hitApproximateRay.normal; } else { started = true; return; } float displacement = GetDisplacement(new Vector2(wheelPivot.transform.position.x, wheelPivot.transform.position.y), tex, _ST); //float mass = (float)p.vessel.totalMass; //float massInfluence = (mass * (float)FlightGlobals.currentMainBody.GeeASL * 9.8f); //Weight in Newtons //massInfluence = (1f - Mathf.Clamp((massInfluence - 186f) / (9800), 0, 1)); //~1000 tons and displacement doesn't matter much. Starts influencing after a 20 ton ship and depends on gravity //displacement *= massInfluence; float craftUpsideDownNess = Vector3.Dot(PhysicsStarter.terrainNormal, p.transform.up); //Value of 1 - Wheel is properly oriented. Value of -1 - Wheel is upside-f****n-down mate if (craftUpsideDownNess <= 0.4f && !p.isKerbalEVA()) { //We got 1.3 to play about with //Will use 0.3 of it to lower the plane to the ground //craftUpsideDownNess = Mathf.Clamp(craftUpsideDownNess, 0, 0.3f) / 0.3f; //0 to 1 //displacement *= craftUpsideDownNess; //Lower plane to the ground when the wheel is tilting too much. Probs played too many FPS games smh //dampingFrames = (int)((float)dampingFrames * craftUpsideDownNess); dampingFrames = 0; //craftOvertipped = true; displacement = -50; //displacement } if (lastDisplacement == 10000) { lastDisplacement = displacement; //First contact with the ground } Vector3 forward = Vector3.Normalize(gameObject.GetComponent <Rigidbody>().velocity); Vector3 tangent = Vector3.Cross(forward, sampleNormal); //Vector to rotate plane around Vector3 disp = new Vector3((displacement * _Displacement_Scale + _Displacement_Offset - 0.5f) * sampleNormal.x, (displacement * _Displacement_Scale + _Displacement_Offset - 0.5f) * sampleNormal.y, (displacement * _Displacement_Scale + _Displacement_Offset - 0.5f) * sampleNormal.z); if (dampingFrames <= 150) { dampingFrames++; } if (hitApproximateRay.collider.gameObject.name.Split(' ')[0] != FlightGlobals.currentMainBody.name) //If not casting down to terrain but instead hitting runway / scatter { plane.GetComponent <Collider>().enabled = false; plane.transform.position = samplePoint; PhysicsValidator.planes[plane] = true; started = true; return; } if (started == false || dampingFrames <= 150) { if (dampingFrames == 1) { ScreenMessages.PostScreenMessage("[Parallax Collisions] Damping...", 3f, ScreenMessageStyle.UPPER_LEFT); } Vector3 disp2 = new Vector3((displacement * _Displacement_Scale + _Displacement_Offset) * sampleNormal.x, (displacement * _Displacement_Scale + _Displacement_Offset) * sampleNormal.y, (displacement * _Displacement_Scale + _Displacement_Offset) * sampleNormal.z); plane.transform.position = samplePoint + (disp2 * ((float)dampingFrames / 150f)) + new Vector3(-0.5f * sampleNormal.x, -0.5f * sampleNormal.y, -0.5f * sampleNormal.z); //Slowly raise plane from terrain ground started = true; plane.GetComponent <Collider>().enabled = true; PhysicsValidator.planes[plane] = true; if (dampingFrames == 150) { ScreenMessages.PostScreenMessage("[Parallax Collisions] Finished Damping!", 3f, ScreenMessageStyle.UPPER_LEFT); } return; } plane.transform.position = samplePoint + disp; float rotationPercentage = 1 - ((lastDisplacement * _Displacement_Scale - _Displacement_Offset) / (displacement * _Displacement_Scale - _Displacement_Offset)); lastDisplacement = displacement; plane.transform.Rotate(tangent, rotationPercentage * 180); //plane.GetComponent<Collider>().enabled = true; EnablePlane(); PhysicsValidator.planes[plane] = true; started = true; }
public override void OnLoadVessel() { if (ParallaxSettings.collide == false) { return; } foreach (Part p in vessel.parts) { if (vessel.parts.Count == 1) { if (vessel.parts[0].isKerbalEVA() == true) { Debug.Log("This vessel is a Kerbal"); p.gameObject.AddComponent <ParallaxPhysics>(); var physicsComponent = p.gameObject.GetComponent <ParallaxPhysics>(); physicsComponent.transform.parent = p.gameObject.transform; physicsComponent.p = p; physicsComponent.origin = p.gameObject.transform.position; physicsComponent.wheelPivot = physicsComponent; } } if ((p.Modules.Contains <ModuleWheelBase>() || p.Modules.Contains <ParallaxWheelPhysicsComponent>() || p.Modules.Contains("KSPWheelBase") || p.Modules.Contains <ModuleGroundSciencePart>() || p.Modules.Contains <ModuleGroundPart>() || p.Modules.Contains <ModuleGroundExperiment>() || p.Modules.Contains <ModuleGroundCommsPart>() || p.Modules.Contains <ModulePhysicMaterial>() ) && p.gameObject.GetComponent <ParallaxPhysics>() == null) { if (p.Modules.Contains("KSPWheelBase")) { Debug.Log("Modded wheel detected - KSPWheelBase component"); } p.gameObject.AddComponent <ParallaxPhysics>(); var physicsComponent = p.gameObject.GetComponent <ParallaxPhysics>(); physicsComponent.p = p; foreach (Component c in p.gameObject.GetComponentsInChildren(typeof(Component))) { if (c.GetType().Name is "KSPWheelBase") { physicsComponent.origin = PhysicsStarter.GetPos(c.transform); physicsComponent.wheelPivot = c; } if (c.name == "WheelCollider") { physicsComponent.origin = PhysicsStarter.GetPos(c.transform); physicsComponent.wheelPivot = c; } else if (c.name == "foot") { physicsComponent.origin = PhysicsStarter.GetPos(c.transform); physicsComponent.wheelPivot = c; } else if (c.name == "leg_collider") { physicsComponent.origin = PhysicsStarter.GetPos(c.transform); physicsComponent.wheelPivot = c; } else if (c is ModuleGroundSciencePart || c is ModuleGroundSciencePart || c is ModuleGroundPart || c is ModuleGroundExperiment || c is ModuleGroundCommsPart || c is ModulePhysicMaterial) { physicsComponent.origin = PhysicsStarter.GetPos(c.transform); physicsComponent.wheelPivot = c; } else { //Can't find the component } } } if (p.gameObject.name == "miniLandingLeg") { foreach (Transform d in p.gameObject.GetComponentsInChildren(typeof(Transform))) { transforms.Add(d); } foreach (Transform d in p.gameObject.GetComponents(typeof(Transform))) { transforms.Add(d); } } if (p.Modules.Contains <ModuleWheelDeployment>()) { p.gameObject.GetComponent <ParallaxPhysics>().wheelDeploy = p.Modules.GetModule <ModuleWheelDeployment>(); if (p.Modules.GetModule <ModuleWheelDeployment>().position < 1) { p.gameObject.GetComponent <ParallaxPhysics>().wheelsHaveBeenRetracted = true; Debug.Log("A wheel is retracted, not starting collisions"); } //Debug.Log("ModuleWheelDeployment exists here"); //ModuleWheelDeployment mwd = p.Modules.GetModule<ModuleWheelDeployment>(); //mwd.on_deploy.OnEvent += DisableComponentOnGearRaise; //Do shit here that actually works } } }