How to create vector within triangle plane, from perpendiculor vector3

Hi,
I’ve been working with Unity for a few months, but now I’m stuck - beginners Unity books don’t include math primers ;)… I would like to know how to find a Vector3 position (p1) within a triangle in 3D space and I also need to know the distance from a point (p0) to the new point (p1). The new Vector3 needs to be created from a right angle between the triangle and p0 (perpendicular to the triangle). I’d also rather avoid RayCasting as I think that would be overkill and this operation needs to happen often. Note: There is 100% certainty that p0 is perpendicular to the triangle’s underside.

I’m guessing the solution involves using the Vector3.Distance function at the end, but first calculating angles between some of the original vectors - which I can do using this but then I’m clueless. I’ve attached a picture of what I am trying to achieve.alt text

Description and example code snippet would be much appreciated. Natural English explanation is preferable as my Greek is terrible. Alternatively (I’ve read asking for code is bad…?) if there’s a link to a similar post, or simply a broken down list of things I need to do, where I can search more informed, then that would be great, but I have searched on several math sites, programming sites and unity without any luck - I’ve been googling for hours :frowning: made worse as I’m not sure what I’m looking for :wink: ANY Help would be much appreciated, please.

PS - my first post, so if I’ve broken protocol somehow, please let me know. Thanks

Might want to edit your question and fix the broken link to your image. Images really help in this situation. A couple of links to get you started: [Unity's mathematical Plane Class][1] [Math3d in the Unity Wiki][2] I think you might be interested in this two functions in particular from Math3d: PlaneFrom3Points(), SignedDistancePlanePoint(). [1]: https://docs.unity3d.com/Documentation/ScriptReference/Plane.html [2]: http://wiki.unity3d.com/index.php/3d_Math_functions

Edited link, think it works now? I'd assumed uploading images from local drive would mean answers forum would make a local copy automatically, evidently not. Checking out your links...

I have no trouble uploading a local image. Not sure why you hand problems. Your comment came after my solution below, so I'm not sure if you did not see it or if I missed something in your problem. Note about GetDistanceToPoint() or the similar function in Math3D, all the ProjectPoinOnPlane() does to find the point on the plane is to first find the signed distance and then walk the reversed normal back to the plane back that distance to the plane.

1 Answer

1

Given the diagram, the calculation is just ProjectPontOnPlane() availabe in Math3D. The solution in Mathf3D is a bit ‘wordy’ with an unnecessary function call or two. Here is a single function version:

function ProjectPointOnPlane(planeNormal : Vector3 , planePoint : Vector3 , point : Vector3 ) : Vector3 {
	planeNormal.Normalize();
	var distance = -Vector3.Dot(planeNormal.normalized, (point - planePoint));
	return point + planeNormal * distance;
}

Once you have p1 you can do this to get the distance:

var dist = Vector3.Distance(p0, p1);

Or if you look at the code I posted above, ‘distance’ in that code is the distance you are looking for. So if you pull the code out of function form and use it inline, you have the distance. Note it is the signed distance.

@robertbu, you can further reduce the square root overhead by removing the planeNormal.normalized use (just use planeNormal), because you're already calling planeNormal.Normalize() above that.

Time sync problems? been a long day ;) - I did not see your new code before posting, though did refresh. Will study the latest stuff, thanks.

@supernat - yep, as long at the planeNormal coming in is guaranteed to be normalized, you don't need the call. I pulled the function from code elsewhere where the incoming 'normal' was not normalized.

If someone could close this, that would be great. I don't seem to have the super powers to do so myself. I'm also unable to vote, though I have accepted the answer. Thanks.

Clicking on the checkmark next to the answer that answered your question marks it as answered. The answer and icon will be displayed in green, marking it as answered. A question can be closed, but there is no reason if the question is marked as answered.