java - 为什么宇宙飞船没有出现?

标签 java 3d blender jmonkeyengine

我使用jmonkeyengine,从blendswap下载了一个宇宙飞船模型,并将其转换为j3o,以使用jmonkeyengine加载它,以实现我可以控制飞船并四处旅行的太空场景。然而,飞船并未装载。太空和行星出现了,但我希望宇宙飞船是玩家控制的,而不是像它看起来那样的第一人称。

enter image description here

我预计宇宙飞船会出现,因为我加载它时没有错误并将其添加到场景中,但它仍然没有显示。

//add saucer
ufoNode = (Node) assetManager
        .loadModel("usaucer_v01.j3o");      
rootNode.attachChild(ufoNode);

我该怎么做才能让飞船出现?程序是

public class PlanetSimpleTest extends SimpleApplication {

    private PlanetAppState planetAppState;
    private Geometry mark;
    private Node ufoNode;
    private GameCharControl ufoControl;
    Camera cam2;

    public static void main(String[] args){
        AppSettings settings = new AppSettings(true);
        settings.setResolution(1024,768);
        PlanetSimpleTest app = new PlanetSimpleTest();

        app.setSettings(settings);
        //app.showSettings = true;
        app.start();
    }

    @Override
    public void simpleInitApp() {
        // Only show severe errors in log
        java.util.logging.Logger.getLogger("com.jme3").setLevel(java.util.logging.Level.SEVERE);

        // Toggle mouse cursor
        inputManager.addMapping("TOGGLE_CURSOR", 
                new MouseButtonTrigger(MouseInput.BUTTON_LEFT),
                new KeyTrigger(KeyInput.KEY_SPACE));
        inputManager.addListener(actionListener, "TOGGLE_CURSOR"); 
        // Toggle wireframe
        inputManager.addMapping("TOGGLE_WIREFRAME", 
            new KeyTrigger(KeyInput.KEY_T));
        inputManager.addListener(actionListener, "TOGGLE_WIREFRAME");
        // Collision test
        inputManager.addMapping("COLLISION_TEST", 
            new MouseButtonTrigger(MouseInput.BUTTON_RIGHT));
        inputManager.addListener(actionListener, "COLLISION_TEST"); 

        // Setup camera

        // In orbit
        this.getCamera().setLocation(new Vector3f(0f, 0f, 180000f));

        // On surface
        //this.getCamera().setLocation(new Vector3f(-6657.5254f, 27401.822f, 57199.777f));
        //this.getCamera().lookAtDirection(new Vector3f(0.06276598f, 0.94458306f, -0.3222158f), Vector3f.UNIT_Y);

        // Add sun
        //PointLight sun = new PointLight();
        //sun.setPosition(new Vector3f(-100000f,0,180000f));
        DirectionalLight sun = new DirectionalLight();
        sun.setDirection(new Vector3f(-.1f, 0f, -1f));
        sun.setColor(new ColorRGBA(0.75f,0.75f,0.75f,1.0f));      
        rootNode.addLight(sun);

        // Add sky
        Node sceneNode = new Node("Scene");
        sceneNode.attachChild(Utility.createSkyBox(this.getAssetManager(), "Textures/blue-glow-1024.dds"));
        rootNode.attachChild(sceneNode);

        // Create collision test mark
        Sphere sphere = new Sphere(30, 30, 5f);
        mark = new Geometry("mark", sphere);
        Material mark_mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
        mark_mat.setColor("Color", ColorRGBA.Red);
        mark.setMaterial(mark_mat);

        // Add planet app state
        planetAppState = new PlanetAppState(rootNode, sun);
        stateManager.attach(planetAppState);

        // Add planet
        FractalDataSource planetDataSource = new FractalDataSource(4);
        planetDataSource.setHeightScale(900f);
        Planet planet = Utility.createEarthLikePlanet(getAssetManager(), 63710.0f, null, planetDataSource);
        planetAppState.addPlanet(planet);
        rootNode.attachChild(planet);

        // Add moon
        FractalDataSource moonDataSource = new FractalDataSource(5);
        moonDataSource.setHeightScale(300f);
        Planet moon = Utility.createMoonLikePlanet(getAssetManager(), 20000, moonDataSource);
        planetAppState.addPlanet(moon);
        rootNode.attachChild(moon);
        moon.setLocalTranslation(-150000f, 0f, 0f);

        //add saucer
        ufoNode = (Node) assetManager
                .loadModel("usaucer_v01.j3o");
        ufoNode.setLocalScale(100f);
        rootNode.attachChild(ufoNode);

    }

    @Override
    public void simpleUpdate(float tpf) {        
        // slow camera down as we approach a planet
        Planet planet = planetAppState.getNearestPlanet();
        if (planet != null && planet.getPlanetToCamera() != null) {
            this.getFlyByCamera().setMoveSpeed(
                    FastMath.clamp(planet.getDistanceToCamera(), 100, 100000));
        }     
    }

    private ActionListener actionListener = new ActionListener(){
        public void onAction(String name, boolean pressed, float tpf){     
            if (name.equals("TOGGLE_CURSOR") && !pressed) {
                if (inputManager.isCursorVisible()) {
                    inputManager.setCursorVisible(false);
                } else {
                    inputManager.setCursorVisible(true);
                }
            }
            if (name.equals("TOGGLE_WIREFRAME") && !pressed) {
                for (Planet planet: planetAppState.getPlanets()) {
                    planet.toogleWireframe();
                }
            }
            if (name.equals("COLLISION_TEST") && !pressed) {
                CollisionResults results = new CollisionResults();
                Ray ray = new Ray(cam.getLocation(), cam.getDirection());

                // Test collision with closest planet's terrain only
                planetAppState.getNearestPlanet().getTerrainNode().collideWith(ray, results);

                System.out.println("----- Collisions? " + results.size() + "-----");
                for (int i = 0; i < results.size(); i++) {
                  // For each hit, we know distance, impact point, name of geometry.
                  float dist = results.getCollision(i).getDistance();
                  Vector3f pt = results.getCollision(i).getContactPoint();
                  String hit = results.getCollision(i).getGeometry().getName();
                  System.out.println("* Collision #" + i);
                  System.out.println("  You shot " + hit + " at " + pt + ", " + dist + " wu away.");
                }

                if (results.size() > 0) {
                  // The closest collision point is what was truly hit:
                  CollisionResult closest = results.getClosestCollision();
                  // Let's interact - we mark the hit with a red dot.
                  mark.setLocalTranslation(closest.getContactPoint());
                  rootNode.attachChild(mark);
                } else {
                  // No hits? Then remove the red mark.
                  rootNode.detachChild(mark);
                }
            }  
        }
    }; 

}

我从here拿走的飞船并转换为 jme3:s 二进制格式 j3o 并添加到游戏中,但我显然没有尽一切努力使其出现在场景中。我已经在 jmonkeyengine IDE 中做到了这一点,但是当我在 Eclipse 中加载它时它不起作用,所以我尝试首先使用 jmonkeyengine IDE 创建场景。

enter image description here

然后,我尝试创建一个包含宇宙飞船的场景,但在 JME SDK 中出现异常。

enter image description here

添加行后

ufoNode.setLocalScale(300f);
ufoNode.setLocalTranslation((new Vector3f(10f, 10f, 180010f)));

宇宙飞船确实出现了,但可能并不完美。可以改进吗?

enter image description here

enter image description here

更新 140104 17:54 CET

看起来宇宙飞船是颠倒的(?)所以我现在把它旋转了。

enter image description here

//add saucer
ufoNode = (Node) assetManager.loadModel("usaucer_v01.j3o");
ufoNode.setLocalScale(1000f);
ufoNode.setLocalTranslation((new Vector3f(10f, 10f, 165000f)));

/* This quaternion stores a 180 degree rolling rotation */ 
Quaternion roll180 = new Quaternion(); 
roll180.fromAngleAxis(FastMath.PI , new Vector3f(0,0,1)); 
/* The rotation is applied: The object rolls by 180 degrees. */ 
ufoNode.setLocalRotation(roll180);      
rootNode.attachChild(ufoNode);  

最佳答案

您将宇宙飞船添加到 0,0,0(因为您没有移动它),但已将相机移动到 0f, 0f, 180000f。最有可能的是它不在镜头之外或者太小而看不见。

尝试在 jME SDK 中加载宇宙飞船,并使用那里的场景编辑器确认它在引擎中工作。

如果有效,请尝试一个简单的测试场景,只需将宇宙飞船和相机放入 - 然后一旦有效,一次向您想要的设置移动一步。

关于java - 为什么宇宙飞船没有出现?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20922219/

相关文章:

java - 当 child 被 orphanRemoval 删除时,EntityListener 不起作用

c++ - 连续内存、OpenGL,以及如何包装顶点?

javascript - 如何在 webgl 中从前/左/上向量创建投影矩阵

three.js - 如何在 Blender for Three.js 中对顶点进行分组?

Java7 笛卡尔积版本没有重复项

java - LibGDX 中的多个敌人阵列

java - 在MySQL中添加java日期时出现问题?

python - 树莓派上的高效 3d

python - 如何在 blender python 中获取选定顶点的排序数组

python - 如何使用 Python 在 Blender 2.6 中设置事件相机后重新渲染场景