java - 调试线条未绘制且未为玩家设置重力

标签 java libgdx bulletphysics

我正在使用《Building a 3d Game with Libgdx》一书创建一个演示游戏。重力似乎根本不起作用,所以我用 debugDrawer 绘制调试线,但角色的调试线也没有显示。

地板的调试线显示正确,并且当我检查了碰撞对象计数时,角色也被添加到世界中。

这是我的运行游戏:(不能直接发布图片作为我的第一篇文章;P)

game demo

我的角色创建代码是:

private static Entity createCharacter(BulletSystem bulletSystem, float x, float y, float z) {
    Entity entity = new Entity();
    ModelComponent modelComponent = new ModelComponent(playerModel, x, y, z);
    entity.add(modelComponent);

    CharacterComponent characterComponent = new CharacterComponent();
    characterComponent.ghostObject = new btPairCachingGhostObject();
    characterComponent.ghostObject.setWorldTransform(modelComponent.instance.transform);
    characterComponent.ghostShape = new btCapsuleShape(2f, 2f);
    characterComponent.ghostObject.setCollisionShape(characterComponent.ghostShape);
    characterComponent.ghostObject.setCollisionFlags(btCollisionObject.CollisionFlags.CF_CHARACTER_OBJECT);
    characterComponent.characterController = new btKinematicCharacterController(characterComponent.ghostObject,
            characterComponent.ghostShape, .35f);
    characterComponent.ghostObject.userData = entity;
    entity.add(characterComponent);

    // add character to bullet world
    bulletSystem.collisionWorld.addCollisionObject(characterComponent.ghostObject,
                    (short) btBroadphaseProxy.CollisionFilterGroups.CharacterFilter,
                    (short) (btBroadphaseProxy.CollisionFilterGroups.AllFilter));

    // It is showing 2 in console.

    System.out.println(bulletSystem.collisionWorld.getNumCollisionObjects());
    bulletSystem.collisionWorld.addAction(characterComponent.characterController);
    return entity;
}

BulletSystem.java


public class BulletSystem extends EntitySystem implements EntityListener {
    public final btCollisionConfiguration collisionConfiguration;
    public final btCollisionDispatcher dispatcher;
    public final btBroadphaseInterface broadphase;
    public final btConstraintSolver solver;
    public final btDiscreteDynamicsWorld collisionWorld;
    private btGhostPairCallback ghostPairCallback;
    public int maxSubSteps = 5;
    public float fixedTimeStep = 1f / 60f;

    @Override
    public void addedToEngine(Engine engine) {
        engine.addEntityListener(Family.all(BulletComponent.class).get(),
                this);
    }

    public BulletSystem() {
        collisionConfiguration = new
                btDefaultCollisionConfiguration();
        dispatcher = new
                btCollisionDispatcher(collisionConfiguration);

        broadphase = new btAxisSweep3(new Vector3(-1000, -1000,
                -1000), new Vector3(1000, 1000, 1000));
        solver = new btSequentialImpulseConstraintSolver();
        collisionWorld = new btDiscreteDynamicsWorld(dispatcher,
                broadphase, solver, collisionConfiguration);
        ghostPairCallback = new btGhostPairCallback();
        broadphase.getOverlappingPairCache().
                setInternalGhostPairCallback(ghostPairCallback);
        this.collisionWorld.setGravity(new Vector3(0, -9f, 0));
    }

    @Override
    public void update(float deltaTime) {
        collisionWorld.stepSimulation(deltaTime, maxSubSteps,
                fixedTimeStep);
    }


}

我的游戏屏幕是

class GameScreen extends ScreenAdapter {
    private BulletGame game;
    private PerspectiveCamera cam;
    private Environment env;
    private ModelBatch mb;

    private Engine engine;
    private BulletSystem bulletSystem;
    private Model floor;
    private Entity character;

    private DebugDrawer debug;
    GameScreen(BulletGame bulletGame) {
        game = bulletGame;
        Bullet.init();
        initCamera();
        initEnvironemnt();
        mb = new ModelBatch();

        ModelBuilder modelBuilder = new ModelBuilder();
        floor = modelBuilder.createBox(40, 1, 40,
                new Material(ColorAttribute.createDiffuse(Color.YELLOW),
                        ColorAttribute.createSpecular(Color.BLUE),
                        FloatAttribute.createShininess(16f)),
                VertexAttributes.Usage.Position
                        | VertexAttributes.Usage.Normal);

        engine = new Engine();
        engine.addSystem(bulletSystem = new BulletSystem());    // WORLD with entity listener
        engine.addSystem(new RenderSystem(mb, env));    // renders all MODEL_COMPONENTS


        debug = new DebugDrawer();
        debug.setDebugMode(btIDebugDraw.DebugDrawModes.DBG_MAX_DEBUG_DRAW_MODE);
        bulletSystem.collisionWorld.setDebugDrawer(debug);
        addEntities();

    }

    private void addEntities() {
        engine.addEntity(EntityFactory.createStaticEntity(floor,0, 0, 0));
        engine.addEntity(character = EntityFactory.createPlayer(bulletSystem, 0,10,15));
    }

    @Override
    public void render(float delta) {

        mb.begin(cam);
        engine.update(delta);
        mb.end();

        debug.begin(cam);
        bulletSystem.collisionWorld.debugDrawWorld();
        debug.end();
    }

最佳答案

我正在以错误的方式处理屏幕外的角色碰撞对象:

private void setScreen(Screen screen) {
        if (screen != null) {
            screen.hide();
            screen.dispose();
        }
        this.screen = screen;
        if (screen != null) {
            screen.show();
            screen.resize(Gdx.graphics.getWidth(),
                    Gdx.graphics.getHeight());
        }
    }

在我的屏幕中处理方法:

public void dispose() {
        mb.dispose();

        bulletSystem.collisionWorld.removeAction(character.getComponent
                (CharacterComponent.class).characterController);
        bulletSystem.collisionWorld.removeCollisionObject
                (character.getComponent(CharacterComponent.class).ghostObject);
        character.getComponent(CharacterComponent.class).characterController.dispose();
        character.getComponent(CharacterComponent.class).ghostObject.dispose();
        character.getComponent(CharacterComponent.class).ghostShape.dispose();
    }

坚持使用默认的 Game 类解决了问题。

关于java - 调试线条未绘制且未为玩家设置重力,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61968895/

相关文章:

c++ - 物理子弹 - 有任何 'Ghost' body 吗?

java - Robolectric 创建不同应用程序的虚拟共享首选项

java - 在 Java 中请求 URL 时忽略证书错误

java - GWT RequestFactory - 向代理类添加自定义方法?

java - 使用 Libgdx 获取 DesktopLauncher 的变量

java - libgdx 桌面项目正在捕捉返回键

java - 在 libgdx 中使用filledcircle和pixmap

debugging - libGDX 中的调试绘图

Java Linux 非阻塞套接字超时行为

javascript - ammo.js 在谷歌浏览器中的性能