PDA

View Full Version : Particle previous position


mmi
04-07-2003, 04: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"

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

mmi
04-07-2003, 05: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! :)

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

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

-- Mark

mmi
04-07-2003, 09: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;

mark_wilkins
04-07-2003, 09: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

mmi
04-08-2003, 12:02 PM
: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.

mark_wilkins
04-09-2003, 05: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

mmi
04-09-2003, 12:49 PM
Aha! That changes things. Thank you for clarifying that. By dividing it by the fps, like you say, I get the same results.

mmi
04-09-2003, 04: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?

mark_wilkins
04-09-2003, 08: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

mmi
04-09-2003, 09: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-14-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.