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

项目运行时,使用Libgdx Box2d创建的主体未显示在屏幕上,libgdx,box2d,render,Libgdx,Box2d,Render,项目:我一直在使用libgdx-box2d进行一个小项目,我想在其中创建一个对象,将其渲染到屏幕上,并对其应用重力,使其与地面碰撞。我目前正在处理的部分是创建和渲染身体 问题:问题是,我创建的主体在运行项目时没有显示在屏幕上,我觉得我已经正确创建了主体,因为没有编译问题 我的尝试: 我尝试了很多方法,比如将圆的半径增加到 使其更大(更可见) 通过执行camera=new正交摄影机(Gdx.graphics.getWidth()/10,Gdx.graphics.getHeight()/10)放大屏

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

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

我的尝试:

  • 我尝试了很多方法,比如将圆的半径增加到 使其更大(更可见)

  • 通过执行
    camera=new正交摄影机(Gdx.graphics.getWidth()/10,Gdx.graphics.getHeight()/10)放大屏幕;
    
    如果是 太小,看不见,但无论我尝试了什么,我创建的对象 看不见

  • 将主体的位置更改为不同的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()
    

    创建函数以避免在注释中提到的错误。构造器可能也有一些参数,但我不熟悉Box2D或LibGDX。

    好的,经过几个小时的研究,我终于意识到我需要添加camera.viewportHeight=;摄像头。视口宽度;照相机.位置.设置(照相机.视口宽度,照相机.视口高度);-

    可能是因为//debugRenderer.render(world,camera.combined);,你没有绘制你的世界?没有,我相信调试。渲染器仅用于测试目的,渲染时并不需要它,它是可选的(通常用于调试测试用例以收集数据),你想看到什么?你没有画任何东西。这是一个有趣的问题,我尝试取消debugRenderer.render(world,camera.combined)的注释;但这只会导致我的项目在启动时立即崩溃,它会编译,但一旦我运行它,它就会崩溃。我已经稍微更新了我的代码,取消了debug.Renderer的注释并修复了崩溃问题,但我的项目仍然不会呈现我的主体@Pauli尝试在create()debugRenderer=new-box2debugrenderer(world,camera.combined)下添加此项;但是我在声明从Box2DebugRenderer的参数中删除参数时出错。但当我删除它们时,我会回到我从文档中开始的地方(没有渲染任何内容),构造函数获取一个世界和一个Matrix4对象,即投影矩阵。因此,您将希望通过camera.projection。如果没有代码和确切的错误,我无法进一步排除故障。感谢帮助,我尝试将其更改为debugRenderer=new-box2debugrenderer(world,camera.projection);但是我得到了这个错误“构造函数box2debugrenderer(World,Matrix4)未定义”。我还将调试渲染器更改为debugRenderer=new-box2debugrenderer(world,camera.projection);在render()stageNo问题下。好的,我很抱歉,因为我似乎在那里向您提供了错误信息,我查看的文档可能不正确/过时。在创建函数中,您需要
    debugRenderer=new box2debugrenderer()
    ,在渲染函数中,您只需要
    debugRenderer.render(world,camera.projection)
    ,您可能需要更改变量名。这似乎会通知渲染器要渲染的世界信息以及要从中渲染的摄影机。希望这能有所帮助。好吧,经过几个小时的研究,我终于意识到我需要添加摄像头;摄像头。视口宽度;camera.position.set(camera.viewportWidth、camera.viewportHeight);