View Full Version : Particle previous position

04 April 2003, 03:53 PM
Is there a way of getting per particle worldspace position at the previous frame? I need to calculate how much each particle translates per frame in the X direction.

I was thinking something like this:

eval( "getAttr -time (`currentTime -q` - 1) particle1.position");

But it only returns "0"

04 April 2003, 04:27 PM
I'm not entirely sure, but you may need to add per particle attributes if you haven't done that already.

04 April 2003, 04:38 PM
I've got per particle attributes. Everything works fine in current time, but trying to get previous frame values return zero. I really hope there's a way of doing this, one way or the other! :)

04 April 2003, 06:31 PM
Isn't the X component of velocity exactly what you're looking for?

vector $test = particleShape1.velocity;
float $velX = $test.x;

-- Mark

04 April 2003, 08:22 PM
Thank you both for replying!

mark_wilkins: No, I'm afraid that's not it. The final solution was this:

A vector attribute prevPosPP.

Runtime rule:

vector $dist = prevPosPP - position;
float $transX = $dist.x;
: expressions
: expressions
: expressions
///last step of the rule:
prevPosPP = position;

04 April 2003, 08:28 PM
ok, but *why* is velocity not the same as that?

I'm not challenging that it's not, just hoping someone can fill me in. :D

-- Mark

04 April 2003, 11:02 AM
:D Because I needed to know exactly how many units the individual particle moved from last frame. Velocity doesn't give me that. Correct me if I'm wrong.

This has nothing to do with controlling the motion of the particles, but simply making the particles affect another PP expression, controlling spritecycles.

04 April 2003, 04:36 AM
Hi mmi:

As it turns out (I just tested it) Maya calculates the per-particle velocity by dividing the length in seconds of a simulation time step (by default one frame) into the difference in position between the last simulation time step and the current time step.

As a result, your solution and mine yield the same result to as many decimal places as I could test.

In the example below I used the userVector1PP variable instead of making a prevPosPP variable, just out of laziness. Doesn't affect the result.

Here's what I did (in a runtime expression):


// calculate difference in X position your way
vector $dist = position - userVector1PP;
float $transX = $dist.x;

// calculate difference in X position my way
vector $test = velocity;
float $velX = $test.x;

// divide by the number of frames per second
// because velocity is in distance per second,
// not distance per frame
// I've assumed 24 fps, but I could grab the
// value from Maya if I wanted.

float $mytransX = $velX / 24;

// print the results

print ($transX + " " + $mytransX + "\n");

// update the previous position

userVector1PP = position;


Try it! (An important note is that if your simulation time step is less than a frame you'll have to divide by the frequency of the simulation time steps, not the frame rate, in order to get the same result either way.)

-- Mark

04 April 2003, 11:49 AM
Aha! That changes things. Thank you for clarifying that. By dividing it by the fps, like you say, I get the same results.

04 April 2003, 03:23 PM
Hi again. I just noticed something when I tested your solution. If i add expressions to control the movement per particle, which I do, velocity returns 0.

Example of runtime expression:

// Add some movement per particle
vector $move = <<3,0,0>>;
position += $move;

// Difference
print ("Previous position: " + userVector1PP + "\n");
print ("Curent position: " + position + "\n");

// My solution
vector $dist = position - userVector1PP;

// Your solution
vector $test = velocity;
vector $mytransX = $test / 24;

// Returns different results
print ("Difference " + $dist + "\n");
print ("Difference using velocity " + $mytransX + "\n");

userVector1PP = position;

The result is this:

Previous position: 72.11415525 0 0.02283105023
Curent position: 75.11415525 0 0.02283105023
Difference 3 0 0
Difference using velocity 0 0 0

Why is that?

04 April 2003, 07:42 PM
The value of the per-particle velocity attribute is not computed on the fly whenever you query it. Instead, the dynamics engine takes turns with you having a crack at the variables.

So, Maya verifies that there are no forces on your particle and sets position to what the previous frame is, velocity to 0, and acceleration to 0. Then your expression gets a crack at it, and changes the position, but it never gets back to Maya to recompute the velocity before you query it.

Note that your script will produce unusual results anyway because it's dependent on your simulation step size: the particle will move five times faster if your sim is running at five steps per frame.

If you were setting velocity by hand, you'd get updated position for free, but acceleration would always remain 0 for the same reason: velocity's computed from acceleration, but not vice versa.

-- Mark

04 April 2003, 08:37 PM
Thank you for all the insightful info Mark!
Note that your script will produce unusual results anyway because it's dependent on your simulation step size: the particle will move five times faster if your sim is running at five steps per frame.
Actually I don't drive the particles like this, I just use it for testing. That way I know what values to expect, and it's easier to verify that every thing works the way I want it. I'll switch over to a velocity driven version as soon as I transfer my expressions to the actual particle simulations.

CGTalk Moderation
01 January 2006, 09:00 PM
This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.