0

I have a small pieace of code that creates a ModelInstance based on the model passed to it etc (Please excuse all unused variables such as w,h,d, they were from a previous test)

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;
    }
}

However, whenever it gets run I receive this error:

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)

It doesn't seem to be able to Calculate the bounding box of the ModelInstance I specify. Mabye I'm just doing something wrong, any advice would be appreciated as to how to use the calculateBoundingBox() method

donfuxx
  • 11,277
  • 6
  • 44
  • 76

1 Answers1

0

See the source code of the method calculateBoundingBox (libgdx is open-source, you can review all the code ;-):

/** 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);
}

The stacktrace complained about a NullPointerException at line 383: out.inf(); At this point we know that out must point to null.

Now let's see why is that: check the line shapeInst.calculateBoundingBox(bounds); The parameter bounds is null, because you forgot to initialize it. (you just declared bounds, but never asign a value to it).

donfuxx
  • 11,277
  • 6
  • 44
  • 76
  • I must be missinterpretting the calculateBoundingBox() then, i thought it was going to return the boundingbox into my bounds variable. How would I go about getting the boundingbox of my model and putting that value into bounds? Or do I have to specify a bounding box for my model myself and offset it by the model's new position? Also thanks for the reply :D – user3408357 Aug 09 '14 at 06:56
  • It actually does return the bounding box that you have passed in as a parameter after calling some method on it and doing some calculations. It is just that this method doesn't like to get null passed as a param. Pass an initialized BoundingBox as parameter instead. – donfuxx Aug 09 '14 at 07:10
  • i dont think im following. Lets say I have a modelInstance located at 3,3,3. if I pass the calculateBoundingBox() a bounding box from 0,0,0 to 1,1,1. what would I get back? Im trying to get the bounding box of a model thats in a translated location so I can check for collision. – user3408357 Aug 09 '14 at 07:19
  • Initializing your bounding box should help: `bounds = new BoundingBox();` Anyhow, I have seen a blog post where there are some examples using that method: http://blog.xoppa.com/3d-frustum-culling-with-libgdx/ – donfuxx Aug 09 '14 at 07:38