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, ColorRGBA.pink, FastMath.nextRandomFloat());
colors[1].interpolate(ColorRGBA.yellow, ColorRGBA.pink, FastMath.nextRandomFloat());
Line line = new Line("Line", points, null, colors, null);
line.setLightCombineMode(Spatial.LightCombineMode.Off);
line.setLineWidth(1f + 2f * FastMath.nextRandomFloat());
node.attachChild(line);
}
return node;
}
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.
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");
BlendState as = renderer.createBlendState();
as.setBlendEnabled(true);
as.setSourceFunction(BlendState.SourceFunction.SourceAlpha);
as.setDestinationFunction(BlendState.DestinationFunction.One);
ColorRGBA color = new ColorRGBA(1f, 0.15f, 0.15f, 0.2f);
MaterialState mat = renderer.createMaterialState();
mat.setAmbient(color);
mat.setEmissive(color);
mat.setDiffuse(color);
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!
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.
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();
vertex[1]= EntityManager.target.getLocalTranslation().subtract(b.getLocalTranslation()).normalize().clone();
Line l = new Line("line",vertex,null,null,null);
Ray ray = new Ray();
PickResults pr = new TrianglePickResults();
l.findPick(ray, pr);
pr.processPick();
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();
results.setCheckDistance(true);
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:
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.