Fix for Ground – Bullet Collision Bug in Flixel

A few friends and I have been working on a game together for the last few weeks. Everything was going well at first, but then we ran into a major snag: bullets were colliding with the ground when they clearly shouldn't have been. We are using Flixel and FlxWeapon from the Flixel Power Tools and we would see the issue pop up only on uneven terrain. If we changed our map to a basic, flat piece then everything worked as expected. However, introducing varying height platforms in the map would cause the issue. The image below demonstrates the problem.


I checked the Flixel forums and found two threads where people were experiencing the same problem, but no one seemed to have a concrete answer on how to fix the problem. It was pure luck that we happened upon a solution.

It turns out that the order of your layers matters. If the player layer is above the bullet layer then the collision breaks. By simply placing the player layer behind the bullet layer, the problem went away. I still don't know the underlying cause of the issue, but I figure this might help anyone else who comes across the problem.

To recap; good code:

private function drawLayers():void
{
    add(obstacleLayer);
    add(playerLayer);    // player layer behind bullet layer - OK!     
    add(bulletLayer);          
    add(enemyLayer);
    add(effectsLayer);
    add(uiLayer);
}

Bad Code

private function drawLayers():void
{
    add(obstacleLayer);
    add(bulletLayer); // bullet layer behind player layer - no good!
    add(playerLayer);
    add(enemyLayer);
    add(effectsLayer);
    add(uiLayer);
}

Below is a video demonstrating the game running first with the player layer above bullets and secondly with the player layer behind the bullets.

Bookmark and Share

One Response to “Fix for Ground – Bullet Collision Bug in Flixel”

  1. Mahmoud says:

    This note really help me; thanks a lot.

Leave a Reply

Subscribe to RSS feed FGS5 Badge