PDA

View Full Version : Determine Position of Object relative to Arbitrary Plane


TAKADS
01-11-2007, 01:17 PM
Suppose i create a Plane 'pPlane01' , move it to a position tX, tY, tZ, and rotate it rX, rY, rZ.

Now suppose i have a sphere 'pSphere01'.

I need to determine the position of the sphere relative to the coordinate system defined by the above plane. Basically i need to determine on which side the sphere lies relative to the plane - either negative or positive.

So the plane is defined by the transformation matrix:

$A =`xform -q -m pPlane01`;

the Sphere is defined by the transformation matrix:

$B = `xform -q -m pShere01`;


so to convert the sphere from Worldspace to the localSpace of the plane, i need to do:

$A * $B (not $B * $A)

How do i do that?
how can i then extract the relative Y of the sphere (to see if it's negative or postive)

I've looked at various threads regarding matrices on this forum and i don't know if i'm approaching it correctly.

Also how do i use the -m flag in xform in create mode not query. For example i have a transformation matrix $C that i'd like to apply to an object.

i tried this:

$A = `xform -q -m pPlane01`;
$newXform = `xform -a -ws -m $A pSphere01`;

i get an 'error while parsing arguments'


So if anyone can help me out with transformation matrix i'd really appreciate it.

Cheers.

du-j
01-11-2007, 02:33 PM
I am sorry to say that I cant solve the first problem.

Maybe I can answer the second question.

MAYA HELP says:*Note* the matrix is represented by 16 double arguments that are specified in row order.

So try to use it like this :


float $CCCc[] = `xform -q -m pPlane1`;
$newXform = `xform -a -ws -m
$CCCc[0] $CCCc[1] $CCCc[2] $CCCc[3]
$CCCc[4] $CCCc[5] $CCCc[6] $CCCc[7]
$CCCc[8] $CCCc[9] $CCCc[10] $CCCc[11]
$CCCc[12] $CCCc[13] $CCCc[14] $CCCc[15] pSphere1`;
Good Luck! :)

TAKADS
01-11-2007, 04:25 PM
thx du-j for clearing up the confusion about -m flag, but i have yet to resolve the first problem as well.

dr.rastaman
01-12-2007, 03:23 PM
Hello,

I think this is what you need:

global proc string position() {
string $inMesh="pPlane1"; //your plane
string $sphere="pSphere1"; //your sphere
string $nearestPointNode = `nearestPointOnMesh $inMesh`;
float $arrayBuffer[];
vector $inPosition=`xform -q -t -ws $sphere`;
setAttr ($nearestPointNode + ".inPosition") ($inPosition.x) ($inPosition.y) ($inPosition.z);
$arrayBuffer = `getAttr ($nearestPointNode + ".position")`;
vector $closestPoint = $arrayBuffer;
$arrayBuffer = `getAttr ($nearestPointNode + ".normal")`;
vector $closestNormal = $arrayBuffer;
vector $pointVector = $closestPoint - $inPosition;
$sideTest = dot($pointVector, $closestNormal);
if($sideTest >= 0)
{
return "negative";
}
else
{
return "positive";
};
delete $nearestPointNode;
}

position();


Hope it works...

vladimirPopovic
01-13-2007, 05:39 PM
Hi,

you can use something like this:


{

// $selectedObjectsS[2] - selected objects (select just two)
// $parentNameS - name of the first selected object
// $childNameS - name of the first selected object

string $selectedObjectsS[2], $parentNameS, $childNameS;

// $parentWorldPositionF[3] - position of the parent in world space
// $childWorldPositionF[3] - position of the child in world space
// $childParentPositionF[3] - position of the child in parent space

float $parentWorldPositionF[3], $childWorldPositionF[3], $childParentPositionF[3];

// find what is selected (assuming the selection are DAG - transform, nodes)

$selectedObjectsS = `ls -sl`;

// the first selected $selectedObjectsS[0] becomes parent, second child

$parentNameS = $selectedObjectsS[0];
$childNameS = $selectedObjectsS[1];

// where are the parent and child in world space

$parentWorldPositionF = `xform -q -t -ws $parentNameS`;
$childWorldPositionF = `xform -q -t -ws $childNameS`;

// Find the position of child in parent space

$childParentPositionF[0] = $childWorldPositionF[0] - $parentWorldPositionF[0];
$childParentPositionF[1] = $childWorldPositionF[1] - $parentWorldPositionF[1];
$childParentPositionF[2] = $childWorldPositionF[2] - $parentWorldPositionF[2];


//print the result

print $childParentPositionF;
print ($childNameS + "'s position relative to" + $parentNameS + "\n" + "X - " + $childParentPositionF[0] + "\n" + "Y - " + $childParentPositionF[1] + "\n" + "Z - " + $childParentPositionF[2]);

}


hope it helps

Vladimir Popovic

vladimirPopovic
01-13-2007, 05:46 PM
Hm just read your question again and here's my, what do you mean by positive or negative side? The position to the side where the nornal of the surface is lookin at?

Just to clear it up and Ill get back to solving the problem it could be a useful piece of code

Popovic Vladimir

TAKADS
01-13-2007, 10:02 PM
I actually found a way using the Inverse Matrix attribute without complicating things too much. It works perfectly.
Thank you for all who helped.




// The objects in question: Determine pos of Sphere relative to Plane |> either over or under

string $sepObjects = "pSphere1";
string $cordPlane = "pPlane1";

// get inverse Matrix

$plIM = `getAttr $cordPlane.worldInverseMatrix`;

// Reset Pivot point (center it) of object then query it's position in Worldspace.

$ResetPV = `xform -cp $sepObjects`;
$posNew = `xform -q -ws -a -piv $sepObjects`;

// Simple var swapping to avoid confusion

$posX = $posNew[0];
$posY = $posNew[1];
$posZ = $posNew[2];

// get Relative values of Y relative to the plane object space (i only need Y but no harm
// in calculating the others in case someone else needs them)

$newX = $posX * $plIM [0] + $posY * $plIM [4] + $posZ * $plIM [8] + 1 * $plIM [12];
$newY = $posX * $plIM [1] + $posY * $plIM [5] + $posZ * $plIM [9] + 1 * $plIM [13];
$newZ = $posX * $plIM [2] + $posY * $plIM [6] + $posZ * $plIM [10] + 1 * $plIM [14];

// Side test

if($newY == 0) {
print ($sepObjects + " is ON the plane\r");
}

if($newY < 0) {
print ($sepObjects + " is under the plane\r");
}

if($newY > 0) {
print ($sepObjects + " is over the plane\r");
}

TAKADS
01-13-2007, 10:06 PM
hey vladimir,

By negative or positive, i mean determine on which side of the Plane does the sphere lie? The Plane in question cuts the Space into 2: a positive space in the normal direction of the plane and a negative space in the opposite way. I wanted to know in which of the halfs the sphere lies. Anywho, found it (code above), unless someone has an even easier way, although i doubt there could be anything shorter.

Thx

cpan
01-15-2007, 10:20 AM
heya Amine,
do you happen to know what does the matrix holds out in it's first column (x)?

eg
| rotateX . . . |
| scaleX . . . |
| shearX . . . |
| positionX . . . |
?

or more exactly how do you form the matrix if i have a {0, 0, 0} positioned obj, {1,1,1} scale and {rx,ry,rz} rotated object?


thanks!

CGTalk Moderation
01-15-2007, 10:20 AM
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.