Download

Verticalize Triangle

Hey!

I have a triangle where two points have the same Z-Value and one has a different Value. Now I want to transform the point with the differenz Z-Value, so that it optically generates a “vertical” Triangle. Assuming point C is the one with the different height-Value, I somehow need to move the X and Y Coordinates of point C orthogonal to the Difference-Vector of A and B, until they vertically line up, eg the slope is exactly 90 degrees. But unfortunaly, I am a complete idiot concerning rotations and stuff. Could you give me any hints how to solve that? Preferably a rather quick calculation, because it has to be called up to 700000 times per player, per chunk load :stuck_out_tongue:

Thanks in advance :slight_smile:

Edit: Thats certainly NOT how to do it :rolleyes:
wr.PNG

Okay, I already received a answer :slight_smile: If someone else ever wonders how to do it:

Let’s say you have points A B C, and A.z == B.z, and z represents the vertical direction.

First, project the x,y co-ordinates of C onto the line between A and B in 2D:

// find normalized AB vector:
AB.x = B.x - A.x;
AB.y = B.y - A.y;
length = sqrt(AB.x * AB.x + AB.y * AB.y);
// test for length == 0 to avoid div by zero
AB.x /= length;
AB.y /= length; // note: you could save a division by dividing dot by length instead

// project C onto AB:
AC.x = C.x - A.x;
AC.y = C.y - A.y;
// this gives us how far along the line AB the projected point is:
dot = (AC.x * AB.x) * (AC.y * AB.y);
newC.x = A.x + (AB.x * dot);
newC.y = A.y + (AB.y * dot);
newC.z = A.z; // same as B.z
Next find the 3D distance between the projected point and C, which will be the vertical height above the AB line of the new point, if the triangle were rotated into a vertical position using AB as a hinge:

newCC.x = C.x - newC.x;
newCC.y = C.y - newC.y;
newCC.z = C.z - newC.z;
height = sqrt((newCC.x * newCC.x) + (newCC.y * newCC.y) + (newCC.z * newCC.z));
Set the z value:

newC.z += height;

source@stackOverflow:samgak