简体   繁体   中英

Box2D Shapes Not Rendering

I've been looking at some other threads, and despite every thing I have tried, the shapes I have created in box2d are not rendering. It is very bizarre, and I hope that you guys can provide a solution.

public class worldRender {
fighterGame game;
PlayScreen renderGame;
private Viewport gamePort = new StretchViewport(1020 / game.PPM,760 / game.PPM);
World world = new World(new Vector2(0,-10), true);
Box2DDebugRenderer b2dr = new Box2DDebugRenderer();
private OrthographicCamera gameCam = new OrthographicCamera();
BodyDef bDef = new BodyDef();
public Body b2body;
FixtureDef fixtureDef = new FixtureDef();
ShapeRenderer shapeRender;


public worldRender() {

    gameCam = new OrthographicCamera(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
    gameCam.position.set(1020/2, 760/2, 0);


}

public worldRender(float dt) {

    gameCam.update();
    world.step(1/60f, 6, 2);


    b2dr.render(world, gameCam.combined);
    bodyRender();

}
public void bodyRender() {

    BodyDef bdef = new BodyDef();
    bdef.position.set(0.0f / game.PPM,4.0f / game.PPM);
    bdef.type = BodyDef.BodyType.DynamicBody;
    b2body = world.createBody(bdef);
    FixtureDef fdef = new FixtureDef();
    fdef.friction = 0.25f;
    CircleShape shape = new CircleShape();
    shape.setRadius(5);
    fdef.shape = shape;
    fdef.density = 1.0f;
    b2body.createFixture(fdef);


}

}

I'm going to list off a few solutions because not everything is clear in the snippet:

  1. Are you sure the worldRender() method is being run
    • If you are using Game and Screen make sure your game render() method calls super() otherwise your Screen render() method will not be run;
  2. As mentioned before is the value of PPM correct/what is it?

Does this draw:

// First we create a body definition
BodyDef bodyDef = new BodyDef();
bodyDef.type = BodyType.StaticBody;

// Set our body's starting position in the world
bodyDef.position.set(50, 50);

// Create our body in the world using our body definition
Body body = world.createBody(bodyDef);

// Create a circle shape and set its radius to 6
CircleShape circle = new CircleShape();
circle.setRadius(10f);

// Create a fixture definition to apply our shape to
FixtureDef fixtureDef = new FixtureDef();
fixtureDef.shape = circle;

// Create our fixture and attach it to the body
Fixture fixture = body.createFixture(fixtureDef);

// Remember to dispose of any shapes after you're done with them!
// BodyDef and FixtureDef don't need disposing, but shapes do.
circle.dispose();

This should draw a circle of radius 10 at x=10,y=10 (make sure those points are in your view port.

I would suggest cleaning up your code a bit, and studying some more tutorials, that will probably solve your problems. But let's give you some hints to get you on the way:

I somehow suspect you are creating a worldRender object every frame. Java is a garbage collected language, doing so will severly impact your performance. Persist as many objects as possible. In my bigger games, i create next to 0 objects each render and game logic tick. Aim for that.

Finally, what will probably solve your problem: the camera you use to render your box2ddebugrenderer ("dbrndr") has screen pixels as units. The dbrndr uses meters as render units. You need to give the dbrndr its own camera in meters. Your current method will draw a 10pixel wide circle at 0 / 4 pixels in the bottom left corner.

Do you create your world with gravity? if yes, the circle instantly falls out of your screen...Yes you do.

You might actually even see your circle for a splitsecond in the lower left corner after starting... given that you render before you do box2d logic.

Please dispose() all objects that you create, otherwise the memory they occupy is not free'd afterwards.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM