0

I am trying to approximate the position of the sun in XYZ for a threejs project.

I am following the maths found here: http://en.wikipedia.org/wiki/Position_of_the_Sun

Following the above, I have written the following Javascript code:

    var n = ((2440587.5 + (this.datemillis / 8.64E7)) - 2451545);
    var L = 280.460 + 0.9856474 * n;
    var g = 357.528 + 0.9856003 * n;
    L = (L + 360) % 360;
    g = (g + 360) % 60;
    var lambda = L + 1.915 * Math.sin(g) + 0.0020 * Math.sin(2 * g);
    var r = 1.00014 - 0.01671 * Math.cos(g) - 0.00014 * Math.cos(2 * g);
    var e = 23.439 - 0.0000004 * n;

    var x = (r * this.constants.EARTH_RADIUS * 2) * Math.cos(lambda);
    var y = (r * this.constants.EARTH_RADIUS * 2) * Math.cos(e) * Math.sin(lambda);
    var z = (r * this.constants.EARTH_RADIUS * 2) * Math.sin(e) * Math.sin(lambda);

this.datemillis is returned by the getMillisecond function of the Javascript date object. It is updated each frame so that time advances at about 1 hour every 2 seconds.

However something must not be correct as this does not produce the expected result. When I apply the computed x y z coordinates to my sun in my threejs project, I can see the sun rotate around the earth (sitting in 0,0,0) but at a very slow rate (rotating the earth in a few days instead of 24 hours).

I'm thinking it might have something to do with the angle calculations that I'm not doing correctly (degrees/radians?) but I'm not very good at maths so I don't really know what I'm doing so maybe I just misinterpreted the Wiki calculations.

If somebody could spot something obvious I'm doing wrong and help me fix this, would be greatly appreciated!

Thanks

EDIT: so my sun currently is not rotating around the earth in a continous way - it rotates clockwise/counterclockwise alternatively and sometimes jumps positions...

Nicolas
  • 403
  • 1
  • 6
  • 18
  • There are some issues on your code. Basically the radians/degrees inconsistency but also other stuff too. I did rewrite my answer to be more exaustive. Hope this helps. Btw: I didn't check every constant and formula with the ones in the wikipedia page. I assume they're correct... Double check them if you're not sure – Paolo Apr 29 '15 at 14:25

2 Answers2

1

I suggest this to get the Julian Date, from Calculating Jday(Julian Day) in javascript

var today = Date();
var JD = Math.floor((today / 86400000) - (today.getTimezoneOffset()/1440) + 2440587.5);

Add to JD the desired amount of days and increment that value at the desired speed. Note that if you add 1 day each millisecond you'll get 1000 days per second, not 1 hour every 2 seconds.

JD += offset;

Then go on with the wikipedia recipe:

var n = JD - 2451545;

 //...

To put L and g in the range 0-360 (you have an error here) use

L = L % 360 + ( L < 0 ? 360 : 0 );
g = g % 360 + ( g < 0 ? 360 : 0 );

The wikipedia formulas express angles in degrees. However JavaScript trigonometric functions cos and sin expect radians.

Just write a "degrees" version of them:

function cosD( deg ) {
    return Math.cos( deg * Math.PI / 180.0 );
}

function sinD( deg ) {
    return Math.sin( deg * Math.PI / 180.0 );
}

Then use sinD() and cosD() in subsequent calculations.

var r = 1.00014 - 0.01671 * cosD(g) - 0.00014 * cosD(2 * g);
var e = 23.439 - 0.0000004 * n;

var x = (r * this.constants.EARTH_RADIUS * 2) * cosD(lambda);
var y = (r * this.constants.EARTH_RADIUS * 2) * cosD(e) * sinD(lambda);
var z = (r * this.constants.EARTH_RADIUS * 2) * sinD(e) * sinD(lambda);
Community
  • 1
  • 1
Paolo
  • 15,233
  • 27
  • 70
  • 91
  • Thank you for your response. I edited my code with your suggestions but was still unable to get a correct result. I followed Radio's answer and was able to copy/paste something that works (although looks a lot more complex) so I decided to give up on what I have been doing wrong. Thanks though. – Nicolas Apr 29 '15 at 22:25
0

I cannot answer your question but I do know this is a solved problem in threejs. There is an example running in an architecture/engineering workflow on Github on this topic. The sun position code is here https://github.com/radio412/viewer/blob/gh-pages/sun-position.js

You can see it being tapped for a directional light in threejs at line 108 here: https://github.com/radio412/viewer/blob/gh-pages/va3c-viewer.js

Radio
  • 2,810
  • 1
  • 21
  • 43
  • Thank you for this - I have been trying to find something like this but haven't been able to. I have imported the relevant code and I have got something working. Sun seems to rotate around the Earth correctly at the correct speed. Now it's not well positioned but I'm thinking it's just my Earth texture that needs shifting. Thanks. – Nicolas Apr 29 '15 at 22:27