Sunday, July 24, 2022
HomeGame Developmentunity - How you can discover vector representing altitude of a triangle?

unity – How you can discover vector representing altitude of a triangle?


Given the enter vectors

   Vector3 AB =  Vector3(Bx, By, Bz) - Vector3(Ax, Ay, Az);
   Vector3 Caxis = Vector3(Cx, Cy, Cz) - Vector3(Ax, Ay, Az); // That is the vector from A to C

The path of the end result vector C we search is identical path as (AB cross Caxis) cross AB

   Vector3 Cdir = Vector3.Cross(Vector3.Cross(AB, Caxis), AB).Normalize();

Discovering the magnitude is extra concerned. We all know that beginning on the level Cz and going within the path Cdir should intersect alongside AB

   Caxis - alpha * Cdir == beta * AB

and we have to discover unknowns alpha and beta. We will discover a vector perpendicular to Caxis by projecting AB onto the airplane to which Caxis is regular

   Vector3 Cperp = Vector3.ProjectOnPlane(Caxis, AB).Normalize();

so our equation with alpha and beta might be diminished to 1 unknown

  - alpha * (Cdir . Cperp)/(AB . Cperp) == beta

So fixing for alpha (which is the truth is the magnitude of our remaining vector C)

   Caxis == alpha * (Cdir - AB* (Cdir . Cperp)/(AB . Cperp) )

It is a vector equation, however we will clear up for alpha by taking the dot product with Caxis on either side. Writing code once more that is

   float num = Caxis.magnitude*Caxis.magnitude;
   float denom1 = Vector3.Dot(Cdir, Caxis);
   float denom2 = Vector3.Dot(AB, Caxis) * Vector3.Dot(Cdir, Cperp) / Vector3.Dot(AB, Cperp);

   float Cmag = num/(denom1 - denom2); 

C is then, utilizing the signal conference out of your diagram,

   Vector3 C = Cmag*Cdir;

I am leaving it with out checks for instances the place you get a divide by zero, akin to when factors B and C are equivalent.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments