void UpdateDownwardsForces() { if (WaterGrid != null) { foreach (var Origin in DownwardsRaycastOrigins) { Ray DownwardsRay = new Ray(Origin.transform.position, new Vector3(0.0f, -1.0f, 0.0f)); var waterBitMask = 1 << 4; if (Physics.Raycast(DownwardsRay, out raycastResult, DefaultDistanceFromWater, waterBitMask)) { GameObject obj = raycastResult.collider.gameObject; if (MassSpringSystem.IsMassUnit(obj.tag)) { Vector3 p = obj.transform.position - WaterGrid.GetUnitOffset(); float pressure = SimulatedPressure * Origin.Force; if (Body != null) { pressure *= Mathf.Clamp(Body.mass / MaxMass, 0.0f, 1.0f); } //need to translate back from unity world space so we use z here rather than y WaterGrid.GridTouches.Add(new Vector3(p.x, p.z, pressure)); } } } } }
/** Cast a ray from the given screen position and check for collision with mass objects. * If there is a collision with a mass object, add a touch point to the grid touches array * (e.g. to be later be handled by a MassSpringSystem controller). */ public void ProjectScreenPositionToMassSpringGrid(Vector2 screenPosition) { Ray ray = Camera.main.ScreenPointToRay(screenPosition); if (Physics.Raycast(ray, out raycastResult)) { GameObject obj = raycastResult.collider.gameObject; if (MassSpringSystem.IsMassUnit(obj.tag)) { Vector3 p = obj.transform.position; //need to translate back from unity world space so we use z here rather than y GridTouches.Add(new Vector3(p.x, p.z, SimulatedPressure)); } } }