使用Libgdx Box2d创建的主体在项目运行时没有显示在屏幕上



项目:我一直在使用libgdx-box2d的一个小项目,我想创建一个对象,渲染到屏幕上,并应用重力到它,使它与地面碰撞。我目前正在做的部分是创建和渲染身体。

问题:问题是,当我运行项目时,我创建的主体没有显示在屏幕上,我觉得我已经正确地创建了主体,因为没有编译问题。

What i tried:

  1. 我尝试了很多事情,比如增加我的圆的半径到放大(更明显)

  2. 通过camera = new OrthographicCamera(Gdx.graphics.getWidth() /10, Gdx.graphics.getHeight() /10); 放大到屏幕,如果它是太小了,看不见,但不管我怎么尝试,我创建的对象

  3. 将body的位置更改为不同的x,y位置

我想要的结果:我想要的是可能得到一个答案,为什么我创建的身体没有被显示,并在渲染它的一些帮助,以便它可以被看到。我希望我提出的问题的结构是清晰的和有信息的。提前感谢大家的帮助

这是我的相关代码:

package com.mohamed.JungleFighter;
import com.badlogic.gdx.Game;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.physics.box2d.BodyDef;
import com.badlogic.gdx.physics.box2d.BodyDef.BodyType;
import com.badlogic.gdx.physics.box2d.Box2DDebugRenderer;
import com.badlogic.gdx.physics.box2d.CircleShape;
import com.badlogic.gdx.physics.box2d.Fixture;
import com.badlogic.gdx.physics.box2d.FixtureDef;
import com.badlogic.gdx.physics.box2d.World;
import com.sun.xml.internal.ws.wsdl.writer.document.soap.Body;
//i'm extending libgdx's built in game class which implements the activity listener
public class JungleFighterMain extends Game {
private OrthographicCamera camera;
private SpriteBatch sBatch;
private Texture player;
private Texture enemy;
//private SpriteBatch enemyBatch;
private Sprite sprite1;
private Sprite sprite2;
//just setting my game heighty and width
public static int gameWidth = 1280, gameHeight = 720;
private World world;
private Box2DDebugRenderer debugRenderer;
p 
@Override
public void create () {
    //camera related
camera = new OrthographicCamera(Gdx.graphics.getWidth() /10, Gdx.graphics.getHeight() /10);
                //end of camera related
//BOX2D CODE FOR CREATING WWORLD
    World world = new World(new Vector2(0, -10), true); 
    //creating box2d body definition
    BodyDef bodyDef = new BodyDef();
    //setting body type to dynamic
    bodyDef.type = BodyType.DynamicBody;
    //position
    bodyDef.position.set(0, 0);
    //sending what i just made to the world i created
    // making a circle with a radius of 6
    CircleShape circle = new CircleShape();
    circle.setRadius(30f);
    //making my fixtures for the circle
    FixtureDef fixtureDef = new FixtureDef();
    fixtureDef.shape = circle;
    fixtureDef.density = 0.5f; 
    fixtureDef.friction = 0.4f;
    //making it bounce a little
    fixtureDef.restitution = 0.6f;
    //addding fixture attributes to my ball
    //Fixture fixture = body.createFixture(fixtureDef);
    //end of creating my circle ball 
    //creating my actual ball in the world
    world.createBody(bodyDef).createFixture(fixtureDef);
    circle.dispose();
    //GROUND START
    BodyDef bodyGround = new BodyDef();
    bodyGround.type = BodyType.StaticBody;
    bodyGround.position.set(-100,-100);
    //setting shape of ground
    ChainShape groundShape = new ChainShape();
    groundShape.createChain(new Vector2[] {new Vector2(-250, 0), new Vector2(250, 0)});
    //fixtures for ground
    FixtureDef fixtureDefGround = new FixtureDef();
    fixtureDefGround.shape = groundShape;
    fixtureDefGround.friction = 0.5f;
    fixtureDefGround.restitution = 0;
    world.createBody(bodyGround).createFixture(fixtureDefGround);
    groundShape.dispose();
}
public  void dispose() {
    world.dispose();
}
public void render (float delta) {
    Gdx.gl.glClearColor(0, 0, 0, 1);
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
    // camera related
       sBatch.setProjectionMatrix(camera.combined); 
    debugRenderer.render(world, camera.combined);
    world.step(TIMESTEP, VELOCITYITERATIONS, POSITIONITERATIONS);   
    }
}
    public  void resize(int width, int height){
    }
    public  void pause(){
    }
    public  void resume(){
    }
}

在Java中仅仅声明一个对象是不够的,就像:

private Box2DDebugRenderer debugRenderer

您还需要实例化它,否则它是一个空引用。尝试添加

debugRenderer = new Box2DDebugRenderer()

到您的create函数,以避免您在注释中提到的错误。构造函数也可以接受一些参数,但我不熟悉Box2D或LibGDX。

好,经过几个小时的研究,我终于意识到我需要添加相机。viewporttheight =;相机。viewportWidth;camera.position.set(相机。viewportWidth camera.viewportHeight);——

最新更新