Box2d 碰撞不会 运行 碰撞

Box2d collision wont run on collision

Contact Collision box2D 不会 运行 发生碰撞我希望子弹能够 运行 WorldContactListener beginContact 开始接触和结束接触时 运行ningendContact

我看了很多地方,我无法得到系统打印

这是我的联系人监听器class:

package com.mygdx.game;

import com.badlogic.gdx.physics.box2d.Contact;
import com.badlogic.gdx.physics.box2d.ContactImpulse;
import com.badlogic.gdx.physics.box2d.ContactListener;
import com.badlogic.gdx.physics.box2d.Manifold;

public class WorldContactListener implements ContactListener {

    @Override
    public void beginContact(Contact contact) {
        //called when 2 fixtures collide
        System.out.println("Begin Contact");

    }

    @Override
    public void endContact(Contact contact) {
        //called when the 2 fixtures connected gets split apart
        System.out.println("end Contact");
    }

    @Override
    public void preSolve(Contact contact, Manifold oldManifold) {
        //gives power to change the characteristics of fixture             
collision
    }

    @Override
    public void postSolve(Contact contact, ContactImpulse impulse) {
        //gives results of what happened because of collision like 
angles ext
        }

}

播放画面class:

 package com.mygdx.game;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.TextureAtlas;
import com.badlogic.gdx.maps.MapObject;
import com.badlogic.gdx.maps.objects.RectangleMapObject;
import com.badlogic.gdx.maps.tiled.TiledMap;
import com.badlogic.gdx.maps.tiled.TmxMapLoader;
import com.badlogic.gdx.maps.tiled.renderers.OrthogonalTiledMapRenderer;
import com.badlogic.gdx.math.Rectangle;
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.ContactListener;
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.utils.viewport.FitViewport;
import com.badlogic.gdx.utils.viewport.ScreenViewport;
import com.badlogic.gdx.utils.viewport.StretchViewport;
import com.badlogic.gdx.utils.viewport.Viewport;
import com.mygdx.game.Sprites.Bullet;
import com.mygdx.game.Sprites.InversePlayer;
import com.mygdx.game.Sprites.Player;

@SuppressWarnings("unused")
public class PlayScreen implements Screen {
    private main game;
    private TextureAtlas atlas;

    private OrthographicCamera gamecam;
    private Viewport gamePort;
    private Hud hud;

    private TmxMapLoader maploader;
    private TiledMap map;
    private OrthogonalTiledMapRenderer renderer;

    private World world;
    private Box2DDebugRenderer b2dr;


    private Player player;
    private InversePlayer inversePlayer;
    private Bullet bullet;

    public PlayScreen(main game) {
        atlas = new TextureAtlas("BurningShooterPlayer.pack");

        this.game = game;
        gamecam = new OrthographicCamera();
        gamePort = new FitViewport(main.V_WIDTH / main.PPM, main.V_HEIGHT / main.PPM, gamecam);
        hud = new Hud(game.batch);

        maploader = new TmxMapLoader();
        map = maploader.load("map1.tmx");
        renderer = new OrthogonalTiledMapRenderer(map, 1 / main.PPM);
        gamecam.position.set(gamePort.getWorldWidth()/2, gamePort.getWorldHeight()/2, 0);

        world = new World(new Vector2(0,-10), true);

        b2dr = new Box2DDebugRenderer();

        new B2WorldCreator(this);

        player = new Player(this);

        inversePlayer = new InversePlayer(this, .32f, .32f);

        bullet = new Bullet(this, .64f, .64f);

    }

    public TextureAtlas getAtlas() {
        return atlas;
    }

    @Override
    public void show() {
        //world.setContactListener(ContactListener listener) .
        }

    public void handleInput(float dt) {
        if(Gdx.input.isKeyJustPressed(Input.Keys.W))
            player.b2body.applyLinearImpulse(new Vector2(0, 4f), player.b2body.getWorldCenter(), true);
        if(Gdx.input.isKeyPressed(Input.Keys.D) && player.b2body.getLinearVelocity().x <= 2)
            player.b2body.applyLinearImpulse(new Vector2(0.1f , 0), player.b2body.getWorldCenter(), true);
        if(Gdx.input.isKeyPressed(Input.Keys.A) && player.b2body.getLinearVelocity().x >= -2)
            player.b2body.applyLinearImpulse(new Vector2(-0.1f , 0), player.b2body.getWorldCenter(), true);
        if(Gdx.input.isKeyJustPressed(Input.Keys.RIGHT)) {
            bullet.b2body.setLinearVelocity(0, 0);
            bullet.b2body.setTransform(new Vector2((float)(player.b2body.getPosition().x+player.getWidth()-(3/main.PPM)), (float)(player.b2body.getPosition().y)), 0);
            bullet.b2body.applyLinearImpulse(new Vector2(Bullet.BULLET_SPEED, 0), bullet.b2body.getWorldCenter(), true);
            Bullet.Right = true;
        }
        if(Gdx.input.isKeyJustPressed(Input.Keys.LEFT)) {
            bullet.b2body.setLinearVelocity(0, 0);
            bullet.b2body.setTransform(new Vector2((float)(player.b2body.getPosition().x-player.getWidth()+(3/main.PPM)), (float)(player.b2body.getPosition().y)), 0);
            bullet.b2body.applyLinearImpulse(new Vector2(-Bullet.BULLET_SPEED, 0), bullet.b2body.getWorldCenter(), true);
            Bullet.Right = false;
        }
        //if(Gdx.input.isKeyJustPressed(Input.Keys.SPACE)) {
            //bullet.b2body.setBullet(true);
            //world.destroyBody(bullet.b2body);
        //}
    }

    public void update(float dt) {
        handleInput(dt);

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

        player.update(dt);
        inversePlayer.update(dt);
        bullet.update(dt);

        gamecam.position.x = player.b2body.getPosition().x;

        gamecam.update(); 
        renderer.setView(gamecam);
    }



    @Override
    public void render(float delta) {
        update(delta);
        Gdx.gl.glClearColor(0, 0, 0, 1);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

        renderer.render();

        b2dr.render(world, gamecam.combined);

        game.batch.setProjectionMatrix(gamecam.combined);
        game.batch.begin();
        player.draw(game.batch);
        inversePlayer.draw(game.batch);
        bullet.draw(game.batch);
        game.batch.end();

        game.batch.setProjectionMatrix(hud.stage.getCamera().combined);
        hud.stage.draw();
    }

    @Override
    public void resize(int width, int height) {
        gamePort.update(width, width);
    }

    public TiledMap getMap() {
        return map;
    }
    public World getWorld() {
        return world;
    }

    @Override
    public void pause() {

    }

    @Override
    public void resume() {

    }

    @Override
    public void hide() {

    }

    @Override
    public void dispose() {
        map.dispose();
        renderer.dispose();
        world.dispose();
        b2dr.dispose();
        hud.dispose();
    }

}

子弹代码(我想检测它是否与另一个物体碰撞):

package com.mygdx.game.Sprites;

import com.badlogic.gdx.graphics.g2d.Animation;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.physics.box2d.BodyDef;
import com.badlogic.gdx.physics.box2d.FixtureDef;
import com.badlogic.gdx.physics.box2d.PolygonShape;
import com.badlogic.gdx.utils.Array;
import com.mygdx.game.PlayScreen;
import com.mygdx.game.main;

public class Bullet extends projectile{
    private float stateTime;
    private Animation<TextureRegion> walkAnimation;
    private Array<TextureRegion> frames;
    public static float BULLET_SPEED = 1f;
    public static boolean Right = true;
    public Bullet(PlayScreen screen, float x, float y) {
        super(screen, x, y);
        frames = new Array<TextureRegion>();
        frames.add(new TextureRegion(screen.getAtlas().findRegion("BurningShooterPlayer"),111, -1, 15, 8));
        walkAnimation = new Animation<TextureRegion>(0.1f, frames);
        stateTime = 0;
        setBounds(getX(), getY(), (float) (7.5/ main.PPM), 4 / main.PPM);
    }

    public void update(float dt) {
        stateTime += dt;
        setPosition((b2body.getPosition().x - getWidth() / 2), b2body.getPosition().y - getHeight() / 2);
        setRegion(walkAnimation.getKeyFrame(stateTime, true));

        if((!Right) && !walkAnimation.getKeyFrame(dt).isFlipX()) {
            walkAnimation.getKeyFrame(dt).flip(true, false);
        }
        else if((Right) && walkAnimation.getKeyFrame(dt).isFlipX()) {
            walkAnimation.getKeyFrame(dt).flip(true, false);
        }

    }

    @Override
    protected void defineProjectile() {
        BodyDef bdef = new BodyDef();
        bdef.position.set(64 / main.PPM, 64 / main.PPM);
        bdef.type = BodyDef.BodyType.DynamicBody;
        b2body = world.createBody(bdef);

        FixtureDef fdef = new FixtureDef();

        PolygonShape shape = new PolygonShape();
        shape.setAsBox((float) (7.5 / 2 / main.PPM), 4 / 2 / main.PPM);
        fdef.filter.categoryBits = main.ENEMY_BIT;
        fdef.filter.maskBits = main.GROUND_BIT |
                main.ENEMY_BIT |
                main.OBJECT_BIT;
        fdef.shape = shape;
        fdef.density = 100;
        b2body.setBullet(true);
        b2body.createFixture(fdef);
        b2body.setUserData(this);
    }
}

您创建了 World 但您忘记将 ContactListener 设置为您的世界:

private World world;
private WorldContactListener worldContactListener;

public PlayScreen(main game) {
    ...
    world = new World(new Vector2(0,-10), true);
    worldContactListener = new WorldContactListener();
    world.setContactListener(worldContactListener);
    ...
}