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.

92 lines
2.9 KiB

  1. /************************************************************************************
  2. Copyright (c) Facebook Technologies, LLC and its affiliates. All rights reserved.
  3. See SampleFramework license.txt for license terms. Unless required by applicable law
  4. or agreed to in writing, the sample code is provided AS IS WITHOUT WARRANTIES OR
  5. CONDITIONS OF ANY KIND, either express or implied. See the license for specific
  6. language governing permissions and limitations under the license.
  7. ************************************************************************************/
  8. using UnityEngine;
  9. using UnityEngine.Assertions;
  10. namespace OculusSampleFramework
  11. {
  12. public abstract class TrainCarBase : MonoBehaviour
  13. {
  14. private static Vector3 OFFSET = new Vector3(0f, 0.0195f, 0f);
  15. private const float WHEEL_RADIUS = 0.027f;
  16. private const float TWO_PI = Mathf.PI * 2.0f;
  17. [SerializeField] protected Transform _frontWheels = null;
  18. [SerializeField] protected Transform _rearWheels = null;
  19. [SerializeField] protected TrainTrack _trainTrack = null;
  20. [SerializeField] protected Transform[] _individualWheels = null;
  21. public float Distance { get; protected set; }
  22. protected float scale = 1.0f;
  23. private Pose _frontPose = new Pose(), _rearPose = new Pose();
  24. public float Scale
  25. {
  26. get { return scale; }
  27. set { scale = value; }
  28. }
  29. protected virtual void Awake()
  30. {
  31. Assert.IsNotNull(_frontWheels);
  32. Assert.IsNotNull(_rearWheels);
  33. Assert.IsNotNull(_trainTrack);
  34. Assert.IsNotNull(_individualWheels);
  35. }
  36. public void UpdatePose(float distance, TrainCarBase train, Pose pose)
  37. {
  38. // distance could be negative; add track length to it in case that happens
  39. distance = (train._trainTrack.TrackLength + distance) % train._trainTrack.TrackLength;
  40. if (distance < 0)
  41. {
  42. distance += train._trainTrack.TrackLength;
  43. }
  44. var currentSegment = train._trainTrack.GetSegment(distance);
  45. var distanceInto = distance - currentSegment.StartDistance;
  46. currentSegment.UpdatePose(distanceInto, pose);
  47. }
  48. protected void UpdateCarPosition()
  49. {
  50. UpdatePose(Distance + _frontWheels.transform.localPosition.z * scale,
  51. this, _frontPose);
  52. UpdatePose(Distance + _rearWheels.transform.localPosition.z * scale,
  53. this, _rearPose);
  54. var midPoint = 0.5f * (_frontPose.Position + _rearPose.Position);
  55. var carLookDirection = _frontPose.Position - _rearPose.Position;
  56. transform.position = midPoint + OFFSET;
  57. transform.rotation = Quaternion.LookRotation(carLookDirection, transform.up);
  58. _frontWheels.transform.rotation = _frontPose.Rotation;
  59. _rearWheels.transform.rotation = _rearPose.Rotation;
  60. }
  61. protected void RotateCarWheels()
  62. {
  63. // divide by radius to get angle
  64. float angleOfRot = (Distance / WHEEL_RADIUS) % TWO_PI;
  65. foreach (var individualWheel in _individualWheels)
  66. {
  67. individualWheel.localRotation = Quaternion.AngleAxis(Mathf.Rad2Deg * angleOfRot,
  68. Vector3.right);
  69. }
  70. }
  71. public abstract void UpdatePosition();
  72. }
  73. }