0

渡されたモデルに基づいて ModelInstance を作成する小さなコードがあります (w、h、d などの未使用の変数はすべて、以前のテストのものでした)

package com.mygdx.game;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.assets.loaders.ModelLoader;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.g3d.Model;
import com.badlogic.gdx.graphics.g3d.ModelInstance;
import com.badlogic.gdx.graphics.g3d.attributes.ColorAttribute;
import com.badlogic.gdx.graphics.g3d.loader.ObjLoader;
import com.badlogic.gdx.math.Vector3;
import com.badlogic.gdx.math.collision.BoundingBox;

public class Shape {
    float w,h,d;
    Color clr;
    Vector3 pos;
    Model shape;
    ModelInstance shapeInst;
    BoundingBox bounds;
    boolean empty;
    public Shape(float width, float height, float depth, Color color, Vector3 position, String model){
        empty = false;
        @SuppressWarnings("rawtypes")
        ModelLoader loader = new ObjLoader();
        shape = loader.loadModel(Gdx.files.internal(model));
        w = width;
        h = height;
        d = depth;
        clr = color;
        pos = position;
        shapeInst = new ModelInstance(shape);
        shapeInst.materials.get(0).set(ColorAttribute.createDiffuse(clr));
        shapeInst.transform.setToTranslation(pos);
        shapeInst.calculateBoundingBox(bounds);
    }
    public Shape(){
        empty = true;
    }
}

ただし、実行されるたびに、次のエラーが表示されます。

Exception in thread "LWJGL Application" java.lang.NullPointerException
    at com.badlogic.gdx.graphics.g3d.ModelInstance.calculateBoundingBox(ModelInstance.java:383)
    at com.mygdx.game.Shape.<init>(Shape.java:37)
    at com.mygdx.game.worldRenderer.<init>(worldRenderer.java:62)
    at com.mygdx.game.GDXGame.create(GDXGame.java:92)
    at com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.java:136)
    at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:114)

指定したModelInstanceのバウンディングボックスを計算できないようです。Mabye私は何か間違ったことをしているだけです。メソッドの使用方法についてアドバイスをいただければ幸いcalculateBoundingBox()です

4

1 に答える 1

0

メソッドcalculateBoundingBoxのソース コードを参照してください(libgdx はオープン ソースです。すべてのコードを確認できます ;-):

/** Calculate the bounding box of this model instance. This is a potential slow operation, it is advised to cache the result.
 * @param out the {@link BoundingBox} that will be set with the bounds.
 * @return the out parameter for chaining */
public BoundingBox calculateBoundingBox (final BoundingBox out) {
    out.inf(); // here is line 383 !
    return extendBoundingBox(out);
}

スタック トレースは、383 行目で NullPointerException について不平を言いました。この時点で、それが null を指している必要out.inf();があることがわかります。out

その理由を見てみましょう: shapeInst.calculateBoundingBox(bounds);The parameter bounds is null という行を確認してください。これは、初期化を忘れたためです。(境界を宣言しただけで、値を割り当てないでください)。

于 2014-08-09T06:49:06.447 に答える