Move the directionalLight to get a more realistic result

Hi !

I’m new on jmonkeyengine development and I’m creating an augmented reality Android app.
So, 3D objects are displayed and move depending on their own position and phone position (gps + sensor).

I want to move directionalLight (sun) to get a realistic result.
For example, if phone date is 2pm, the sun is up, if it 5pm the sun is to the west, etc.
So I need to take care of phone date and phone compass.

I already got those two parameters, but I need a few help to set sun position.

Thanks,
Florian.

Search these forums for something called “SkyControl” - I think it might be able to do the math of calculating the sun postion.

I’m not clear why the compass matters for the sun position.

Compass is usefull to get the north and show sun in the right position

But it’s a 3D world or a 2D world?

Maybe we need more info because it sounds like your app is not really 3D.

It’s a 3D world (3D objects layer over Android camera in 2D).
Maybe I took me the wrong way ?
In order to set the light position, I need to know where is the east right ?

The sun position (and the direcitonal light) is set in the 3D-world coordinate system. The compass is used to set where the camera is pointing (so nothing to do with the sun position). As pspeed suggests the compass is irrelevant to setting the sun position.
I though OP was asking for math to calculate the sun position given a lat/long and datetime.

East is in +x direction. North is in -z direction, source is in +z direction, west is in -x direction.

y is up.

Direction of the sun is in the x,y plane based on time of day relative to 6 AM or whatever. (Depending on how accurate you want to be.)

float angleDegrees = timeInHours - 6;
float angleRads = FastMath.DEG_TO_RAD * angleDegrees;
float xSun = FastMath.cos(angleRads);
float ySun = FastMath.sin(angleRads);

Vector3f sunDirection = new Vector3f(-xSun, -ySun, 0);

…basic trig.

1 Like

I think it’s not so easy.
Because I’m in an AR context, east position depend on phone position…
If the phone is to the north then east is to the left.
If the phone is to the west then east is behind.

No… camera direction depends on phone direction. You make your life 100000x harder if the whole world must be reoriented to your phone.

1 Like

Ok thank you.
In your example, if I replace timeInHours by 14 for 2pm, or any other hour, the result is the same : light is on the right.

I forgot to convert time to degrees…

float angleDegrees = (timeInHours - 6) * 30;

…not to be critical, but if this simple math is the math that trips you up then you are going to have a bad time, I think.