Proceed With Integrate Script_Test Example

Description:

The following example shows the usage of the Integrator interface in a Script_Test action.

Can Be Used With:

Should be used inside a Script_Test : Helper action.

Effect:

Shows the necessity of using the Integrator. Without the Integrator particles are stopped somewhere around the zero Z plane. The Integrator is able to stop particles exactly at the plane level (without any modification of the particle position).

In the example flow, the particles are born and accelerated. The Script_Test checks their position and if they pass the scripted test, they are moved to the second event which stops them.

EXAMPLE

   --The ChannelsUsed handler defines the channels
   --to be made available to the script.
   --For the list of all channels, see
   --Interface: MaxscriptParticleContainer
   --The parameter pCont passed to the handler
   --contains the Particle Container the script is applied to
   on ChannelsUsed pCont do
   (
   pCont.useTime = true --enable the Time channel
   pCont.usePosition = true --enable the Position channel
   pCont.useSpeed = true --enable the Speed channel
   )

   --The Init handler is called on initialization.
   --It is not used in this case.
   on Init pCont do
   (

   )

   on Proceed pCont do
   (
   --Get the End Time
   end = pCont.GetTimeEnd() as float
   --Get the interface to the Integrator
   _int = pCont.GetIntegrator()
   --Get the current particle count
   count = pCont.NumParticles()
   --Get the particle container
   pCont_curr = pCont.GetParticleContainer()
   --Initialize Arrays
   timeToStopInt = #()
   timeToStopFloat = #()
   particlesToStop = #{}
   --Initialize a flag to signal when
   --there are particles to send to the integrator
   hasParticlesToIntegrate = false

   --Go through all particles
   for i in 1 to count do
   (
   --Set the current particle index
   pCont.particleIndex = i
   --Calculate the remaining time to travel
   timeForTravel = end - (pCont.particleTimeasfloat)
   -- format "time to travel % = %\n" i timeforTravel
   --Get the current position and speed of the particle
   curPos = pCont.particlePosition
   curSpeed = pCont.particleSpeed

   --Calculate the time to the XY plane. Because of the minus sign,
   --when the Z coordinate is negative, the time will be positive
   timeToPlane = -curPos.z / curSpeed.z
   -- format "time to plane % = %\n" i timeToPlane

   --Initialize a flag
   stopThisParticle = 0
   --Get the particle time in frames
   curParticleTime = pCont.particleTime as time

   --If the time to plane is positive or zero and shorter
   --than the time to travel, then the particle has passed
   --the plane already and has to be stopped!
   if((timeToPlane >= 0.0)and(timeToPlane <= timeForTravel))do
   (
   --Set the Test status to true
   pCont.particleTestStatus = true
   --Set the precise particle time
   pCont.setParticleTestTimePrecise i pCont.particleTime timeToPlane
   --Append the particle index to the BitArray of particles
   --to be stopped
   append particlesToStop i
   --Raise the flag to signal there is work for the integrator
   hasParticlesToIntegrate = true
   )

   --Append the particle time frame and fraction to the two arrays 
   append timeToStopInt curParticleTime
   append timeToStopFloat timeToPlane
   )

   --If the flag has been set 
   if hasParticlesToIntegrate then
   (
   --If the integrator can be accessed, send the particles to stop
   --to the Integrator
   if _int != undefined then
   _int.proceedASync pCont_curr timeToStopInt timeToStopFloat true particlesToStop
   )
   -- format "time to stop Int= %\n" timeToStopInt
   -- format "time to stop Float= %\n" timeToStopFloat
   -- format "part to stop Index= %\n" particlesToStop
   )

   --The Release handler is used to do cleanup work.
   --Not used in this case.
   on Release pCont do
   (
   )