Tracer Bullets

I'm adding tracer bullet effect to my robot game. I'm using following switch node for the effect. One of the random lines is shown per frame. The node is scaled by the distance from gun to the target. I works quite well but the problem is the color. The lines were gray until I found that I have to set light combine mode off. Now the colors are still a bit dark. Any Ideas?

   public static SwitchNode createTracerBullets(int count) {
      SwitchNode node = new SwitchNode("Lines");
      Vector3f[] points = { new Vector3f(), new Vector3f() };
      ColorRGBA[] colors =  { new ColorRGBA(),  new ColorRGBA() };
      for (int i = 0; i < count; i++) {
         points[0].x = FastMath.nextRandomFloat();
         points[1].x = FastMath.nextRandomFloat();
         colors[0].interpolate(ColorRGBA.yellow,, FastMath.nextRandomFloat());
         colors[1].interpolate(ColorRGBA.yellow,, FastMath.nextRandomFloat());
         Line line = new Line("Line", points, null, colors, null);
         line.setLineWidth(1f + 2f * FastMath.nextRandomFloat());
        return node;

Probably from interpolating between yellow and pink…

What does it look like with just red or green?

The problem is not in the interpolation of colors, because even with pure yellow the color looks dark.

After posting the this code I implemented the tracer effect the same way but using narrow cylinders. I looks better, because the thickness of Line object is constant in near and far. Hopefully I get this color problem solved also. I will post the new tracer bullet code and screen shot when it is ready.

Maybe some of the color intensity is lost on the line due to anti-aliasing if you are using that. Maybe try making the line a little more wide.

Lines and points do not react to perspective, they always have the same width.

Take a look at TestTrailMesh, maybe thats is what you're looking for.

Thanks for the help! I finally found suitable RenderStates and quite satisfied  :P. Here is the code and a video. Billboard quad like in TestTrailMesh would be best way, but with cylinder it was maybe easier to implement.


Sorry for bad quality. It is recorded using cell phone.

    * Creates unit size SwitchNode for tracer bullet effect.
    * @param count        the count of random tracer bullets
    * @param maxThickness the maximum thickness of tracer
    * @return             the SwitchNode
   public static SwitchNode createTracerBullets(int count, float maxThickness) {
      SwitchNode node = new SwitchNode("Lines");

      for (int i = 0; i < count; i++) {
         float height = 0.3f + 0.7f * FastMath.nextRandomFloat();
         float offset = (1.0f - height) * FastMath.nextRandomFloat();
         float thickness = 0.5f + 0.5f * FastMath.nextRandomFloat();
         thickness *= maxThickness;
         Cylinder cyl = new Cylinder("Cylinder", 2, 5, thickness, height, false);
         cyl.setLocalTranslation(0, 0, 0.5f + offset);

      Renderer renderer = DisplaySystem.getDisplaySystem().getRenderer();
      CullState cs = renderer.createCullState();

      BlendState as = renderer.createBlendState();

      ColorRGBA color = new ColorRGBA(1f, 0.15f, 0.15f, 0.2f);
      MaterialState mat = renderer.createMaterialState();

      node.setLocalRotation(new Quaternion().fromAngleAxis(FastMath.HALF_PI, Vector3f.UNIT_Y));
      return node;

I really like that effect. Do you mind sharing an example of how that code is used?

I absolutely LOVE this effect and it is EXACTLY what I would love for my game.

Thanks for the example of how to use it - I am trying to port this to my current game at the moment!

So for the above example - gunNode.attachChild(gun) - I am presuming gun is your spatial containing the model which does the actual firing - or the moving model which responds to your mouse clicks.

SO you do as follows:

1/ Create a new empty node.

2/ Attach the complicated moving animated node to this which handles mouse clicks and responds by moving and      firing.

3/ Attach the particle affect SwitchNode to the above to create the effect

Any chance of a simpleTestGame example so I can get a clearer idea of how to port this over!

Thanks so much for any help


I like the effect too. I have not coded this robot game project in two months so maybe it is time to continue this. The weather has lately been too good for coding  8). Now the weather in Finland seems to turn back to normal (about 15 Celsius).

I will try to make SimpleTestGame without physics. I will add the smoke and flames also to the example.

That would be absolutely fantastic!! I cannot wait for it - any eta on the example!!! =>

I actually got the simple trail effect working , can you show me the particle code for the other effects going on during that weapon fire, because the bullet trail is working, but that video shows a host of other fantastic graphical effects! Thanks


I actually cannot work out for the life of my how to work out the correct way to get your hitRay.length calculation to work to find out the appropriate scale for the particles actually.

I have tried this but its not working:

Vector3f[] vertex = new Vector3f[2];
vertex[0] = b.getLocalTranslation().clone();
Line l = new Line("line",vertex,null,null,null);
Ray ray = new Ray();
PickResults pr = new TrianglePickResults();
l.findPick(ray, pr);
if(pr.getNumber() > 0)System.out.println("Num is: "+pr.getNumber() +" and distance is: "+pr.getPickData(0).getDistance());

It always returns 0..... so im not sure how you got that to work, enlighten me!

The spark effect is standard jme ParticleSystem. The particle system is attached to the gunNode. The local translation is setup with the distance between gun and the hit point. The light smoke is also standard jme ParticleSystem. I will add these to my example.

I use jBullet physic library for picking. It is super fast. I have no experience of jme picking system.

I think in jme the picking would be some thing like this. I have not tested the following code.

// the root for pickable nodes
Node sceneRoot;

Vector3f locationOfGunBarrel = new Vector3f();
Vector3f directionOfGunBarrel = new Vector3f();
// TODO set values to the vectors in world coordinates
Ray ray = new Ray(locationOfGunBarrel, directionOfGunBarrel);
TrianglePickResults results = new TrianglePickResults();
sceneRoot .findPick(ray, results) ;
float distance = -1;
if(results.getNumber() > 0) {
    PickData data = results.getPickData(0) ;
    distance = data.getDistance();

I hope I have today time to post the working example.

That would be amazing mate, the working example is something I could learn a lot from!

So that distance code you posted works nicely - I was just using it wrongly!

So now I am scaling the lines to the correct size - however the problem is the line seems to grow from both sides - which means that position of the particles now looks wrong i.e the pic below:

Love your gun fire effect - reminds me of ED209 from robocop.  Any text/articles on how to implement?




Could you possibly state how you create the bullet fuzz effect around the barrell of the guns - because that is not included in your example!

Ah actually I see I was running an older version of the program.  I thought I had downloaded the most recent.  The version I was running did not have the option to save tracer files.

Thanks for your help -


is there a way to stop the tracer round from going through the enemy if it causes the explosion effect?