Disctance Between Obj.and Per-Particle-position

Hi Honoured Forum.

The "distanceBetween"-node only allows me to query the distance between to transform-nodes (to my knowledge).

Is it possible to query the distance between individual particles (i.e. on a per-particle basis) and a transform-node?

Best regards, T.

Hey,

There are probably several ways of doing this.
The way I did it was to include code that exports the distance into a global variable from the particle object's runtime expression.

xform -q -ws -t yourObject
will return the world space coords of the object's pivot

you can access each particle's position via the position attribute (in the runtime expression).

use vector function mag() to give you the magnitude of the vector resulting from subtracting the two positions, and export it into a global float array with the index of the particle's id with something like:

global float[] $distances;
float $pid = particleId;
distances[$pid] = mag($vector);
// where vector is particle position minus object position

of course, if you only need the data within the runtime expression itself, exporting it is not necessary.

hope this helps.

Cheers,

Malcolm

Hi Malcom,

Thanks for your quick and elegant response!!!
Im relative new to expressions, but I tried to get as close as possible to what you suggested with my knowledge, and with minor modifications at least I got the syntax right. But in my example below Im still not able to utlilise the distance between an object and individual particles.
In my following example, the if-statement only took effect when the distance between the object and the emitter (or particle birth-position) was the same as the test-condition.
SO if you (or others) can see what I need to do to extract real per-particle distance, I would be gratefull.

Also, I want to elaborate my example-expression with a linstep-function, so that the (e.g.) radiusPP gradually increases as the object gradually comes closer. Any suggestions on that would also be very welcome.

Example-expression:

//As you can see of my expression, I did not understand how to make use of your "xform -q -ws -t yourObject " suggestion.

When and where in the expression (or elssewhere) should it be situated, and where do I store the resulting data?
//I did use this method instead:
//

vector $objPos = <>;

//Subtracting the object position from the per-particle position.
//

vector $vector = particleShape1.position - $objPos;

//Repeating exactly what you said, except the $distances and $pid variables had to be integers.
//

int $distances[];
int $pid = particleShape1.particleId;
$distances[$pid] = mag($vector);

//Example-statements.
//

if ($distances[$pid] < .4)

particleShape1.radius = .3;

else

particleShape1.radius = .1;

The radius of the particles only got radius .4 when the nurbsSphere1 was less than .4 units from the entire particle-objects position, not the individual particles. Also, they did not get back to radius .1 when the object moved further away than .4 units.

Best whishes, T

perhaps something simple like this..

vector $pos = particleShape1.position;
float $distance = mag <>;
if ($distance < 0.3)
radiusPP = 0.03;
else
radiusPP = 0.01;

..not sure exactly what your after ...what you trying to achieve?

Hey,

What you did with the translate channels is ok as long as the object is not parented to anything with animation. The xform command with -ws returns the world coords of the object.

store it with something like:

float $temp[] = xform -q -t -ws yourObject;
vector $objPos = <>;

Mel doesn't know that the return from the xform statement will only contain three elements, and it will complain if you try to store it into a vector, so you need to temporarily store it in a float array and then manually assign the elements of the array to your vector variable.

If you want to make the radius grow or shrink as it gets closer, you have to use the distance or the inverse of the distance a multiplier of your radiusPP.

radiusPP = 1 / $dist[$pid] ;

would cause your radius to grow as it gets closer. Use whatever tricks you need to normalize/control the precise radius change within this type of formula.
Linstep is not necessary for what I think I understand you are trying to achieve.

Talk to you,

Malcolm.

Thanks again, both of you!

But one thing still remains. Im still not able to find a way to store the distance between a object and EACH individual particle in the particle object, in a per-particle array-attribute. So that I can have individual particles react based on the distance to a given object.

The example with the radius is just for illustration-purposes.

Another example would be to have a object, which also is a goal, to e.g. fly through the particles, and when the distance between a particle falls within a certain range, the particle is attractet to that object (goal-wightPP increases), particles outside the distance range from the object gets goal-wightPP 0 (actually I think there is a individual goal-wight for every object, certainly hope so).

Best regards, T.

I wouldnt use

yeah, do some vector-matrix maths to grab wrld pos rather than resorting to commands (speaking of which, the mag call in my example should have been a function rather than the command! arg. - i'd been up for over 36h, thats my excuse).

if you need this data it in an array..

create float array distPP atrib, then ...

particleShape1.distPP = mag (<>);

if you wanted to hand it to a variable instead ...

float $dist[];
$dist[int(id)] = mag (<>);

Now I think I got the statement to respond on the distance between an object and individual particles, but the if-statement only seems to affect the radius at birth. I.e. when the sphere gets close enough to on particle, a particle which is born at that time gets the change in radius, but I want the radius-increase to apply to the very particle that the sphere got close too.

Any suggestions?

Thanks for your time, T.

//I also added a dynamic per-particle vector-attribute called "particleShape1.dist"
//

vector $objPos = <>;

particleShape1.dist = particleShape1.worldPosition - $objPos;

int $pid = particleShape1.particleId;
float $dist[];
$dist[$pid] = mag(particleShape1.dist);

if ($dist[$pid] < .5)

particleShape1.radius = 1;
$rad[$pid] = particleShape1.radius;
$rad[$pid] = particleShape1.radius * mag(particleShape1.dist);

//The last lines may be unnessesary, I was flirting with a way to get the right particles get the right radius.
//

"the if-statement only seems to affect the radius at birth"

that's because you're using radius instead of radiusPP.

Also...like Campbell showed...its more convenient and probably faster if you create a per-particle attribute to store your distances rather than use a float array.

Edit: oops...was reading too fast...you're already storing your target position in a per-particle array