[LibGdx] box2d debug renderer problem

Discuss issues specific the Java port of Box2D
Posts: 2
Joined: Mon Sep 08, 2014 2:05 am

[LibGdx] box2d debug renderer problem

Postby mustache » Mon Sep 08, 2014 2:10 am

Hello All

My code is based off of this Website (Scroll down for video):
http://programmersweb.blogspot.ch/2012/ ... -ball.html
This code works no problem, i can see the ball bouncing off the ground. I have one single "Game" class

My next Step was to take the code out of my "Game" class and divide it into a "Game" class and a "Screen" class.
The code was changed slightly because there was an issue with the way World world was initialized but otherwise it is the same code.

Code: Select all

package com.me.blast.screens;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.physics.box2d.Body;
import com.badlogic.gdx.physics.box2d.BodyDef;
import com.badlogic.gdx.physics.box2d.Box2DDebugRenderer;
import com.badlogic.gdx.physics.box2d.CircleShape;
import com.badlogic.gdx.physics.box2d.FixtureDef;
import com.badlogic.gdx.physics.box2d.PolygonShape;
import com.badlogic.gdx.physics.box2d.World;
import com.badlogic.gdx.physics.box2d.BodyDef.BodyType;

public class GameScreen implements Screen{

   World world;
    Box2DDebugRenderer debugRenderer; 
    OrthographicCamera camera; 
    static final float BOX_STEP=1/60f; 
    static final int BOX_VELOCITY_ITERATIONS=6; 
    static final int BOX_POSITION_ITERATIONS=2; 
    static final float WORLD_TO_BOX=0.01f; 
    static final float BOX_WORLD_TO=100f;
   public GameScreen(){
       world = new World(new Vector2(0, -100), true); 
         camera = new OrthographicCamera(); 
         camera.viewportHeight = 320; 
         camera.viewportWidth = 480; 
         camera.position.set(camera.viewportWidth * .5f, camera.viewportHeight * .5f, 0f); 
         //Ground body 
         BodyDef groundBodyDef =new BodyDef(); 
         groundBodyDef.position.set(new Vector2(0, 10)); 
         Body groundBody = world.createBody(groundBodyDef); 
         PolygonShape groundBox = new PolygonShape(); 
         groundBox.setAsBox((camera.viewportWidth) * 2, 10.0f); 
         groundBody.createFixture(groundBox, 0.0f); 
         //Dynamic Body 
         BodyDef bodyDef = new BodyDef(); 
         bodyDef.type = BodyType.DynamicBody; 
         bodyDef.position.set(camera.viewportWidth / 2, camera.viewportHeight / 2); 
         Body body = world.createBody(bodyDef); 
         CircleShape dynamicCircle = new CircleShape(); 
         FixtureDef fixtureDef = new FixtureDef(); 
         fixtureDef.shape = dynamicCircle; 
         fixtureDef.density = 1.0f; 
         fixtureDef.friction = 0.0f; 
         fixtureDef.restitution = 1; 
         debugRenderer = new Box2DDebugRenderer();
   public void render(float delta) {
        debugRenderer.render(world, camera.combined); 

   //Screen Methods omitted

Code: Select all

public class Blast extends Game{ 
     public void create() {     
        setScreen(new GameScreen());

Running the code like this i get just a Black screen which doesnt show the ball or the ground. I get no errors.
Why do the ball and the ground not show up correctly when i run the code from a "Screen" as opposed to a "Game"?

Hope someone can help! Thanks

irresistible force
Posts: 1991
Joined: Tue Jun 24, 2008 8:25 pm
Location: Tokyo

Re: [LibGdx] box2d debug renderer problem

Postby irresistible force » Mon Sep 08, 2014 3:08 am

Sounds like a question for the libGDX forums.

Posts: 2
Joined: Mon Sep 08, 2014 2:05 am

Re: [LibGdx] box2d debug renderer problem

Postby mustache » Mon Sep 08, 2014 4:00 am

posted the same question there. I've got a feeling theyll be sending me to the box2d forums :lol:

Return to “Java”

Who is online

Users browsing this forum: No registered users and 1 guest