Guest User

Untitled

a guest
Mar 9th, 2017
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.62 KB | None | 0 0
  1. public class PhysicsBody : MonoBehaviour {
  2. [Header( "Calculated Variables" )]
  3. public Vector3 CenterOfMass;
  4. public double TotalMass;
  5. public double MomentOfInertia;
  6. public Vector3 CurrentVelocity;
  7. public double CurrentTime;
  8. public Vector3 InitialVelocity;
  9. public Vector3 CurrentAngularVelocity;
  10. public Vector3 CurrentAngularAcceleration;
  11. public Vector3 AngularDisplacement = Vector3.zero;
  12. public float Timer = 0f;
  13. public Vector2 PointPosition;
  14. public Vector2 PointVelocity;
  15. //Flipped signs for display
  16. public Vector2 PointAcceleration;
  17. public float Theta;
  18. public float Omega;
  19. public float Alpha;
  20. public float Gamma;
  21. public Vector2 Radial = Vector2.zero;
  22.  
  23. [Header( "Preset Variables" )]
  24. public bool Gravity = false;
  25. public float Drag = 0.0f; // N/m/s
  26. public float WindMagnitude = 0.0f; // m/s
  27. public bool WindFacingObject = true;
  28. public Vector3 WindDirection = Vector3.zero;
  29. public float Cw = 0.1f; // N/m/s
  30. public float AccelerationTime = 8;
  31. public float ConstantAcceleration = -10; //m/s^2
  32. public float InitialVelocitySpeed = 100f; //ms/s
  33. public Vector3 InitialVelocityAngles = new Vector3( 0, 0, 0 ); //angles
  34. public Vector3 InitialAngularVelocity = new Vector3(0, 0, 1.8f); //angular velocity, radians per second
  35. public Vector3 InitialAngularAcceleration = new Vector3(0, 0, 0);
  36.  
  37. [Header( "Controls" )]
  38. [InspectorButton( "StartToAcceleration" )]
  39. public bool Accelerate;
  40. [InspectorButton( "ResetPhysics" )]
  41. public bool Reset;
  42.  
  43. private bool m_currentlyMoving = false;
  44. private Vector3 m_startPosition;
  45. private Transform m_reference;
  46.  
  47. // Use this for initialization
  48. void Start() {
  49. UpdateValues();
  50. m_startPosition = transform.position;
  51. var reference = new GameObject ();
  52. reference.name = "Physics";
  53. m_reference = reference.transform;
  54. }
  55.  
  56. PhysicsProperties[] GetPhysicsProperties() {
  57. return GetComponentsInChildren<PhysicsProperties>();
  58. }
  59.  
  60. double GetTotalMass() {
  61. var totalMass = 0.0;
  62. foreach ( var child in GetPhysicsProperties() ) {
  63. totalMass += child.Mass;
  64. }
  65. return totalMass;
  66. }
  67.  
  68. Vector3 GetCenterOfMass() {
  69. var xCenterMass = 0.0;
  70. var yCenterMass = 0.0;
  71. var zCenterMass = 0.0;
  72. foreach ( var child in GetPhysicsProperties() ) {
  73. var childCenterPoint = child.GetCenterPoint();
  74. xCenterMass += child.Mass * childCenterPoint.x;
  75. yCenterMass += child.Mass * childCenterPoint.y;
  76. zCenterMass += child.Mass * childCenterPoint.z;
  77. }
  78. xCenterMass /= TotalMass;
  79. yCenterMass /= TotalMass;
  80. zCenterMass /= TotalMass;
  81. return new Vector3( (float)xCenterMass, (float)yCenterMass, (float)zCenterMass );
  82. }
  83.  
  84. void CalculateDistanceFromCenterOfMassInChildren() {
  85. foreach ( var child in GetPhysicsProperties() ) {
  86. child.GetDistanceFromCenterOfMass( CenterOfMass );
  87. }
  88. }
  89.  
  90. double GetTotalMomentOfInertia() {
  91. var inertia = 0.0;
  92. foreach ( var child in GetPhysicsProperties() ) {
  93. inertia += child.GetMomentOfIntertiaTheCOM( CenterOfMass );
  94. }
  95. return inertia;
  96. }
  97.  
  98. public void UpdateValues() {
  99. TotalMass = GetTotalMass();
  100. CenterOfMass = GetCenterOfMass();
  101. CalculateDistanceFromCenterOfMassInChildren();
  102. MomentOfInertia = GetTotalMomentOfInertia();
  103. }
  104.  
  105. public void UpdatePosition( double seconds ) {
  106. //Bullet through air with air resistance
  107. if (!m_gunStop)
  108. {
  109. //Old way, used if Drag is set to 0
  110. if (Drag == 0)
  111. {
  112. //In the same way we had to rotate 90 degrees to face the target, we rotate on that same y axis to point in its direction, not the z axis
  113.  
  114. var deceleration = new Vector3(0, -9.81f, 0);
  115.  
  116. //x/y/z velocity
  117. transform.position = transform.position + (float)seconds * CurrentVelocity;
  118. CurrentVelocity += deceleration * (float)seconds;
  119.  
  120. //angular velocity
  121. var degreesToRotate = -CurrentAngularVelocity * (float)seconds * Mathf.Rad2Deg;
  122.  
  123. transform.rotation *= Quaternion.Euler(degreesToRotate);
  124. AngularDisplacement += CurrentAngularVelocity * (float)seconds;
  125. CurrentAngularVelocity = CurrentAngularVelocity + CurrentAngularAcceleration * (float)seconds;
  126.  
  127. //points angular stuff
  128. var currentAngularAccelerationShouldBeWhyDidIInvertTheFuckingPlanes = new Vector3(CurrentAngularAcceleration.z, CurrentAngularAcceleration.y, CurrentAngularAcceleration.x);
  129. var currentAngularVelicityShouldBe = new Vector3(CurrentAngularVelocity.z, CurrentAngularVelocity.y, CurrentAngularVelocity.x);
  130. if (transform.childCount > 0)
  131. {
  132. var point = transform.GetChild(0);
  133. var R = point.transform.position - transform.position;
  134. print(R);
  135. PointPosition = point.transform.position;
  136. PointVelocity = CurrentVelocity + (Vector3.Cross(currentAngularVelicityShouldBe, R));
  137. var pointAcceleration = new Vector3(0.0f, -9.81f, 0.0f) + (Vector3.Cross(currentAngularAccelerationShouldBeWhyDidIInvertTheFuckingPlanes, R)) + (Vector3.Cross(currentAngularVelicityShouldBe, Vector3.Cross(currentAngularVelicityShouldBe, R)));
  138. PointAcceleration = new Vector3(pointAcceleration.x, pointAcceleration.y, pointAcceleration.z);
  139. }
  140. // Project 08 code
  141. }
  142. else {
  143. var m = GetTotalMass();
  144. var xo = m_startPosition.x;
  145. var vxo = InitialVelocity.x;
  146. var t = Timer;
  147. var Cd = Drag;
  148. var Tau = (float)m / Cd;
  149. var w = WindMagnitude;
  150. var g = -9.81f;
  151. var yo = m_startPosition.y;
  152. var vyo = InitialVelocity.y;
  153. var zo = m_startPosition.z;
  154. var vzo = InitialVelocity.z;
  155. var gamma = Gamma;
  156.  
  157. //Tau * (1-E^(-tTau))
  158. var TauE = Tau * (1 - Mathf.Exp(-t / Tau));
  159. //Cw * w * Cos(y) / Cd
  160. var CwWCosCd = ((Cw * w * Mathf.Cos(gamma)) / Cd);
  161. //Cw * w * Sin(y) / Cd
  162. var CwWSinCd = ((Cw * w * Mathf.Sin(gamma)) / Cd);
  163.  
  164. var x = xo + (vxo * TauE) + (CwWCosCd * Tau * TauE) - (CwWCosCd * t);
  165. var y = yo + (vyo * TauE) + (g * Tau * Tau * TauE) - (g * Tau * t);
  166. var z = zo + (vzo * TauE) + (CwWSinCd * Tau * TauE) - (CwWSinCd * t);
  167.  
  168. transform.position = new Vector3(x, -y, z);
  169. }
  170. Timer += (float)seconds;
  171. }
  172.  
  173. if (transform.position.y <= 0)
  174. {
  175. m_currentlyMoving = false;
  176. }
  177. }
  178.  
  179. public void StartToAcceleration() {
  180. var gunAngleToHit = GameObject.Find ("Gun").GetComponent<PhysicsTargetting> ().GunAngleToHit;
  181. InitialVelocityAngles = new Vector3 (gunAngleToHit.x, gunAngleToHit.y, 0.0f);
  182. m_startPosition = transform.position;
  183. m_currentlyMoving = true;
  184. CurrentTime = 0;
  185. m_reference.eulerAngles = new Vector3( InitialVelocityAngles.x, (-InitialVelocityAngles.y) + 90 - InitialVelocityAngles.z, m_reference.eulerAngles.z );
  186. var initialVelocityDirection = m_reference.forward;
  187. InitialVelocity = InitialVelocitySpeed * initialVelocityDirection;
  188. CurrentVelocity = InitialVelocity;
  189. CurrentAngularVelocity = InitialAngularVelocity;
  190. CurrentAngularAcceleration = InitialAngularAcceleration;
  191. Timer = 0f;
  192. }
  193.  
  194. public void ResetPhysics() {
  195. transform.position = m_startPosition;
  196. CurrentTime = 0;
  197. m_currentlyMoving = false;
  198. InitialVelocity = Vector3.zero;
  199. CurrentVelocity = Vector3.zero;
  200. m_gunStop = false;
  201. var trail = GetComponent<TrailRenderer>();
  202.  
  203. if ( trail != null ) {
  204. trail.Clear();
  205. }
  206. }
  207.  
  208. void FixedUpdate() {
  209. CenterOfMass = GetCenterOfMass ();
  210. if ( m_currentlyMoving ) {
  211. UpdatePosition( Time.deltaTime );
  212. }
  213. }
  214.  
  215. private bool m_gunStop = false;
  216. void OnTriggerEnter( Collider other ) {
  217. if ( other.gameObject.tag == "Immobile" ) {
  218. CurrentVelocity = Vector3.zero;
  219. m_gunStop = true;
  220. }
  221. }
  222.  
  223. public void ApplyForce(Vector3 atPoint, Vector3 direction, float newtons, float deltaTime) {
  224. if (!m_currentlyMoving) {
  225. StartToAcceleration ();
  226. m_gunStop = true;
  227. }
  228. print ("ApplyingForce Called");
  229. var F = newtons;// * direction; //Only on one direction s
  230. var R = Vector2.Distance(new Vector2(CenterOfMass.x, CenterOfMass.z), new Vector2(atPoint.x, atPoint.z));//CenterOfMass - (transform.position + atPoint);
  231. var forceDirectionRaw = atPoint + direction;
  232. var forceDirection2D = new Vector2 (forceDirectionRaw.x, forceDirectionRaw.z);
  233. var COMRaw = CenterOfMass - atPoint;
  234. var COM2D = new Vector2 (COMRaw.x, COMRaw.z);
  235. var T = 180f - Vector3.Angle (forceDirection2D, COM2D); // Angle between force direction and direction to COM
  236. //print((CenterOfMass - atPoint) + " " + direction);
  237. //print(T);
  238. //print (T + " " + (transform.position + direction.normalized) + " " + (transform.position + (CenterOfMass - transform.position).normalized));
  239. var NewT = Mathf.Abs(CenterOfMass.z - atPoint.z)/R;
  240. var angularVelocityBeforeDivision = new Vector3(0.0f, R * F * NewT, 0.0f);//* Mathf.Sin (T);
  241. var angularAcceleration = angularVelocityBeforeDivision / (float)MomentOfInertia;
  242. var accelerationMagnitude = newtons / TotalMass;
  243. var acceleration = direction * (float)accelerationMagnitude * deltaTime; // m/s
  244. //print(deltaTime);
  245.  
  246. var degreesToRotate = -CurrentAngularVelocity * deltaTime * Mathf.Rad2Deg;
  247. //transform.rotation *= Quaternion.Euler (degreesToRotate);
  248. transform.RotateAround(CenterOfMass, Vector3.up, degreesToRotate.y);
  249.  
  250. AngularDisplacement += CurrentAngularVelocity * deltaTime;
  251. Theta = AngularDisplacement.y;
  252. Omega = CurrentAngularVelocity.y;
  253. Alpha = CurrentAngularAcceleration.y;
  254. CurrentAngularAcceleration = angularAcceleration;
  255. CurrentAngularVelocity = CurrentAngularVelocity + CurrentAngularAcceleration * deltaTime;
  256.  
  257. print(string.Format("Acceleration: {0}", direction * (float)accelerationMagnitude));
  258. CurrentVelocity += acceleration;
  259. }
  260. }
Advertisement
Add Comment
Please, Sign In to add comment