Rising Sun

Im trying to get a sun rising and lowering as the game day progresses, but I’m having some problems.

[java] float multiplier = (float)Math.sin(Math.PI * _dayTimeManager.getGameDayTotalSeconds() / (24 * 60 * 60));

    _app.getViewPort().setBackgroundColor(new ColorRGBA(_baseColor.r * multiplier,
                                                _baseColor.g * multiplier,
                                                _baseColor.b * multiplier,
                                                1.0f));
    
    _deltaSeconds = _dayTimeManager.getGameDayTotalSeconds() - _deltaSeconds;
    
    float degreesPerSeconds = (float)360 / (24 * 60 * 60);

    _sunBaseNode.rotate((float)Math.toRadians(_deltaSeconds * degreesPerSeconds), 0.0f, 0.0f);[/java]

The “sky” color works just fine, it’s the rotation of the sun that doesn’t work.

_dayTimeManager.getGameDayTotalSeconds() gets the passed seconds of the current in-game day.

The _sunBaseNode looks like this:
[java] Sphere sphere = new Sphere(64, 64, 50.0f);
Geometry geom = new Geometry(“SunSphere”, sphere);
Material mat = new Material(_app.getAssetManager(), “Common/MatDefs/Misc/Unshaded.j3md”);
mat.setColor(“Color”, ColorRGBA.Yellow);
geom.setMaterial(mat);
geom.setCullHint(CullHint.Never);
//_app.getRootNode().attachChild(geom);

    _sunBaseNode = new Node();
    _sunBaseNode.attachChild(geom);
    geom.setLocalTranslation(0.0f, 300.0f, 0.0f);
    _app.getRootNode().attachChild(_sunBaseNode);[/java]

The trouble I’m having is making the sun rotate at the correct speed. As it is now, it rotates hundreds of times per “day”.

1 Like

rotate() rotates relative to the previous rotation. So if the first time, you rotate 1 degree and the next time you rotate 2 degrees then you have really rotated 3 degrees.

If you want to deal with absolute rotations then do not use rotate(). Use setLocalRotation() and construct the appropriate quaternion.

But that’s why I calculate the in-game seconds passed since last rotation. Or am I thinking completely wrong?

I noticed that my _deltaSeconds is broken. I will try to fix that and get back.

Seems to me that setLocalRotation(new Quaternion().fromAngles(sunAngle, 0,0)) would save a whole bunch of unnecessary math and book-keeping.

Any idea why the sky is bright for much longer than it is dark?

PI instead of 2 * PI? Looks like you are only running through half the circle… but I guess that doesn’t explain why one is longer than the other.

Oh, yeah, sin()… since it never goes negative in 0 - PI and it doesn’t spend very long at 0 either, that’s why it’s not even.

Switch to 2 PI and deal with the negative. multiplier = (multiplier + 1) / 2;

<cite>@pspeed said:</cite> Oh, yeah, sin()... since it never goes negative in 0 - PI and it doesn't spend very long at 0 either, that's why it's not even.

Switch to 2 PI and deal with the negative. multiplier = (multiplier + 1) / 2;

Aah, thank you.

[java] double degreesPerDay = 360.0 / (24 * 60 * 60);
double passedDays = (double)_dayTimeManager.getGameDayTotalSeconds() / (24 * 60 * 60);

    _sunBaseNode.setLocalRotation((new Quaternion()).fromAngles((float)Math.toRadians(passedDays * degreesPerDay), 0.0f, 0.0f));[/java]

That is the code for “rotating” my sun. The sun doesn’t move at all though.
I printed the two variables and they seem fine. Any idea why this happends?

Nevermind. The degreesPerDay thing doesn’t make any sense at all… :stuck_out_tongue_winking_eye:

I added a DirectionalLight to act as the suns light. But, I can’t figure out how to calculate the lights direction from the suns current position.
Seems like i need to sharpen up my math a bit.

Yes, probably.

Vector3f dir = position.normalize().negate();

In other words, you want it to point in the opposite direction of its position and also for it to be a normalized direction vector.