Assignment for RMIT Mixed Reality in 2020
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

88 lines
2.5 KiB

  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. public class RotationController : MonoBehaviour
  5. {
  6. #region Read-Only Fields
  7. public float RotationPeriod => m_RotationPeriod;
  8. public Vector3 Position => transform.position;
  9. public Vector3 RotationAxis => m_RotationAxis;
  10. public float CoriolisStrength => m_coriolisStrength;
  11. public float EnvironmentRotationPeriod => m_RotationPeriod / m_SkyboxRotationMultiplier;
  12. #endregion Read-Only Fields
  13. [SerializeField]
  14. private float m_RotationPeriod;
  15. [SerializeField]
  16. private Vector3 m_RotationAxis = Vector3.forward;
  17. [SerializeField]
  18. private float m_coriolisStrength = 100;
  19. [SerializeField]
  20. private Material m_Skybox;
  21. [SerializeField]
  22. private float m_SkyboxRotationMultiplier = 1;
  23. public Vector3 getDownDirection(Vector3 objectPosition, bool normalized = true)
  24. {
  25. if (normalized)
  26. return Vector3.ProjectOnPlane((objectPosition - transform.position), m_RotationAxis).normalized;
  27. else
  28. return Vector3.ProjectOnPlane((objectPosition - transform.position), m_RotationAxis);
  29. }
  30. public Vector3 getPerpendicularDirection(Vector3 objectPosition)
  31. {
  32. return Vector3.Cross(RotationAxis, getDownDirection(objectPosition)).normalized;
  33. }
  34. public Quaternion getUpRotation(Vector3 objectPosition)
  35. {
  36. return Quaternion.FromToRotation(Vector3.up, -getDownDirection(objectPosition));
  37. }
  38. public float GetGravityAtRadius(float radius)
  39. {
  40. return radius * Mathf.Pow(2 * Mathf.PI / RotationPeriod, 2);
  41. }
  42. public Vector3 GetGravityAtPoint(Vector3 point)
  43. {
  44. Vector3 direction = getDownDirection(point,false);
  45. return direction.normalized * GetGravityAtRadius(direction.magnitude);
  46. }
  47. public Vector3 GetCoriolisAtPoint(Vector3 point)
  48. {
  49. return getPerpendicularDirection(point) * Mathf.Pow(2 * Mathf.PI / RotationPeriod, 2) * CoriolisStrength;
  50. }
  51. public void SetGravityAtRadius(float Strength, float radius)
  52. {
  53. m_RotationPeriod = 2 * Mathf.PI * Mathf.Sqrt(radius / Strength);
  54. }
  55. private void Start()
  56. {
  57. if (m_Skybox == null)
  58. m_Skybox = RenderSettings.skybox;
  59. }
  60. private void Update()
  61. {
  62. if (m_Skybox != null)
  63. {
  64. m_Skybox.SetVector("_RotationAxis", m_RotationAxis);
  65. m_Skybox.SetFloat("_Rotation", (Time.time * 360/EnvironmentRotationPeriod) % 360);
  66. }
  67. }
  68. }