Notifications
Clear all

[Closed] Calculating distance particles have travelled

Hi Everyone

I am trying to write a script in 3DS which will calculate and output the distance each of the particles in the system have travelled.

So far i have a script that calculates the distance between two objects which is the basis for the entire script which would have the following logic…

  • get position of particle in current frame and in previous frame
  • calculate the distance between them (3d pythag)
  • add this to a running total
  • move to the next frame

The problem i am having is that i dont know how to access the individual particle’s position information (i only really need the calculated output for one particle) in order to do the appropriate calculations.

However, I am a complete newbie at maxScripting so if someone has a completely different take on how to achieve this then i am all ears!

Cheers
Stuart

4 Replies

I had to visualize the paths of particles recently, so I dumped the particle positions to an array, then built splines from the position data. You can easily get the length of a spline, so that might be a useful and flexible solition.

I wrote these functions:


 		function arrayToSpline arr =
 			(
 			-- spline
 				local ss = SplineShape pos:arr[1]
 				addNewSpline ss
 				
 			-- knots
 				for i = 1 to arr.count do
 					(
 					addKnot ss 1 #corner #line arr[i]
 					)
 				updateShape ss
 				
 			-- return
 				ss
 			)
 
 		function particleTrajectoryToArray pf pIndex =
 			(
 			local arr	= #()
 			for t = 0 to animationrange.end do
 				(
 				pf.updateParticles pf t
 				append arr (pf.getParticlePositionById pIndex)
 				)
 			arr
 			)

So you just need to run something like this:

[left]


arr = particleTrajectoryToArray $pf 1
ss = arrayToSpline arr
curveLength ss

[/left]

It wouldn’t be 100% accurate but how about:


function particleTrajectoryToArray pf pIndex =
(
  pdist = 0
  local prevloc
  for t = 0 to animationrange.end do
  (

	  pf.updateParticles pf t	  
	  if t != 0 do pdist += (distance prevloc (pf.getParticlePositionById pIndex))
	  prevloc = (pf.getParticlePositionById pIndex)

  )
  return pdist
)

Thank you guys very much! That is fantastic.

Ive tested it and modified it a wee bit to suit and its looking great – especially the spline drawing bit, i hadnt thought to include something like that but it will be a great addition to visualize the movement.

One more thing however, is there a way to get the number of particles emmitted so i can loop through them all and get the data for each of them, without having to input the indexes individually. I know i could just loop through the number i know i have set but it seems very clumsy and not very portable so i’m sure there is a way!

Thanks again, much appreciated
Stuart

Hey Stuart,

Happy to help =)

You know all this information is in the MaxScript help, don’t you? Look at the last but 1 topic in the contents, MacScript Extensions, then the Partice Flow topic, and it’s all in there. Might take some getting to know, but it is all there.

In the meantime: $pf.numParticles()