Hello,
I think, I found a strange bug with DirectionalLight:
History:
I discovered HelloAnimation.java within the old starter guide that demonstrates a pointlight, wich rotates around a sphere.
I now wanted to use a DirectionalLight instead of a PointLight.
It should do the following:
Rotate around the Z-Axis and light a sphere that is situated at (0,0,0), just like the PointLight in HelloAnimation.java.
Ok, I tried it straight forward:
import com.jme.app.SimpleGame;
import com.jme.scene.shape.Sphere;
import com.jme.scene.Node;
import com.jme.math.Vector3f;
import com.jme.math.Quaternion;
import com.jme.math.FastMath;
import com.jme.animation.SpatialTransformer;
import com.jme.light.SimpleLightNode;
import com.jme.renderer.ColorRGBA;
import com.jme.scene.state.MaterialState;
import com.jme.light.DirectionalLight;
/**
* This class is adapted from the HelloAnimation.java from Jack Lindamood.
*/
public class PlanetAnimation2 extends SimpleGame {
public static void main(String[] args) {
PlanetAnimation2 app = new PlanetAnimation2();
app.setDialogBehaviour(SimpleGame.ALWAYS_SHOW_PROPS_DIALOG);
app.start();
}
protected void simpleInitGame() {
Sphere p = new Sphere("My sphere",30,30,5);
MaterialState planetMaterial = display.getRenderer().createMaterialState();
planetMaterial.setAmbient(new ColorRGBA(0.15f,0.25f,0.05f,1.0f));
planetMaterial.setDiffuse(new ColorRGBA(0.3f, 0.7f, 0.1f, 1.0f));
p.setRenderState(planetMaterial);
p.updateRenderState();
//I will rotate this pivot to move my light
Node pivot = new Node("Pivot node for sun sphere");
//This light will rotate around my sphere. Notice I don't
//give it a position
//PointLight pl=new PointLight();
DirectionalLight pl=new DirectionalLight();
//Color the light white
pl.setDiffuse(ColorRGBA.white);
//Enable the light
pl.setEnabled(true);
pl.setDirection(new Vector3f(0.0f, 0.0f, 0.0f));
//Remove the default light and attach this one
lightState.setEnabled(true);
lightState.detachAll();
lightState.attach(pl);
//This node will hold my light
SimpleLightNode ln=
new SimpleLightNode("A node for my light",pl);
ln.setLocalTranslation(new Vector3f(0.0f, 80.0f, 0.0f));
ln.updateRenderState();
//I set the light's position thru the node
pivot.setLocalTranslation(new Vector3f(0,0,0));
//I attach the light's node to my pivot
pivot.attachChild(ln);
SpatialTransformer st = new SpatialTransformer(1);
st.setObject(ln, 0, -1);
//Assign a rotation for object 0 at time 0 to rotate 0
//degrees around the z axis
Quaternion x0=new Quaternion();
x0.fromAngleAxis(0,new Vector3f(0,0,1));
st.setRotation(0,0,x0);
//Assign a rotation for object 0 at time 2 to rotate 180
//degrees around the z axis
Quaternion x180=new Quaternion();
x180.fromAngleAxis(FastMath.DEG_TO_RAD*180,new Vector3f(0,0,1));
st.setRotation(0,10,x180);
//Assign a rotation for object 0 at time 4 to rotate 360
//degrees around the z axis
Quaternion x360=new Quaternion();
x360.fromAngleAxis(FastMath.DEG_TO_RAD*360,new Vector3f(0,0,1));
st.setRotation(0,20,x360);
//Prepare my controller to start moving around
st.interpolateMissing();
//Tell my pivot it is controlled by st
pivot.addController(st);
rootNode.attachChild(pivot);
rootNode.attachChild(p);
rootNode.updateRenderState();
}
}
This did not work. But than ... I changed the quaternion-vector from (0,0,1) to (0,1,0) - wich should rotate the directional light around the Y-axis.
This did work !!!!!
And I tried (1,0,0) in the quaternion vector (Rotation around X-Axis) like that:
//Assign a rotation for object 0 at time 0 to rotate 0
//degrees around the z axis
Quaternion x0=new Quaternion();
x0.fromAngleAxis(0,new Vector3f(1,0,0));
st.setRotation(0,0,x0);
//Assign a rotation for object 0 at time 2 to rotate 180
//degrees around the z axis
Quaternion x180=new Quaternion();
x180.fromAngleAxis(FastMath.DEG_TO_RAD*180,new Vector3f(1,0,0));
st.setRotation(0,10,x180);
//Assign a rotation for object 0 at time 4 to rotate 360
//degrees around the z axis
Quaternion x360=new Quaternion();
x360.fromAngleAxis(FastMath.DEG_TO_RAD*360,new Vector3f(1,0,0));
st.setRotation(0,20,x360);
This also worked !!!!!
But why ... why only ... does it not work with (0,0,1) ?
I tried out a lot of things, but every time it did rotate correctly with (0,1,0) and (1,0,0) but not (0,0,1).
It seems that only the projected portion relative to the X-Y-plane is used for the rotation of the directional light.
Then I discovered DirectionalLight.setDirection(), but that didn't work for my purpose.
(Or I didn't use it correctly)
Can you tell me, how to do this without using a point light at "unlimited distance" ?
Ogli