The square of the hypotenuse is equal to…

I’m once again limited by my ability to transfer a concept from paper to computer.

I’m working on a game where I need to give the user the ability to draw boxes. Right now, I’ve got it working to where you can drag on the background and create meshes as in the screenshot above. My problem is that right now, the vertices on the bottom move straight down along the y-axis, so the different boxes that are drawn have different heights.

So basically, what I need to do is figure out a way to place a third vertex(the bottom left) so that it forms a right angle to the other two vertices(the top left and top right). Here’s some pseudocode to illustrate.

var blockHeight : float = 1.0; //the distance between the top layer of vertices and the bottom layer

var topLeftVertex : Vector3 = oldMousePos; //the point that the mouse was at when the drag started
var topRightVertex : Vector3 = newMousePos; //the point that the mouse is at after the drag
var bottomLeftVertex : Vector3 = topLeftVertex; //NEEDS FIXING
bottomLeftVertex.y -= blockHeight;

So in the code above, depending on the y of the mouse before and after the drag, the actual height of the block may change. So I need to have a right triangle formed by the three vertices in the above code. So I:

var firstLegLength : float = Vector3.Distance(topLeftVertex, topRightVertex);
var hypotenuse = squareroot((blockHeight * blockheight) + (firstLegLength + firstLegLength));
[code]

Now I've got the distance between the top right vertex and the bottom left vertex. Now while working on paper, I would use a compass to draw curves of the two distances, and place my point where the curves intersect. How would I do this in Unity?

(Hoping this isn't entirely opaque)

-Lincoln Green[/img]

OK, I just thought up a much simpler way of putting this: How do I create a vector that is a specified distance from 2 points?

I’ll assume your boxes are all in the XY plane and

Subtract one point from the other to get a vector:-

var offset: Vector3 = ptA - ptB;

Get the cross product of this vector with Vector3.forward and normalise it:-

var perp: Vector3 = Vector3.Cross(offset, Vector3.forward).normalized;

The “perp” vector is perpendicular to the line between ptA and ptB and of length 1. Now, get the mid point between ptA and ptB:-

var midPt: Vector3 = (ptA + ptB) * 0.5;

Now, normalise the perp vector, scale it and add it to the mid point:-

var cornerA: Vector3 = midPt + (perp * distance);
var cornerB: Vector3 = midPt - (perp * distance);

Thanks for the reply!
I’m not acquainted with cross products - I’ll have to do some googling for that. But I do have a question regarding the code: What exactly is the ‘distance’ variable?

Hello!
After plugging your code into my code, the result was less than satisfactory - it ended up with one of the corners above the line and one below the line. I fixed this by changing the code to:

var lowerP1: Vector3 = midPt + (perp * distance); 
var lowerP2: Vector3 = midPt + (perp * distance);

Which resulted in an isosceles triangle(the two points were in the same place). Fixed this by adding:

lowerP1.x -= (midPt.x - p1.x);
lowerP1.y -= (midPt.y - p1.y);
lowerP2.x -= (midPt.x - p2.x);
lowerP2.y -= (midPt.y - p2.y);

Which works great! Thanks for the help.