LetsMakeAnIndieGame / PhysicsShmup

A 2D shooter made with LibGDX
65 stars 20 forks source link

The game crashes with NullPointerException during collisions between some physics bodies #3

Closed LetsMakeAnIndieGame closed 8 years ago

LetsMakeAnIndieGame commented 8 years ago

I've noticed consistently that the game will crash when bullets from the Player collide with the Body of the Enemy character(s). I plan on fixing this when I finish the Threaded Pathfinding series of videos/updates, but if someone wants to jump in then it would be a big help.

phowell commented 8 years ago

My Java is pretty rusty, and I haven't worked with a code-base anywhere near this size or complexity in years, so take this with a grain of salt. (I've been using libGDX, along with your series, to try and re-learn this stuff). It looks like when you put your flying enemy into the game, you may have left some hanging references to a more generic enemy entity. You were also failing to instantiate glyphLayout. I modified EnemyCollisionComponent.java like this:

` private ComponentMapper am = ComponentMapper.getFor(EnemyAgentComponent.class); private ComponentMapper fm = ComponentMapper.getFor(FlyingTestEnemyComponent.class);

private GlyphLayout glyphLayout = new GlyphLayout();

@Override
public void handleCollision(Engine engine, Entity collider, Entity collidee) {
    short type = cm.get(collidee).type;

    EnemyDataComponent data = em.get(collider);

    if (type == PhysicsManager.COL_FRIENDLY_BULLET) {
        if(am.has(collider)){
            am.get(collider).isShot = true;
        } else if(fm.has(collider)){
            fm.get(collider).isShot = true;
        }
        long damage = bm.get(collidee).damage;

`

I don't know why markdown isn't properly formatting those first few lines, but you should get the idea. after making those changes, the game continues to crash when the bird is shot, but now it appears to be a steering behavior issue... I think it's creating a zero length fleeing path... I think... I'm in a bit over my head.

Either way, I hope this helps. Thanks for starting this up again, it's the best source I've found outside of trolling the javadoc.

LetsMakeAnIndieGame commented 8 years ago

Thanks for looking into it. I'll take a crack at it tonight before I work on the next part in the series.

LetsMakeAnIndieGame commented 8 years ago

All right, starting with what you pointed out I resolved the problem. I guess the issue was I switched from the ground-based enemies to the flying enemies, but never changed the component type. Then I didn't instantiate the Glyph thingy. AND finally I didn't set a target for the enemy. What I discovered is that the enemy behavior is a hot mess. The cursor won't appear until you shoot an enemy, sometimes the pathfinding goes from the enemy to the bullet and messes with the bullet's physics which doesn't even remotely make sense. I guess I'll have to clean that up at some point. Anyway, thanks again for the help.