[SOLVED] Sky sphere rotates the wrong direction

If cube-mapped skies are subject to rotation, it would be nice if rotation affected them
the same way it affects other geometries. In the case of rotation around the Y-axis,
the effect appears to be backward.

In the following test code, I apply the same rotation to the floor and the sky
(by pressing the “T” or “Y” key) and see them turn in opposite directions.
Why is the sky rotating the wrong way?

package mygame;

import com.jme3.app.SimpleApplication;
import com.jme3.input.KeyInput;
import com.jme3.input.controls.ActionListener;
import com.jme3.input.controls.KeyTrigger;
import com.jme3.material.Material;
import com.jme3.math.ColorRGBA;
import com.jme3.math.Quaternion;
import com.jme3.math.Vector3f;
import com.jme3.scene.Geometry;
import com.jme3.scene.Spatial;
import com.jme3.scene.shape.Box;
import com.jme3.system.JmeVersion;
import com.jme3.util.SkyFactory;

public class Main extends SimpleApplication implements ActionListener {
    Spatial floor, sky;
    float angle;
    public static void main(String[] arguments) {
        Main application = new Main();
        application.start();
    }
    @Override
    public void simpleInitApp() {
        System.out.printf("jME3-core version is %s%n", JmeVersion.FULL_NAME);
        cam.setLocation(new Vector3f(-7f, 4f, 8f));
        cam.setRotation(new Quaternion(0.0036812495f, 0.944684f, -0.010609083f, 0.3277894f));

        sky = SkyFactory.createSky(assetManager,
                "Scenes/Beach/FullskiesSunset0068.dds",
                SkyFactory.EnvMapType.CubeMap);
        rootNode.attachChild(sky);

        Box b = new Box(10f, 0.1f, 10f);
        floor = new Geometry("floor", b);
        Material mat = new Material(assetManager, 
                "Common/MatDefs/Misc/Unshaded.j3md");
        mat.setColor("Color", ColorRGBA.White);
        mat.setTexture("ColorMap", 
                assetManager.loadTexture("Interface/Logo/Monkey.jpg"));
        floor.setMaterial(mat);
        rootNode.attachChild(floor);

        angle = 0f;

        inputManager.addMapping("left", new KeyTrigger(KeyInput.KEY_T));
        inputManager.addMapping("right", new KeyTrigger(KeyInput.KEY_Y));
        inputManager.addListener(this, "left");
        inputManager.addListener(this, "right");
    }

    @Override
    public void onAction(String name, boolean ongoing, float ignored) {
        if (!ongoing) {
            return;
        }
        if (name.equals("left")) {
            angle += 0.1f;
            System.out.print("rotate both spatials left");
        } else if (name.equals("right")) {
            angle -= 0.1f;
            System.out.printf("rotate both spatials right");
        } else {
            return;
        }
        
        System.out.printf(" to %f radians from start%n", angle);
        Quaternion rotation = new Quaternion();
        rotation.fromAngleNormalAxis(angle, Vector3f.UNIT_Y);
        floor.setLocalRotation(rotation);
        sky.setLocalRotation(rotation);
    }
}

I forgot to mention, I was seeing this on 3.1-beta1. I also tested on 3.0.10 and saw the same unexpected behavior.
(To test with 3.0.10, change the 3rd argument of SkyFactory.createSky() to false.)

This is probably because the sky is drawn on the back faces, so it is rotating correctly, but you are seeing the inverse since you are looking at the back/inside of the box.

Have you run the test code yet?

I opened an issue at GitHub: rotation is inverted for skies that use Sky.vert · Issue #651 · jMonkeyEngine/jmonkeyengine · GitHub

Fixed in ‘master’ branch on GitHub.

https://github.com/jMonkeyEngine/jmonkeyengine/commit/4b4cf62008f33fe933ec3851c6b20d3b2c694b86

3 Likes