CelestialBody.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. 'use strict';
  2. var OrbitalElements = require('./OrbitalElements');
  3. var ns = require('ns');
  4. var THREE = require('./Three.shim');
  5. var CelestialBody = {
  6. init : function() {
  7. this.reset();
  8. this.movement = new THREE.Vector3();
  9. this.invMass = 1 / this.mass;
  10. this.orbitalElements = Object.create(OrbitalElements);
  11. this.orbitalElements.setName(this.name);
  12. this.orbitalElements.setDefaultOrbit(this.orbit, this.orbitCalculator);
  13. //console.log(this.name, this.position, this.velocity);
  14. },
  15. reset : function(){
  16. this.angle = 0;
  17. this.force = new THREE.Vector3();
  18. this.movement = new THREE.Vector3();
  19. this.previousPosition = null;
  20. },
  21. //if epoch start is not j2000, get epoch time from j2000 epoch time
  22. getEpochTime : function(epochTime) {
  23. if(this.epoch) {
  24. epochTime = epochTime - ((this.epoch.getTime() - ns.J2000) / 1000);
  25. }
  26. return epochTime;
  27. },
  28. setPositionFromDate : function(epochTime, calculateVelocity) {
  29. epochTime = this.getEpochTime(epochTime);
  30. this.position = this.isCentral ? new THREE.Vector3() : this.orbitalElements.getPositionFromElements(this.orbitalElements.calculateElements(epochTime));
  31. this.relativePosition = new THREE.Vector3();
  32. if(calculateVelocity) {
  33. this.velocity = this.isCentral ? new THREE.Vector3() : this.orbitalElements.calculateVelocity(epochTime, this.relativeTo, this.calculateFromElements);
  34. }
  35. this.positionRelativeTo();
  36. },
  37. getAngleTo : function(bodyName){
  38. var ref = require('./SolarSystem').getBody(bodyName);
  39. if(ref) {
  40. var eclPos = this.position.clone().sub(ref.getPosition()).normalize();
  41. eclPos.z = 0;
  42. var angleX = eclPos.angleTo(new THREE.Vector3(1, 0, 0));
  43. var angleY = eclPos.angleTo(new THREE.Vector3(0, 1, 0));
  44. //console.log(angleX, angleY);
  45. var angle = angleX;
  46. var q = Math.PI / 2;
  47. if(angleY > q) angle = -angleX;
  48. return angle;
  49. }
  50. return 0;
  51. },
  52. positionRelativeTo : function(){
  53. if(this.relativeTo) {
  54. var central = require('./SolarSystem').getBody(this.relativeTo);
  55. if(central && central!==require('./SolarSystem').getBody()/**/) {
  56. this.position.add(central.position);
  57. //console.log(this.name+' pos rel to ' + this.relativeTo);
  58. this.velocity && central.velocity && this.velocity.add(central.velocity);
  59. }
  60. }
  61. },
  62. calculatePosition : function(t) {
  63. return this.orbitalElements.calculatePosition(t);
  64. },
  65. getPosition : function(){
  66. return this.position.clone();
  67. },
  68. getVelocity : function(){
  69. return this.velocity && this.velocity.clone();
  70. },
  71. //return true/false if this body is orbiting the requested body
  72. isOrbitAround : function(celestial){
  73. return celestial.name === this.relativeTo;
  74. }
  75. };
  76. module.exports = CelestialBody;