Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
DebugDraw class in JBox2D
#1
Here's my code:
Code:
package test;

import org.jbox2d.callbacks.*;
import org.jbox2d.collision.shapes.*;
import org.jbox2d.common.*;
import org.jbox2d.dynamics.*;

public class Main {
    private static DebugDraw debugDraw;
    
    public static DebugDraw getDebugDraw() {
        return debugDraw;
    }
    public static void main(String[] args) {
        Vec2  gravity = new Vec2(0,-10);
    boolean doSleep = true;
    World world = new World(gravity,doSleep);
    BodyDef groundBodyDef = new BodyDef();
    groundBodyDef.position.set(0, -10);
    Body groundBody = world.createBody(groundBodyDef);
    PolygonShape groundBox = new PolygonShape();
    groundBox.setAsBox(50,10);
    groundBody.createFixture(groundBox, 0);

    // Dynamic Body
    BodyDef bodyDef = new BodyDef();
    bodyDef.type = BodyType.DYNAMIC;
    bodyDef.position.set(0, 4);
    Body body = world.createBody(bodyDef);
    PolygonShape dynamicBox = new PolygonShape();
    dynamicBox.setAsBox(1, 1);
    FixtureDef fixtureDef = new FixtureDef();
    fixtureDef.shape = dynamicBox;
    fixtureDef.density=1;
    fixtureDef.friction=0.3f;
    body.createFixture(fixtureDef);

    // Setup world
    float timeStep = 1.0f/60.0f;
    int velocityIterations = 6;
    int positionIterations = 2;

    // Run loop
    for (int i = 0; i < 60; ++i)
    {
        world.step(timeStep, velocityIterations, positionIterations);
        Vec2 position = body.getPosition();
        float angle = body.getAngle();
        debugDraw.setFlags(debugDraw.e_shapeBit);
        world.setDebugDraw(debugDraw);
        System.out.println(i+": X: "+position.x+" Y: "+position.y+" ANGLE: "+angle);
    }

    }
    }

And here's the ouput:
Code:
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
Exception in thread "main" java.lang.NullPointerException
    at test.Main.main(Main.java:49)
Java Result: 1

I'm not entirely sure what is causing the NPE. Can anyone help?
Btw, this is just a test, not a actual game.
EDIT: Line 49 is "debugDraw.setFlags(debugDraw.e_shapeBit);"
Reply
#2
Either debugDraw.setFlags() or debugDraw.e_shapeBit is probably missing if it's an NPE.
Reply
#3
@Qwertygiy's deleted (why?) post:

Nope, I checked the DebugDraw class. It exists.
(07-14-2012, 12:59 AM)Qwertygiy Wrote: Either debugDraw.setFlags() or debugDraw.e_shapeBit is probably missing if it's an NPE.

Nope, both exist.
Reply
#4
OH, I bet I know your problem.

"private static DebugDraw debugDraw;"

You never defined debugDraw.

Try

"private static DebugDraw debugDraw = new DebugDraw();"
Reply
#5
Interesting, I looked at JBox2D sources and the numbers for e_shapeBit were entirely diferent.

Letme try something.
(07-14-2012, 01:04 AM)Qwertygiy Wrote: OH, I bet I know your problem.

"private static DebugDraw debugDraw;"

You never defined debugDraw.

Try

"private static DebugDraw debugDraw = new DebugDraw();"

I can't, DebugDraw is abstract.
Reply
#6
I'm pretty sure it still is "null" when you call it on line 49.

A few ideas that I'm not sure are any good:

a) Instead of defining it as a variable, try calling it straight from the class.

b) Try implementing DebugDraw in a new class and using that instead.
Reply
#7
a) Its not static, so it can't be done.

b) What do you mean by that?
Reply
#8
Let me take a Minecraft example.

IInventory:
Code:
package net.minecraft.src;

public interface IInventory
{
    /**
     * Returns the number of slots in the inventory.
     */
    int getSizeInventory();

    /**
     * Returns the stack in slot i
     */
    ItemStack getStackInSlot(int var1);

    /**
     * Decrease the size of the stack in slot (first int arg) by the amount of the second int arg. Returns the new
     * stack.
     */
    ItemStack decrStackSize(int var1, int var2);

    /**
     * When some containers are closed they call this on each slot, then drop whatever it returns as an EntityItem -
     * like when you close a workbench GUI.
     */
    ItemStack getStackInSlotOnClosing(int var1);

    /**
     * Sets the given item stack to the specified slot in the inventory (can be crafting or armor sections).
     */
    void setInventorySlotContents(int var1, ItemStack var2);

    /**
     * Returns the name of the inventory.
     */
    String getInvName();

    /**
     * Returns the maximum stack size for a inventory slot. Seems to always be 64, possibly will be extended.
     */
    int getInventoryStackLimit();

    /**
     * Called when an the contents of an Inventory change, usually
     */
    void onInventoryChanged();

    boolean isUseableByPlayer(EntityPlayer var1);

    void openChest();

    void closeChest();
}


InventoryBasic:
Code:
package net.minecraft.src;

import java.util.List;

public class InventoryBasic implements IInventory
{
    private String inventoryTitle;
    private int slotsCount;
    private ItemStack[] inventoryContents;
    private List field_20073_d;

    public InventoryBasic(String par1Str, int par2)
    {
        this.inventoryTitle = par1Str;
        this.slotsCount = par2;
        this.inventoryContents = new ItemStack[par2];
    }

    /**
     * Returns the stack in slot i
     */
    public ItemStack getStackInSlot(int par1)
    {
        return this.inventoryContents[par1];
    }

    /**
     * Decrease the size of the stack in slot (first int arg) by the amount of the second int arg. Returns the new
     * stack.
     */
    public ItemStack decrStackSize(int par1, int par2)
    {
        if (this.inventoryContents[par1] != null)
        {
            ItemStack var3;

            if (this.inventoryContents[par1].stackSize <= par2)
            {
                var3 = this.inventoryContents[par1];
                this.inventoryContents[par1] = null;
                this.onInventoryChanged();
                return var3;
            }
            else
            {
                var3 = this.inventoryContents[par1].splitStack(par2);

                if (this.inventoryContents[par1].stackSize == 0)
                {
                    this.inventoryContents[par1] = null;
                }

                this.onInventoryChanged();
                return var3;
            }
        }
        else
        {
            return null;
        }
    }

    /**
     * When some containers are closed they call this on each slot, then drop whatever it returns as an EntityItem -
     * like when you close a workbench GUI.
     */
    public ItemStack getStackInSlotOnClosing(int par1)
    {
        if (this.inventoryContents[par1] != null)
        {
            ItemStack var2 = this.inventoryContents[par1];
            this.inventoryContents[par1] = null;
            return var2;
        }
        else
        {
            return null;
        }
    }

    /**
     * Sets the given item stack to the specified slot in the inventory (can be crafting or armor sections).
     */
    public void setInventorySlotContents(int par1, ItemStack par2ItemStack)
    {
        this.inventoryContents[par1] = par2ItemStack;

        if (par2ItemStack != null && par2ItemStack.stackSize > this.getInventoryStackLimit())
        {
            par2ItemStack.stackSize = this.getInventoryStackLimit();
        }

        this.onInventoryChanged();
    }

    /**
     * Returns the number of slots in the inventory.
     */
    public int getSizeInventory()
    {
        return this.slotsCount;
    }

    /**
     * Returns the name of the inventory.
     */
    public String getInvName()
    {
        return this.inventoryTitle;
    }

    /**
     * Returns the maximum stack size for a inventory slot. Seems to always be 64, possibly will be extended.
     */
    public int getInventoryStackLimit()
    {
        return 64;
    }

    /**
     * Called when an the contents of an Inventory change, usually
     */
    public void onInventoryChanged()
    {
    }

    /**
     * Do not make give this method the name canInteractWith because it clashes with Container
     */
    public boolean isUseableByPlayer(EntityPlayer par1EntityPlayer)
    {
        return true;
    }

    public void openChest() {}

    public void closeChest() {}
}
Reply
#9
UPDATE: It appears to be the setFlags() method that is causing this. Sad
(07-14-2012, 01:30 AM)Qwertygiy Wrote: Let me take a Minecraft example.

IInventory:
Code:
package net.minecraft.src;

public interface IInventory
{
    /**
     * Returns the number of slots in the inventory.
     */
    int getSizeInventory();

    /**
     * Returns the stack in slot i
     */
    ItemStack getStackInSlot(int var1);

    /**
     * Decrease the size of the stack in slot (first int arg) by the amount of the second int arg. Returns the new
     * stack.
     */
    ItemStack decrStackSize(int var1, int var2);

    /**
     * When some containers are closed they call this on each slot, then drop whatever it returns as an EntityItem -
     * like when you close a workbench GUI.
     */
    ItemStack getStackInSlotOnClosing(int var1);

    /**
     * Sets the given item stack to the specified slot in the inventory (can be crafting or armor sections).
     */
    void setInventorySlotContents(int var1, ItemStack var2);

    /**
     * Returns the name of the inventory.
     */
    String getInvName();

    /**
     * Returns the maximum stack size for a inventory slot. Seems to always be 64, possibly will be extended.
     */
    int getInventoryStackLimit();

    /**
     * Called when an the contents of an Inventory change, usually
     */
    void onInventoryChanged();

    boolean isUseableByPlayer(EntityPlayer var1);

    void openChest();

    void closeChest();
}


InventoryBasic:
Code:
package net.minecraft.src;

import java.util.List;

public class InventoryBasic implements IInventory
{
    private String inventoryTitle;
    private int slotsCount;
    private ItemStack[] inventoryContents;
    private List field_20073_d;

    public InventoryBasic(String par1Str, int par2)
    {
        this.inventoryTitle = par1Str;
        this.slotsCount = par2;
        this.inventoryContents = new ItemStack[par2];
    }

    /**
     * Returns the stack in slot i
     */
    public ItemStack getStackInSlot(int par1)
    {
        return this.inventoryContents[par1];
    }

    /**
     * Decrease the size of the stack in slot (first int arg) by the amount of the second int arg. Returns the new
     * stack.
     */
    public ItemStack decrStackSize(int par1, int par2)
    {
        if (this.inventoryContents[par1] != null)
        {
            ItemStack var3;

            if (this.inventoryContents[par1].stackSize <= par2)
            {
                var3 = this.inventoryContents[par1];
                this.inventoryContents[par1] = null;
                this.onInventoryChanged();
                return var3;
            }
            else
            {
                var3 = this.inventoryContents[par1].splitStack(par2);

                if (this.inventoryContents[par1].stackSize == 0)
                {
                    this.inventoryContents[par1] = null;
                }

                this.onInventoryChanged();
                return var3;
            }
        }
        else
        {
            return null;
        }
    }

    /**
     * When some containers are closed they call this on each slot, then drop whatever it returns as an EntityItem -
     * like when you close a workbench GUI.
     */
    public ItemStack getStackInSlotOnClosing(int par1)
    {
        if (this.inventoryContents[par1] != null)
        {
            ItemStack var2 = this.inventoryContents[par1];
            this.inventoryContents[par1] = null;
            return var2;
        }
        else
        {
            return null;
        }
    }

    /**
     * Sets the given item stack to the specified slot in the inventory (can be crafting or armor sections).
     */
    public void setInventorySlotContents(int par1, ItemStack par2ItemStack)
    {
        this.inventoryContents[par1] = par2ItemStack;

        if (par2ItemStack != null && par2ItemStack.stackSize > this.getInventoryStackLimit())
        {
            par2ItemStack.stackSize = this.getInventoryStackLimit();
        }

        this.onInventoryChanged();
    }

    /**
     * Returns the number of slots in the inventory.
     */
    public int getSizeInventory()
    {
        return this.slotsCount;
    }

    /**
     * Returns the name of the inventory.
     */
    public String getInvName()
    {
        return this.inventoryTitle;
    }

    /**
     * Returns the maximum stack size for a inventory slot. Seems to always be 64, possibly will be extended.
     */
    public int getInventoryStackLimit()
    {
        return 64;
    }

    /**
     * Called when an the contents of an Inventory change, usually
     */
    public void onInventoryChanged()
    {
    }

    /**
     * Do not make give this method the name canInteractWith because it clashes with Container
     */
    public boolean isUseableByPlayer(EntityPlayer par1EntityPlayer)
    {
        return true;
    }

    public void openChest() {}

    public void closeChest() {}
}

I see what you mean, but I'm not quite sure how to define the methods of it. I'll try though.
Reply
#10
Random idea, try swapping these two lines:

debugDraw.setFlags(debugDraw.e_shapeBit);
world.setDebugDraw(debugDraw);
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)