View Full Version : Find position between 2 points?

 AtrusDni02 February 2009, 03:36 AMOk so here my dilemma (shoulda paid attention in math class more ...) Say i have 2 locators, and I query their worldspace translate x,y,z. Now, using only mel and math how would I - 1) Find the center point between both locators? The world position xyz? 2) Align an object from this point to one of the other points, like an aim constraint? 3) Also offset this point, so if the middle is 50 %, how would I find the point at 40%? Any information would be much appreciated. Thanks guys.
NolanSW
02 February 2009, 05:14 AM
Something that can done is have your script create a 2 point ep curve at each position of your 2 locators. Then simply grab the pointPosition of the curve.u[value]. So if you wanted the half way point you would do something like `pointPosition curve1.u[0.5]`. This will get you the 50% position. As for the aiming, you could have each locator aim at each other(delete the constraint when your done). Then for each new transform you create along the curve take the orientation of the either aimed locator and apply that to each new transform you create.

Hope that was clear.

AtrusDni
02 February 2009, 05:28 AM
Thanks for the info Nolan. I could do this all using constraints, but I think for what im trying to do it would slow down way too much (like thousands of constraints), and I think just using math would be more efficient and cleaner. I have however figured out the midpoint thing, so heres the code for it:

spaceLocator -p \$aPos[0] \$aPos[1] \$aPos[2];
spaceLocator -p \$bPos[0] \$bPos[1] \$bPos[2];
spaceLocator -p ((\$aPos[0] + \$bPos[0])/2) ((\$aPos[1] + \$bPos[1])/2) ((\$aPos[2] + \$bPos[2])/2);

so its ((X1 + X2)/2), ((Y1 + Y2)/2), ((Z1 + Z2)/2), for the X, Y, and Z values. That works so now I need to figure out how to offset this, so maybe its not in the exact middle, but maybe closer to one point or another. I also still need to figure out the aim.

r4inm4ker
02 February 2009, 05:40 AM
suppose the two points are \$p1[3] and \$p2[3].

3.

{
float \$p1[3] = {0,1,2};
float \$p2[3] = {4,5,6};

//convert to vector
vector \$v1 = {\$p1[0],\$p1[1],\$p1[2]};
vector \$v2 = {\$p2[0],\$p2[1],\$p2[2]};

//calculate direction vector
vector \$d = \$v2-\$v1;

//normalize
\$d = \$d/mag(\$d);

//suppose you want 40% from point 1
float \$n = 0.4;

//calculate new position
vector \$newV = \$v1 + (\$n * \$d);

//convert back to float
float \$newP[] = {\$newV.x,\$newV.y,\$newV.z};
print \$newP;

}

the aim vector is \$d. I guess that's the vector you want for question no.2.

Hope this helps..

greatPumpkin
02 February 2009, 05:44 AM
my math is pretty awful, but to get the angle i think you can use atan2d on x and y, and then atan2d on y and z to get two rotation values to give you basic orienation as well-?

AtrusDni
02 February 2009, 07:59 AM
Awesome guys, thanks for the help! I will have to give these a go tomorrow.

scroll-lock
02 February 2009, 08:09 AM
or simply point constraint a third locator to the both, delete the constraint and query the new locator's world pos. You can play with the weights too. I saw you don't want doing it with constraints now. Sorry.
My suggestion is to check for linear interpolations or barycentric coordinates. There are some easy formulas on the math sites.

CGTalk Moderation
02 February 2009, 08:09 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.

1