View Full Version : Trying to find the distance an object is from a surface.

 CraigHorne11 November 2006, 01:19 PMtrying to find the distance an object is from a surface. so i am looking for a distance from surface node. not the distance from one object to another. it must be from surface of one object to the surface of another object. thanks in advance. Craig
ljilekor
11 November 2006, 01:51 PM
Don't know how u should do it but that's a really nice one!

I guess there must be some raycasting involved... ??? BTW can I cast Rays in MEL? If yes, does the function return an object list and/or intersectiondata (worldpos / normal / polyID / ...)

Just curious to see the answer to that one.

CraigHorne
11 November 2006, 02:02 PM
yea, i have always wanted to try and work out how to create a few virtual sensors equal to the real thing. the first one i could think of was the distance sensor. but all the distanc from nodes, seem to calculate the distance from the centre of the objects. thanks for your interest in this post. its all about robotics and A.I.

craig

ljilekor
11 November 2006, 03:54 PM
I don't know if there's a 'terrainfollower.mel' or something. A MEL that forces an object to folow the topology of a surface (somekind of raycaster).

But if there is I think you can find some usefull code in it for ur tool.

grantimus
11 November 2006, 07:09 PM
try this:

global proc gtClosestPointsOnSurfaces(int \$iterations) {
string \$objs[] = `filterExpand -sm 12`;
string \$CPOM1;
string \$CPOM2;
float \$pos1[],\$pos2[];

if(size(\$objs)!=2)
error("You must have exactly two polygonal objects selected to run this tool");
if(`pluginInfo -q -l closestPointOnMesh` == 0)
select -clear;
select -r \$objs[0];
\$CPOM1 = `closestPointOnMesh`;
select -r \$objs[1];
\$CPOM2 = `closestPointOnMesh`;
\$pos1 = `xform -q -ws -t \$objs[1]`;
\$pos2 = `xform -q -ws -t \$objs[0]`;
setAttr (\$CPOM1 + ".inPosition") \$pos1[0] \$pos1[1] \$pos1[2];
setAttr (\$CPOM2 + ".inPosition") \$pos2[0] \$pos2[1] \$pos2[2];
for(\$i=0;\$i<\$iterations;\$i++){
\$pos1 = `getAttr (\$CPOM2 + ".position")`;
\$pos2 = `getAttr (\$CPOM1 + ".position")`;
setAttr (\$CPOM1 + ".inPosition") \$pos1[0] \$pos1[1] \$pos1[2];
setAttr (\$CPOM2 + ".inPosition") \$pos2[0] \$pos2[1] \$pos2[2];
}
\$pos1 = `getAttr (\$CPOM2 + ".position")`;
\$pos2 = `getAttr (\$CPOM1 + ".position")`;
distanceDimension -sp \$pos1[0] \$pos1[1] \$pos1[2] -ep \$pos2[0] \$pos2[1] \$pos2[2];
delete \$CPOM1;
delete \$CPOM2;
}

Select two polygonal objects and then run the command, gtClosestPointsOnSurfaces(number of iterations you want to run). The higher the number of iterations you run, the more accurate the result will be. For objects of similar sizes 5 or 6 iterations should be fine. But if you have something like a small object above a very large plane you might need 20 - 30 iterations.

grantimus
11 November 2006, 08:18 PM
if you just want the distance between an object's pivot and another surface then the problem becomes a lot more simple:

global proc float[] gtClosestPointOnSurface(string \$source, string \$surface) {
string \$CPOM;
float \$sourcePiv[] = `xform -q -ws -t \$source`;
float \$results[];

if(`pluginInfo -q -l closestPointOnMesh` == 0)
\$CPOM = `closestPointOnMesh \$surface`;
setAttr (\$CPOM + ".inPosition") \$sourcePiv[0] \$sourcePiv[1] \$sourcePiv[2];
\$results = `getAttr (\$CPOM + ".position")`;
\$results[3] = sqrt(pow((\$sourcePiv[0]-\$results[0]),2) + pow((\$sourcePiv[1]-\$results[1]),2) + pow((\$sourcePiv[2]-\$results[2]),2));
delete \$CPOM;
return(\$results);
}

This procedure calculates the closest point on the surface object to the pivot of the source object. It returns a float array with the x,y,z values of the closest point and the distance between that point and the source objects pivot.

grantimus
11 November 2006, 08:28 PM
One more thing, ljilekor asked if you can do raycasting in MEL. You can with this plugin, http://www.highend3d.com/maya/downloads/plugins/polygon/2174.html. Versions of this plugin for Maya 7 and 8 can be found in this thread: http://forums.cgsociety.org/showthread.php?t=421588.

Aright, I'll shut up and stop flooding this thread now.

CraigHorne
11 November 2006, 12:28 PM
Thank you for all the great ideas. i have just loged on again, and i am going to spend tonight testing it all out.

so this is what i have been testing. fire a particle towards a collision object. when the particle dies, you extract the time it took from particle creation to particle death, and that will give you the time for:

Speed x time \ 60 = distance ( of emitter to the surface that the particle died)

Speed is (Basic Emission Speed Attributes in emitter properties.)

can someone confirm that for me.

mad hey? cant be sure if its even on the correct path. so i will try your sugestions first.

kind regards

craig

CraigHorne
11 November 2006, 01:56 PM
well i have tested the scripts that grantimus has posted. and wow, it works great. i still need to find a way to allow the script to run on every frame, set an objects y translate, and delete the locaters, before running it again on the following frame.

but wow. this has been a real eye opener. and thanks for all the help so far. i have a few more ideas that i would like to prapose in the future i.e. ( how can you let an object follow a white line on a plane) and definately interested in how to measure distance using particle collision death.

thanks again

Craig

CraigHorne
11 November 2006, 02:59 PM
i would like to ask a feeble question. what is the best way to view a variable in your scene once you have run a procedure.

thanks

craig

faultymoose
11 November 2006, 11:26 PM
If you want to just view the contents of a variable for debugging purposes, add a line in your code like:

print ("The value of myVariable is:"+\$myVariable+"\n");

This will print the results into the script editor.

CraigHorne
11 November 2006, 07:36 AM
thanks for the help . much appreciated

ljilekor
11 November 2006, 01:56 PM
One more thing, ljilekor asked if you can do raycasting in MEL. You can with this plugin, http://www.highend3d.com/maya/downl...lygon/2174.html (http://www.highend3d.com/maya/downloads/plugins/polygon/2174.html). Versions of this plugin for Maya 7 and 8 can be found in this thread: http://forums.cgsociety.org/showthread.php?t=421588.

Thx a lot. This opens whole new spectrums of pain. :buttrock:

grantimus
11 November 2006, 08:07 PM
Yeah it does. This plugin is by far the most useful mel extension I have ever used. Why something like this doesn't ship with Maya I don't know. It is pretty damned useful.

CGTalk Moderation
11 November 2006, 08:07 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.

1