Posted: 30th Jun 2003 1:12
I've been trying for a while to get realistic-ish missile trails using the native particle system, but gave up and wrote some code this afternoon which does it nicely.

image deleted

This code is really pitched at a newbie, since it's quite simple. I suppose it's just handy to have knockin about really. It only handles one missile at a time (so you can't unleash terror) which is what I need in my game, but I suppose that could be easily adapted with a 'Missile Age' array...

+ Code Snippet
sync on
sync rate 30
autocam off
color backdrop rgb(0,0,0)

load image "smoke.bmp", 2

` -- convert ambient to point light
set ambient light 0
set point light 0,0,0,0
set light range 0, 10000

` -- make matrix stuff
make matrix 1,5000,5000,20,20
position matrix 1,0,0,0
randomize matrix 1,80
update matrix 1

` -- make player --
make object sphere 1, 10
set object 1,1,1,1,1,1
color object 1,rgb(255,255,255)
global px# as float
global py# as float
global pz# as float
global ay# as float

ay# = 0
px# = 2500
pz# = 2500
py# = 100

` -- make missile --
make object cube 100,5
color object 100,rgb(128,0,0)
hide object 100

global bMissileFlying as boolean    : bMissingFlying = 0
global bShooting as boolean         : bShooting = 0

` -- missile params
global iMissileSpeed as integer     : iMissileSpeed = 8
global iMissileAge as integer       : iMissileAge = 0
global iMissileLife as integer      : iMissileLife = 200

` -- smoke params
global iSmokeInterval as integer    : iSmokeInterval = 0
global iCurrentSmoke as integer     : iCurrentSmoke = 0
global iSmokeLife as integer        : iSmokeLife = 250
global iNumberSmokes as integer     : iNumberSmokes = 150

` -- if aSmokeAge(i) == 0, then it's dead
global dim aSmokeAge(iNumberSmokes) as integer

for i = 1 to iNumberSmokes
   aSmokeAge(i) = 0
   make object plain 500+i,15,15
   color object 500+i, rgb(255,255, 255)
   hide object 500+i
   texture object 500+i,2
   ghost object on 500+i
   fade object 500+i, 0
next i

` --------------------------- MAIN LOOP --------------------------- `

do
   if leftkey()=1 then ay# = wrapvalue(object angle y(1) - 2)
   if rightkey()=1 then ay# = wrapvalue(object angle y(1) + 2)
   if upkey()=1:
      px# = newxvalue(px#, ay#, 5)
      pz# = newzvalue(pz#, ay#, 5)
   endif

   if spacekey()=1 and bMissileFlying = 0 then bShooting = 1

   if bShooting = 1:
      ` -- set up missile params
      iCurrentSmoke = 0
      bMissileFlying = 1
      bShooting = 0
      iMissileAge = 0
      ` -- align missile to the gun
      position object 100, px#, py#, pz#
      yrotate object 100,ay#
      show object 100
   endif

   if bMissileFlying = 1:
      ` -- fly the missile
      inc iMissileAge,1
      inc iSmokeInterval,1
      move object 100,iMissileSpeed
      ` -- time to create a smoke? --
      if iSmokeInterval > int(iMissileLife/iNumberSmokes):
         inc iCurrentSmoke, 1
         ` -- make this smoke become alive
         aSmokeAge(iCurrentSmoke) = 1
         ` -- make it appear
         show object 500+iCurrentSmoke
         fade object 500+iCurrentSmoke, 100
         ` -- place it here
         position object 500+iCurrentSmoke, object position x(100), object position y(100), object position z(100)
         ` -- make sure this event only triggers when we want it to
         iSmokeInterval = 0
      endif

   endif

   ` -- age each active smoke --
   for i = 1 to iNumberSmokes
      if aSmokeAge(i) > 0:
         yrotate object 500+i, camera angle y()-180
         inc aSmokeAge(i),1
         position object 500+i, object position x(500+i), object position y(500+i)+0.05, object position z(500+i)
         a# = aSmokeAge(i)
         b# = iSmokeLife
         ` -- not sure why I have to do this here - can't seem to implicity cast to floats
         fade object 500+i, 100-((a#/b#)*100)
         ` -- kill old smokes --
         if aSmokeAge(i) > iSmokeLife:
            hide object 500+i
            aSmokeAge(i) = 0
         endif
      endif
   next i

   ` -- time to kill the missile? --
   if iMissileAge > iMissileLife:
      hide object 100
      bMissileFlying = 0
   endif

   ` -- update boring stuff
   position object 1, px#,py#,pz#
   position light 0,  newxvalue(px#,ay#-180,600), py#+400, newzvalue(pz#,ay#-180,600)
   yrotate object 1, ay#
   set camera to follow px#, py#, pz#, ay#, 50, py#, 5, 0
   point camera px#, py#, pz#

   sync
loop


If you use this in a game and you feel like crediting me, go for it. If you don't, then it's not like you wouldn't have figured this out for yourself after a while...

Enjoy

-- edited -- removed a text() call in the example
Posted: 30th Jun 2003 2:41
Here we are, a coupla tweaks later, and it's in the game:

http://www.fuzzee.co.uk/game/heli/shot10.jpg
Posted: 30th Jun 2003 7:22
that looks good!!

whats global for? never heard of this comand
Posted: 30th Jun 2003 7:52
o_O variables by nature are local... only used in a specific function... but when you make it global ... it can be used within functions and within other functions... =\ I just go ahead and use references... only globals I would make are constant... =P
Posted: 9th Aug 2003 21:25
If you make a global outside a function, call the function, change the global in the function. When you end the function would the global have changed value or would it be out of scope?
Posted: 10th Aug 2003 3:06
KamaKase : why not just try it yourself!

See code below, the variable 'a' is global and if set in a function, it keeps its value outside the function.

Remember also, arrays are global's default and functions automatically have access to all arrays defined outside the functions.

+ Code Snippet
global a

a=0
b=0

sub()

print a
print b
wait key

function sub()
   a=10
   b=10
endfunction
Posted: 10th Aug 2003 16:04
Page not found on the image
Posted: 16th Aug 2003 14:36
Forgot about this post

Changed the links ....
Posted: 5th Sep 2003 19:56
I did, I was just being lazy (I have to turn the other comp on....grrrr).

The links still don't work BTW....and I wanna see!!!
Posted: 5th Sep 2003 22:00
sorry - I posted it so long ago and it looks like I've deleted the image!

Just make a black square (64x64) and splash on some grey in a air-brush like fashion. Use that as the smoke texture and use the code.