# Face Time

If I have three Vector3 points: p0, p1 and p2, how can I calculate the midpoint of those points AND the normal at that midpoint?

TIA!

First I’d find the midpoint of two of the points. And save that to a temporary point.

``````var tempPoint:Vector3 = Vector3( ((p0.x+p1.x)/2) , ((p0.y+p1.y)/2) , ((p0.z+p1.z)/2) );
``````

Then find the midpoint between the temporary point and the third point.

``````var midPoint:Vector3 = Vector3( ((tempPoint.x+p2.x)/2) , ((tempPoint.y+p2.y)/2) , ((tempPoint.z+p2.z)/2) );
``````

Then simply do the cross product of the vectors p0 → p1 and p2 → p1 to get the normal of the plane.

``````var normal:Vector3 = Vector3.Cross( p0 - p1 , p2 - p1 );
``````

This code is not tested.

Also, you can actually calculate the average of two vectors just by adding them and dividing by 2:-`var midPt = (v1 + v2) / 2;`
This is sometimes clearer than averaging the individual components, depending on what you are doing.

I think this is what you want for mid point–

The previous mid point code probably isn’t what you want–
For triangle ABC
The correct way is to take the midpoint of AB and draw a line to C
Take the midpoint of either BC or AC and draw a line to the opposite vertex.
Find the intersection point

This is the barycenter, or center of gravity

I think you can just do (A+B+C )/3

http://en.wikipedia.org/wiki/Centroid

Thanks, all!

Yeah, there’s a nice function call for RayCast hit points that returns the baryCenter of the struck face. But I’m not casting a ray, thus my problem.

Thanks again, you 3D Math Gods!

Marty: long ago I did just this in “that other product”, how to find the “incenter” of a triangle, if you need any further help (after the guru support above) then let me know and I’ll dig that up. It seems that the old poppy server has finally been taken down…

So, how can I maike sure that the calculated normal is always positive?

There’s a rule that determines which way the normal points. Imagine the two vectors you are crossing to be the hands of a clock. If the first vector would have to sweep around in a clockwise direction to line up with the second (by the shortest route, I mean) then the normal will point out of the clock toward you. If the first vector sweeps round anticlockwise to meet the second, then the normal points away from you. You just have to make sure you put the original vectors in the right order when you pass them to Vector3.Cross.