void calculateResults() { //Make sure a gameobject is selected! if (SelectedObject != null) { bool Failed = false; //First calculate the density of the object. if (SelectedObject.GetComponent <MeshFilter>() && SelectedObject.GetComponent <MeshFilter>().sharedMesh != null) { // Use meshfilter based bounds CalculatedVolume = MeshVolume.getVolume(SelectedObject as GameObject); } else if (SelectedObject.GetComponent <Collider>()) { // Use collider based bounds CalculatedVolume = MeshVolume.getVolumeByColliderBounds(SelectedObject.GetComponent <Collider>()); } else { Debug.Log("[Realistic Water Physics] Object is missing a mesh filter or collider."); Failed = true; } if (!Failed) // a small fix! { //Now the percentage thats solit and the remaining is air. float percentageSolidMass = CalculatedVolume * ((selectedMaterialValue / 100) * percentageSolid); float percentageAir = CalculatedVolume * ((RealisticWaterPhysics.currentAirDensity / 100) * (100 - percentageSolid)); //The totaal mass of the object is the amount of solit material + the remaining amount of air. CalculatedMass = percentageSolidMass + percentageAir; //the density of the intire object is its calculated mass / its volume. CalculatedDensity = CalculatedMass / CalculatedVolume; CalculatedWaterDensity = PhysicsMaterialsList.getLiquidsMaterialValue(selectedOceanLiquidsMaterial); CalculatedAirDensity = airDensity; if (CalculatedDensity > CalculatedWaterDensity) // if denser then water density it will sink. { CalculatedWillFloatInWater = false; } else { CalculatedWillFloatInWater = true; } if (CalculatedDensity > CalculatedAirDensity) // if denser then air density it will sink { CalculatedWillFloatInAir = false; } else { CalculatedWillFloatInAir = true; } DidTest = true; } } }
// Jacob changed this from private to public public void setup() { //Eanble support #if PW findPlayWaterWater(); #endif if (RealisticWaterPhysics.currentDebugEnabled) { forces = new List <Vector3[]>(); // For drawing force gizmos } selectedMaterialValue = getSelectedMaterialValue(); _thisRigidbody = this.GetComponent <Rigidbody>(); waterDensity = RealisticWaterPhysics.currentWaterDensity; isMeshCollider = GetComponent <MeshCollider>() != null; if (this.gameObject.GetComponent <MeshFilter>() && this.gameObject.GetComponent <MeshFilter>().sharedMesh != null) { volume = MeshVolume.getVolume(this.gameObject); Bounds meshBounds = GetComponent <MeshFilter>().sharedMesh.bounds; bounds = new Bounds(); //A fix for a odd bug. bounds.center = meshBounds.center; bounds.extents = new Vector3( meshBounds.extents.x * this.transform.localScale.x, meshBounds.extents.y * this.transform.localScale.y, meshBounds.extents.z * this.transform.localScale.z); //meshBounds.extents * this.transform.localScale; if (RealisticWaterPhysics.currentDebugEnabled) { Debug.LogFormat("bounds.extents={0} |bounds.center={1} |this.transform.position={2} |_thisRigidbody.centerOfMass={3}", bounds.extents, bounds.center, this.transform.position, _thisRigidbody.centerOfMass); } //if object scale = 10,10,10 and sharedMesh.bounds; returns 0.5 then result must be 5,5,5; FIX THIS !!! secondSetup(); } else if (this.gameObject.GetComponent <Collider>()) { // Use collider based bounds if (RealisticWaterPhysics.currentDebugEnabled) { Debug.LogFormat("[Realistic Water Physics] Object: {0} is missing a mesh filter, using collider Bounds!", this.gameObject.name); } volume = MeshVolume.getVolumeByColliderBounds(this.gameObject.GetComponent <Collider>()); bounds = GetComponent <Collider>().bounds; secondSetup(); } else { Debug.LogFormat("[Realistic Water Physics] Object: {0} is missing a mesh filter and collider.", this.gameObject.name); this.gameObject.SetActive(false); } }
/// Calculates physics. /// private void CalculatePhysics() { if (_thisRigidbody.isKinematic == false) { if (HeavyRunTimeEdits || LightRunTimeEdits && CheckOlds()) { if (HeavyRunTimeEdits) { if (this.gameObject.GetComponent <MeshFilter>() && this.gameObject.GetComponent <MeshFilter>().mesh != null) { volume = MeshVolume.getVolume(this.gameObject); } else if (this.gameObject.GetComponent <Collider>()) { // Use collider based bounds volume = MeshVolume.getVolumeByColliderBounds(this.gameObject.GetComponent <Collider>()); } else { return; } } percentageSolidMass = volume * ((selectedMaterialValue / 100) * percentageSolid); percentageAir = volume * ((RealisticWaterPhysics.currentAirDensity / 100) * (100 - percentageSolid)); //The totaal mass of the object is the amount of solit material + the remaining amount of air. mass = percentageSolidMass + percentageAir; //the density of the intire object is its calculated mass / its volume. density = mass / volume; _thisRigidbody.mass = mass; getRunTimeEditsInfo(); } if (RealisticWaterPhysics.currentDebugEnabled) // Just skip this if debug info is disabled. { forces.Clear(); // For drawing force gizmos } if (BuoyancyEnabled) { if (voxels == null) { if (reSetupTimeout < 5) // its a fail safe, if indeed no voxels are found and resetup is done 5 times, it wil trip! { reSetup(); } else { this.GetComponent <RealisticBuoyancy>().enabled = false; Debug.LogErrorFormat("Gameobject {0} Disabled due to incorrect setup!", this.name); } } else { inWater = false; for (int i = 0; i < voxels.Count; i++) { var wp = transform.TransformPoint(voxels[i]); float waterLevel = GetWaterLevel(wp.x, wp.z); if (wp.y - voxelHalfHeight < waterLevel) // if underwater { inWater = true; float k = (waterLevel - wp.y) / (2 * voxelHalfHeight) + 0.5f; if (k > 1) { k = 1f; } else if (k < 0) { k = 0f; } var velocity = GetComponent <Rigidbody>().GetPointVelocity(wp); var localDampingForce = -velocity * DAMPFER * _thisRigidbody.mass; var force = localDampingForce + Mathf.Sqrt(k) * localArchimedesForce; //Again this is for MovePosition if (ignoreChanges == true && ignoreChangesDelay < ignoreChangesMaxDelay) { ignoreChangesDelay++; } else { if (ignoreChangesDelay > ignoreChangesMaxDelay) { ignoreChanges = false; } _thisRigidbody.AddForceAtPosition(force, wp); } if (RealisticWaterPhysics.currentDebugEnabled) { forces.Add(new[] { wp, force }); // For drawing force gizmos } } else //not underwater? so above water! { if (RealisticWaterPhysics.currentAirDensity > 0f) //is there air to calculate with ? { float k = ((waterLevel + RealisticWaterPhysics.currentAirLevel) - wp.y) / (2 * voxelHalfHeight) + 0.5f; if (k > 1) { k = 1f; } else if (k < 0) { k = 0f; } var velocity = GetComponent <Rigidbody>().GetPointVelocity(wp); //var localDampingForce = -velocity * DAMPFER * GetComponent<Rigidbody>().mass; var localDampingForce = -velocity * 0.01f * _thisRigidbody.mass; //remove the dempfer, but keep a tiny bit in. var force = localDampingForce + Mathf.Sqrt(k) * localArchimedesForceAir; if (ignoreChanges == true && ignoreChangesDelay < ignoreChangesMaxDelay) { ignoreChangesDelay++; } else { if (ignoreChangesDelay > ignoreChangesMaxDelay) { ignoreChanges = false; } _thisRigidbody.AddForceAtPosition(force, wp); } if (RealisticWaterPhysics.currentDebugEnabled) // Just skip this if debug info is disabled. { forces.Add(new[] { wp, force }); // For drawing force gizmos } } } } } } else { if (HeavyRunTimeEdits || LightRunTimeEdits) { #if DW if (RealisticWaterPhysics.currentUseDW == true) { if (GetComponent <BuoyancyForce>() == false || GetComponent <BuoyancyForce>().enabled == false) { //System is disabled or not found so turn the system back on! BuoyancyEnabled = true; } } #endif } } BuoyancyActive = BuoyancyEnabled; } }