Game Development Reference
In-Depth Information
float y = FastMath.sin(time ) * 5f;
position.set(x, y, z);
4. Then, we should set localTranslation of the sun. Since we want it to ap-
pear to be very far away, we add the camera's location. This way it will always
appear to be the same distance from the camera:
spatial.setLocalTranslation((cam.getLocation().add(position)));
5. We also want the sun to always face the camera. This is easily done by calling the
following code:
spatial.lookAt(cam.getLocation(), Vector3f.UNIT_Y);
6. If the directionalLight field is set, we should also set its direction. We get
the direction by inverting position , as shown in the following code:
directionalLight.setDirection(position.negate());
7. Finally, we increase the time value by a factor of tpf (depending on how fast
we want the sun to move). Since two PI in radians make up a circle, we start over
once time exceeds that value, using the following code:
time += tpf * timeFactor;
time = time % FastMath.TWO_PI;
8. Going back to the application class, we add the control to the Geometry
sun and Geometry to the scene graph:
sun.addControl(sunControl);
rootNode.attachChild(sun);
The previous implementation can be enough for many games but it can be taken much
further. Let's explore how to make the sun color dynamic based on its height above the
horizon and how to also have a dynamic sky color by performing the following steps:
1. First of all, let's introduce two static ColorRGBA fields in the SunControl
class called dayColor and eveningColor . We also add another Color-
RGBA field called sunColor .
2. In the controlUpdate method, we take the y value of the sun and divide it so
that we get a value between -1 and 1 , and store this as the height.
Search WWH ::




Custom Search