5
$\begingroup$

This is probably a dumb question. But how do I find the nearest point along an edge? I'm not good at vector math and can't figure this out.

So for example, I have a singular object. That object has 3 verts. That's all. Just 3 verts. Arbitrarily placed. From that, I imagine a vector defined by 2 of the verts. Then, I imagine the 3rd vert projected to the nearest point of the vector. That's the position I want. How did I get it? There are a number of explanations on other websites for this, but the math equations still go over my head. Can someone show me how it's expressed in geometry nodes?

I thought it would be as easy as subtracting point A from point B, then projecting point C onto that. But no, the projection snaps down toward the origin point. As the magnitude is relative to the origin of the object I suppose. So I guess I need to add the projection with another vector to get it back up to where it's supposed to be? IDK. I'm lost. enter image description here

$\endgroup$

2 Answers 2

6
$\begingroup$

Given the positions of points A, B, and C, what is the position of point D on line AB such that AB is perpendicular to CD?

One way to solve this is using the dot product. The dot product of two normalized vectors is the length of the projection of one vector onto the other. If we re-orient these vectors such that one is the horizon, the dot product is the length of the shadow cast from the other by a noon-time sun.

enter image description here

Using our three positions, we first create two vectors, AB and AC, and normalize them. If we take the dot product of these two vectors, we get the "shadow" cast by normalized AC on AB (or vice versa.) We can scale AB by the length of that shadow to get the projection.

AC is probably not a unit-length vector. But as AC gets longer, the length of its shadow gets longer as well, in the same proportion. So we multiply the length of the shadow by the length of AC.

We have this shadow as a vector relative to A. We need to add it back to A to get its position relative to the object origin.

I'm creating a line both for CD and AB and joining them so you can see that they are perpendicular.

I do wish there was a "sample nearest edge" node to simplify problems like this (and several others.) But, alas.

$\endgroup$
4
  • 1
    $\begingroup$ Just curious: Why don't you actually project AC onto AB? $\endgroup$
    – quellenform
    Commented Dec 7, 2023 at 8:57
  • 3
    $\begingroup$ this is the quellenform's suggestion: i.imgur.com/AzvujEI.png $\endgroup$ Commented Dec 7, 2023 at 9:56
  • $\begingroup$ This long form solution will help me understand the math behind it all I think. Given time to study it more. Though, I do see why the short solution is more expedient and tighter, I'm glad I got the long form to help me "see" it. $\endgroup$
    – Smeebit
    Commented Dec 7, 2023 at 10:43
  • $\begingroup$ @quellenform Well, because I didn't know that existed :) Thanks; happy to see it was useful to Smeebit anyways. Come to think of it, doing a pair of transforms by - and + A.pos at beginning and end would probably also simplify it a bit (ie, think of it as a different coordinate space.) $\endgroup$
    – Nathan
    Commented Dec 7, 2023 at 17:30
8
$\begingroup$

I would think that this is the easiest way to solve it:

enter image description here

Here I simply project vector $\vec{AC}$ onto vector $\vec{AB}$.

$\endgroup$
2
  • $\begingroup$ Adding A after project here is important. Otherwise, this method only works when A is on the origin. Markus von Broady's image has it $\endgroup$
    – Smeebit
    Commented Dec 7, 2023 at 10:46
  • $\begingroup$ @Smeebit Absolutely right, very well observed! ...that got lost here in a hurry and has of course just been corrected. $\endgroup$
    – quellenform
    Commented Dec 7, 2023 at 12:28

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .