- Home /
How do I find the centroid of 4 points in 3D space?
First a little background. I'm trying to define bounds for a 3D race track. I have 4 3D meshes that form a rectangular tube (It's always going to be a rectangular shape). I'm casting rays from the character Up, Down, Left and Right.
RaycastHit l = CastRay (m_FollowObject, Vector3.left);
RaycastHit r = CastRay (m_FollowObject, Vector3.right);
RaycastHit u = CastRay (m_FollowObject, Vector3.up);
RaycastHit d = CastRay (m_FollowObject, Vector3.down);
Using the l.point, r.point, u.point, and d.point I want to find the centroid so that I can figure out where the center is to properly place the camera. Does anyone know how to do that?
Answer by Kiloblargh · Dec 22, 2012 at 08:42 PM
c= Vector3((l.point.x + r.point.x )/2,(u.point.y+d.point.y)/2,m_FollowObject.position.z)
?
Tried that out. It works for a simple test case, but I'm not sure it will work when the track is no longer pointing down the Z axis.
Would averaging all of the x's, all of the y's, and all of the z's be the proper thing to do?
Vector3 result = (r.point + l.point + u.point + d.point) / 4.0f;