Hi all!
So I know that Baxslash has come up with a very cool system for this, but I thought I'd share what I came up with tonight for those wanting to implement a very simplistic 3D particles system into their game. The code is pretty simple. It only supports up to 1000 particles at a time, but that's easily expandable. I just don't have a need for more than that in my game.
So firstly, there's the global declaration to hold the particle info:
global dim Particles#[1000, 8]
`1) Obj index
`2) X angle
`3) Y angle
`4) Z angle
`5) Speed
`6) Spin
`7) Gravity
`8) Life
Then, there's the manager function. Stick a call to this in your main game loop to manage any existing particles:
function ProcessParticles()
for p = 1 to 1000
if (Particles#[p, 1] > 0.0)
SetObjectRotation(Particles#[p, 1], Particles#[p, 2], Particles#[p, 3], Particles#[p, 4])
SetObjectPosition(Particles#[p, 1], GetObjectX(Particles#[p, 1]), GetObjectY(Particles#[p, 1])-Particles#[p, 7], GetObjectZ(Particles#[p, 1]))
MoveObjectLocalZ(Particles#[p, 1], Particles#[p, 5]*-1)
SetObjectLookAt(Particles#[p, 1], GetCameraX(1), GetCameraY(1), GetCameraZ(1), 0)
Particles#[p, 8] = (Particles#[p, 8] - 1.0)
Particles#[p, 6] = Particles#[p, 6] + 1.0
if (Particles#[p, 8] <= 0.0)
DeleteObject(Particles#[p, 1])
for pd = 1 to 8
Particles#[p, pd] = 0.0
next
endif
endif
next
endfunction
Then, there is of course the function to produce a particle. I'm sure you could also use this to create some sort of "particle source" system, too.
function SpawnParticle(particleX#, particleY#, particleZ#, Speed#, Size#, angleX#, angleY#, angleZ#, Spin#, DriftAngle, Gravity#, Life, TextureIndex)
for p = 1 to 1000
if (Particles#[p, 1] = 0.0)
Particles#[p, 1] = CreateObjectPlane(Size#, Size#)
SetObjectPosition(Particles#[p, 1], particleX#, particleY#, particleZ#)
SetObjectImage(Particles#[p, 1], TextureIndex, 0)
SetObjectAlphaMask(Particles#[p, 1], 1)
Particles#[p, 2] = angleX#+Random(((DriftAngle/2)*-1), (DriftAngle/2))
Particles#[p, 3] = angleY#+Random(((DriftAngle/2)*-1), (DriftAngle/2))
Particles#[p, 4] = angleZ#+Random(((DriftAngle/2)*-1), (DriftAngle/2))
Particles#[p, 5] = Speed#
Particles#[p, 6] = Spin#
Particles#[p, 7] = Gravity#
Particles#[p, 8] = Life
exit
endif
next
endfunction
Creating a particle with this should be pretty self explanatory, but here are the details:
ParticleX# - X starting position of the particle in the game world
ParticleY# - Y starting position of the particle in the game world
ParticleZ# - Z starting position of the particle in the game world
Speed# - The speed at which the particle moves
Size# - The size of the particle billboard
angleX# - X angle of direction at which the particle travels (it will always face the camera)
angleY# - Y angle of direction at which the particle travels (it will always face the camera)
angleZ# - Z angle of direction at which the particle travels (it will always face the camera)
Spin# - The starting rotation of the particle. Spin speed is constant but that can be modified if you really want to control the spin speed
DriftAngle - The amount of chaos in the projection angle of the particle. Set this to a big number to create "puffs" of smoke, or a low number to create a "jet" stream of particles
Gravity# - Positive numbers make particles sink, negative numbers make particles float
Life - How many ticks the particle will live for before de-spawning, making room for future particles
TextureIndex - The image reference number of the texture file to use for this particle
Anyways, I know it's very simple but I hope someone can get some use out of it!