Muzzle Flash A How too

Hey Guys,

Its me again, just got back from finishing up most of my College apps. I have made lots of progress in my game, but now I want to know how do I make a muzzle flash. I have tried to add a PointLight to my weapon node, which is attached to my Character node, which is connected to the rootNode. This doesn’t show up, it doesn’t light up the area as I set it. I even made the Light color Bright red, and set it’s radius 150, but still nothing. Another effect I want is to have a particle effect happen that would actually be a muzzle flash(In theory, some kind of explosion, followed by a bit of smoke). Can you guys help me out? Point me in the right direction please.

Thanks

You may want to attach the light to the rootNode and update the position via your character control (i.e. pass the light in as a param and update it’s position according to the current control spatial position. Lights have to be associate with a node to effect the nodes geom. So, if it resides in (is associated with) your weapons node… your weapon is all it is going to light up.



As for the actual weapon flash. You may want to use multiple impostor-like meshes. They would look like this from the top down:

\|/

/|\

(each quad at approximately 33 degrees from the last. soooo 0, 33, 66 is a star pattern). All you need to do to create a custom mesh containing the muzzle flash and exhaust flare from a flash suppressor, is create a quad, clone them using Quaternions like so:

[java]

// verts is an ArrayList

// cIndex is the next index in the array list (or verts.size())

Quaternion q33 = q33.fromAngleAxis(33f2fFastMath.DEG_TO_RAD, Vector3f.UNIT_Y);

Quaternion q66 = q66.fromAngleAxis(66f2fFastMath.DEG_TO_RAD, Vector3f.UNIT_Y);



verts.add(new Vector3f( widthUnit, heightUnit, 0f));

verts.add(new Vector3f(-widthUnit, heightUnit, 0f));

verts.add(new Vector3f( widthUnit, -heightUnit, 0f));

verts.add(new Vector3f(-widthUnit, -heightUnit, 0f));

verts.add(new Vector3f( widthUnit, heightUnit, 0f));

verts.add(new Vector3f(-widthUnit, heightUnit, 0f));

verts.add(new Vector3f( widthUnit, -heightUni), 0f));

verts.add(new Vector3f(-widthUnit, -heightUnit, 0f));



// Clone and rotate the two quads by 33 degrees

for (int i = cIndex; i < cIndex+8; i++) {

verts.add(q33.mult(verts.get(i).clone()));

}



// Clone and rotate the two quads by 66 degrees

for (int i = cIndex; i < cIndex+8; i++) {

verts.add(q66.mult(verts.get(i).clone()));

}

[/java]



At this point you can rotate and position all 24 verts to place them at the end of the barrel or at one of the suppressor vents, then call the routine again to ad another flash. Once they are all in place (mapped with texCoords etc), add everything to a single node and apply a single texture. Flash from a barrel happens fast enough that you could simply hide and show this as needed and not need to eat up a ton of processing with a particle emitter.



Here be the code from adding triangles, mapping texCoords, etc:

[java]

// texCoord template assumes you want to use some texture atlasing.

this.coordTemplate = new Vector2f[imgCols+imgRows][4];

int index = 0;

for (int i = 0; i < imgCols; i++) {

for (int j = 0; j < imgRows; j++) {

this.coordTemplate[index][0] = new Vector2f(1.0f/(float)imgCols*(i), 1.0f/(float)imgRows*(j));

this.coordTemplate[index][1] = new Vector2f(1.0f/(float)imgCols*(i+1), 1.0f/(float)imgRows*(j));

this.coordTemplate[index][2] = new Vector2f(1.0f/(float)imgCols*(i), 1.0f/(float)imgRows*(j+1));

this.coordTemplate[index][3] = new Vector2f(1.0f/(float)imgCols*(i+1), 1.0f/(float)imgRows*(j+1));

index++;

}

}



// Normals template because these are all quads and nothing ever changes

private Vector3f[] normalsTemplate = new Vector3f[] {

new Vector3f(1f, 0f, 0f),

new Vector3f(-1f, 0f, 0f),

new Vector3f(1f, 0f, 0f),

new Vector3f(-1f, 0f, 0f),

new Vector3f(1f, 0f, 0f),

new Vector3f(-1f, 0f, 0f)

};



// Map triangle faces

int offsetInc = 8;

int offset = cIndex;



for (int i = 0; i < 3; i++) {

indexes.add(offset+0);

indexes.add(offset+2);

indexes.add(offset+3);

indexes.add(offset+3);

indexes.add(offset+1);

indexes.add(offset+0);



indexes.add(offset+3);

indexes.add(offset+2);

indexes.add(offset+0);

indexes.add(offset+0);

indexes.add(offset+1);

indexes.add(offset+3);



offset += offsetInc;

}



// Map texture coordinates

for (int x = 0; x < 6; x++) {

for (int i = 0; i < 4; i++) {

texCoord.add(coordTemplate[rand]);

}

}



int templateIndex = 0;

for (int x = 0; x < 6; x++) {

for (int i = 0; i < 4; i++) {

normals.add(normalsTemplate[templateIndex].x);

normals.add(normalsTemplate[templateIndex].y);

normals.add(normalsTemplate[templateIndex].z);

}

templateIndex++;

}

[/java]



Anyways… hope it helps

Maybe as an alternative look at the ParticleEmitter for the explosion particles, like in the tutorial for ParticleEmitter which shows an explosion :slight_smile:



https://wiki.jmonkeyengine.org/legacy/doku.php/jme3:beginner:hello_effects