Let me introduce to you my solution to a well known problem with muzzleflashes!
The problem is caused by having a single plain to represent a flash. Sometimes the camera cannot see it! Most people make loads of flashes and point them in different directions, hoping that the camera will see one. This doesnt look very good, plus the extra polys cant help on FPS.
My solution uses only one flash object. And uses Matrix algebra to calculate which direction the flash should face. Its alot like billboarding but it will work in any angle you point it in.
Demo Attached.
Enjoy!
// MUZZLE FLASH SYSTEM
// VERSION 1 - 13th July 2009
// PROGRAMMED BY KARL HOBLEY
// YOU MAY USE THIS CODE IN COMMERCIAL AND NON COMMERCIAL PROJECTS FREE OF CHARGE!
// www.blitzwerks.co.uk
type MF_Main
objnum as dword
objnum2 as dword
vector as dword
matrix1 as dword
matrix2 as dword
matrix3 as dword
muzzleflashes as dword
endtype
type MF_Muzzleflash
created as dword
objnum as dword
exists as boolean
endtype
function MF_Start(freeobject,freeobject2,tex1,tex2,freevector,freematrix1,freematrix2,freematrix3)
//UDT
global MF_Main as MF_Main
//Make flash object
MF_Main.objnum=freeobject
make object plain freeobject,20,13
i=0
repeat
inc i
until object exist(i)=0
t=0
repeat
inc t
until mesh exist(t)=0
make object plain i,10,10
make mesh from object t,i
delete object i
add limb freeobject,1,t
rotate limb freeobject,0,270,270,0
offset limb freeobject,0,0.5,0,10
delete mesh t
set object cull freeobject,0
set object light freeobject,0
texture limb freeobject,1,tex1
texture limb freeobject,0,tex2
exclude object on freeobject
set object transparency freeobject,2
disable object zwrite freeobject
//Make another flash object (with limbs swapped)
MF_Main.objnum2=freeobject2
make object plain freeobject2,10,10
i=0
repeat
inc i
until object exist(i)=0
t=0
repeat
inc t
until mesh exist(t)=0
make object plain i,20,13
make mesh from object t,i
delete object i
add limb freeobject2,1,t
rotate limb freeobject2,1,270,90,0
offset limb freeobject2,1,0.5,0,10
delete mesh t
set object cull freeobject2,0
set object light freeobject2,0
texture limb freeobject2,0,tex1
texture limb freeobject2,1,tex2
exclude object on freeobject2
set object transparency freeobject2,2
disable object zwrite freeobject2
//Make Matrices and a vector
MF_Main.vector=freevector
MF_Main.matrix1=freematrix1
MF_Main.matrix2=freematrix2
MF_Main.matrix3=freematrix3
n=make vector4(MF_Main.vector)
n=make matrix4(MF_Main.matrix1)
n=make matrix4(MF_Main.matrix2)
n=make matrix4(MF_Main.matrix3)
//Make array
global dim MF_Muzzleflash(0) as MF_Muzzleflash
endfunction
function MF_Update()
//Loop through muzzleflashs
for i=1 to MF_Main.muzzleflashes
//Check if the muzzle flash exists
if MF_Muzzleflash(i).exists
//Work how long its been alive
time=timer()-MF_Muzzleflash(i).created
//If its too old, kill it
if time>40
delete object MF_Muzzleflash(i).objnum
MF_Muzzleflash(i).exists=0
endif
endif
next i
endfunction
function MF_CreateMuzzleflash(objectnumber,x#,y#,z#,xang#,yang#,zang#,camx#,camy#,camz#,size#)
//Allocate muzzleflash
muzzleflash=MF_Intern_FindFreeMuzzleFlash()
MF_Muzzleflash(muzzleflash).created=timer()
MF_Muzzleflash(muzzleflash).objnum=objectnumber
//Set Matrices
rotate x matrix4 MF_Main.matrix2,-xang#/57.2958
rotate y matrix4 MF_Main.matrix3,-yang#/57.2958
//Multiply matrices
set identity matrix4 MF_Main.matrix1
multiply matrix4 MF_Main.matrix1,MF_Main.matrix1,MF_Main.matrix3
multiply matrix4 MF_Main.matrix1,MF_Main.matrix1,MF_Main.matrix2
//Set vector to cameras current position
set vector4 MF_Main.vector,camx#-x#,camy#-y#,camz#-z#,0
//Transform vector with matrix
transform vector4 MF_Main.vector,MF_Main.vector,MF_Main.matrix1
//Get values
camx#=x vector4(MF_Main.vector)
camy#=y vector4(MF_Main.vector)
camz#=z vector4(MF_Main.vector)
//Get angle
angle#=atanfull(camx#,camy#)
//Instance the source object
if camz#>0
instance object objectnumber,MF_Main.objnum2
else
instance object objectnumber,MF_Main.objnum
endif
//Rotate back
rotate limb objectnumber,1,0,0,rnd(359)
//Position the muzzleflash
position object objectnumber,x#,y#,z#
//Rotate the muzzleflash
rotate object objectnumber,xang#,yang#,0
roll object right objectnumber,angle#
endfunction
function MF_Intern_FindFreeMuzzleFlash()
//Loop through current muzzleflashes to see if we can recyccle any
for i=1 to MF_Main.muzzleflashes
if MF_Muzzleflash(i).exists=0
MF_Muzzleflash(i).exists=1
exitfunction i
endif
next i
//Allocate a new muzzleflash
inc MF_main.muzzleflashes
muzzleflash=MF_main.muzzleflashes
array insert at bottom MF_Muzzleflash(0)
MF_Muzzleflash(muzzleflash).exists=1
endfunction muzzleflash