public void UpdateBoundingBox(ref BOUNDING_BOX bounds) { for (int v = 0; v < m_ModelVerts.Length; v++) { bounds.Update(m_ModelVerts[v].dxv.X, m_ModelVerts[v].dxv.Y, m_ModelVerts[v].dxv.Z); } }
public void UpdateCameraByBoundingBox(ref BOUNDING_BOX bb, float scale, float speed) { float tx, ty, tz; float fx, fy, fz; float sizex, sizey, sizez, maxdim; //calculate centroid for the "to" vector bb.GetCenter(out tx, out ty, out tz); //calculate appropriate "from" vector sizex = bb.max[0] - bb.min[0]; sizey = bb.max[1] - bb.min[1]; sizez = bb.max[2] - bb.min[2]; maxdim = sizex; if (sizey > maxdim) { maxdim = sizey; } if (sizez > maxdim) { maxdim = sizez; } fx = scale * maxdim; fy = ty; fz = tz; //SpeedControl.m_fSpeed = speed; SetLookAt(new Vector3(fx, fy, fz), new Vector3(tx, ty, tz)); }
public void UpdateCameraByCentroid(BOUNDING_BOX bb) { Vector3 centroid = new Vector3(); bb.GetCentroid(out centroid.X, out centroid.Y, out centroid.Z); SetLookAt(new Vector3(centroid.X - 10, centroid.Y - 10, centroid.Z + 4), centroid); }
/// <summary> /// Ray-Axis Aligned Bounding Box intersection test. (Un-tested) /// </summary> /// <param name="position">Start position of ray.</param> /// <param name="direction">Direction of ray.</param> /// <returns></returns> public static bool RayAABBIntersect(Vector3 position, Vector3 direction, BOUNDING_BOX box) { int whichPlane; bool inside = true; const int NUMDIM = 3; float[] coord = new float[NUMDIM]; //Point of intersection. float[] dir = new float[NUMDIM] { direction.X, direction.Y, direction.Z }; float[] origin = new float[NUMDIM] { position.X, position.Y, position.Z }; float[] minB = box.min; float[] maxB = box.max; float[] maxT = new float[NUMDIM]; float[] candidatePlane = new float[NUMDIM]; Region[] quadrant = new Region[NUMDIM]; /* Find candidate planes; this loop can be avoided if * rays cast all from the eye(assume perpsective view) */ for (int i = 0; i < NUMDIM; i++) { if (origin[i] < minB[i]) { quadrant[i] = Region.Left; candidatePlane[i] = minB[i]; inside = false; } else if (origin[i] > maxB[i]) { quadrant[i] = Region.Right; candidatePlane[i] = maxB[i]; inside = false; } else { quadrant[i] = Region.Middle; } } /* Ray origin inside bounding box */ if (inside) { coord = origin; return(true); } /* Calculate T distances to candidate planes */ for (int i = 0; i < NUMDIM; i++) { if (quadrant[i] != Region.Middle && dir[i] != 0) { maxT[i] = (candidatePlane[i] - origin[i]) / dir[i]; } else { maxT[i] = -1; } } /* Get largest of the maxT's for final choice of intersection */ whichPlane = 0; for (int i = 1; i < NUMDIM; i++) { if (maxT[whichPlane] < maxT[i]) { whichPlane = i; } } /* Check final candidate actually inside box */ if (maxT[whichPlane] < 0) { return(false); } for (int i = 0; i < NUMDIM; i++) { if (whichPlane != i) { coord[i] = origin[i] + maxT[whichPlane] * dir[i]; if (coord[i] < minB[i] || coord[i] > maxB[i]) { return(false); } } else { coord[i] = candidatePlane[i]; } } /* ray hits box */ return(true); }