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.

87 lines
2.6 KiB

  1. namespace Oculus.Platform.Samples.VrHoops
  2. {
  3. using UnityEngine;
  4. using System.Collections;
  5. // This component handles network coordination for moving balls.
  6. // Synchronizing moving objects that are under the influence of physics
  7. // and other forces is somewhat of an art and this example only scratches
  8. // the surface. Ultimately how you synchronize will depend on the requirements
  9. // of your application and its tolerance for users seeing slightly different
  10. // versions of the simulation.
  11. public class P2PNetworkBall : MonoBehaviour
  12. {
  13. // the last time this ball locally collided with something
  14. private float lastCollisionTime;
  15. // cached reference to the GameObject's Rigidbody component
  16. private Rigidbody rigidBody;
  17. void Awake()
  18. {
  19. rigidBody = gameObject.GetComponent<Rigidbody>();
  20. }
  21. public Vector3 velocity
  22. {
  23. get { return rigidBody.velocity; }
  24. }
  25. public bool IsHeld()
  26. {
  27. return !rigidBody.useGravity;
  28. }
  29. public void ProcessRemoteUpdate(float remoteTime, bool isHeld, Vector3 pos, Vector3 vel)
  30. {
  31. if (isHeld)
  32. {
  33. transform.localPosition = pos;
  34. }
  35. // if we've collided since the update was sent, our state is going to be more accurate so
  36. // it's better to ignore the update
  37. else if (lastCollisionTime < remoteTime)
  38. {
  39. // To correct the position this sample directly moves the ball.
  40. // Another approach would be to gradually lerp the ball there during
  41. // FixedUpdate. However, that approach aggravates any errors that
  42. // come from estimatePosition and estimateVelocity so the lerp
  43. // should be done over few timesteps.
  44. float deltaT = Time.realtimeSinceStartup - remoteTime;
  45. transform.localPosition = estimatePosition(pos, vel, deltaT);
  46. rigidBody.velocity = estimateVelocity(vel, deltaT);
  47. // if the ball is transitioning from held to ballistic, we need to
  48. // update the RigidBody parameters
  49. if (IsHeld())
  50. {
  51. rigidBody.useGravity = true;
  52. rigidBody.detectCollisions = true;
  53. }
  54. }
  55. }
  56. // Estimates the new position assuming simple ballistic motion.
  57. private Vector3 estimatePosition(Vector3 startPosition, Vector3 startVelocty, float time)
  58. {
  59. return startPosition + startVelocty * time + 0.5f * Physics.gravity * time * time;
  60. }
  61. // Estimates the new velocity assuming ballistic motion and drag.
  62. private Vector3 estimateVelocity(Vector3 startVelocity, float time)
  63. {
  64. return startVelocity + Physics.gravity * time * Mathf.Clamp01 (1 - rigidBody.drag * time);
  65. }
  66. void OnCollisionEnter(Collision collision)
  67. {
  68. lastCollisionTime = Time.realtimeSinceStartup;
  69. }
  70. void OnDestroy()
  71. {
  72. PlatformManager.P2P.RemoveNetworkBall(gameObject);
  73. }
  74. }
  75. }