afAABBox.h
资源名称:AirForce.rar [点击查看]
上传用户:kaiguan
上传日期:2007-10-28
资源大小:1074k
文件大小:5k
源码类别:
其他游戏
开发平台:
Visual C++
- #ifndef AF_AABBOX
- #define AF_AABBOX
- #include "afVec3.h"
- #include "afMathTool.h"
- #include "afPlane.h"
- #include <math.h>
- /// Axis aligned bounding box
- /**
- * An axis aligned bounding box can be calculated
- * very effectivly. A AABBox is defined by only two
- * vertices (corners).
- */
- class afAABBox // axis aligned bounding box
- {
- public:
- // constructors
- afAABBox() { reset(); }
- afAABBox(const afVec3& nMin, const afVec3& nMax) : minVertex(nMin), maxVertex(nMax) {}
- afAABBox(const afVec3& nMid, float dx, float dy, float dz) : minVertex(nMid[0]-dx/2.0f, nMid[1]-dy/2.0f, nMid[2]-dz/2.0f),
- maxVertex(nMid[0]+dx/2.0f, nMid[1]+dy/2.0f, nMid[2]+dz/2.0f)
- {}
- /// Sets one corner of the bounding box
- void setMin(const afVec3& nVertex) { minVertex = nVertex; }
- /// Sets another corner of the bounding box
- void setMax(const afVec3& nVertex) { maxVertex = nVertex; }
- /// Extends the box to contain the given vertex
- inline void addVertex(const afVec3& nVertex);
- /// Extends the box to contain an other box
- inline void addBox(const afAABBox& nOther);
- // Uncreases the box, so that it would fit, if it was rotated by nAngle
- inline void exentedByRotationY(float nAngle);
- /// Calculates and returns the center of the box
- afVec3 getCenter() const { return (maxVertex+minVertex)*0.5f; }
- /// Moves the box by nMove
- void move(const afVec3& nMove) { minVertex += nMove; maxVertex += nMove; }
- /// Resets the box to contain everything
- void
- reset()
- {
- maxVertex = afVec3(afFLOAT_MIN,afFLOAT_MIN,afFLOAT_MIN);
- minVertex = afVec3(afFLOAT_MAX,afFLOAT_MAX,afFLOAT_MAX);
- };
- /// Resets the box to be centered at nMid and to have each side of length d
- void
- setMid(const afVec3& nMid, float d)
- {
- setMid(nMid, d,d,d);
- }
- /// Resets the box to be centered at nMid and to have size dx,dy,dz
- void
- setMid(const afVec3& nMid, float dx, float dy, float dz)
- {
- minVertex = afVec3(nMid[0]-dx/2.0f, nMid[1]-dy/2.0f, nMid[2]-dz/2.0f);
- maxVertex = afVec3(nMid[0]+dx/2.0f, nMid[1]+dy/2.0f, nMid[2]+dz/2.0f);
- }
- /// returns one point of the box
- const afVec3&
- getMin() const { return minVertex; }
- /// returns the other point of the box
- const afVec3&
- getMax() const { return maxVertex; }
- /// returns the center of the box
- afVec3
- getMid() const { return afVec3(minVertex[0] + (maxVertex[0] - minVertex[0]) / 2.0f,
- minVertex[1] + (maxVertex[1] - minVertex[1]) / 2.0f,
- minVertex[2] + (maxVertex[2] - minVertex[2]) / 2.0f); }
- // testing functions
- //
- inline bool
- doesOverlap(const afAABBox& nOther) const;
- protected:
- afVec3 minVertex,
- maxVertex;
- };
- inline void afAABBox::addVertex(const afVec3& nVertex)
- {
- // is there a new max
- if (maxVertex[0] == afFLOAT_MIN)
- maxVertex = nVertex;
- else
- {
- if (nVertex[0] > maxVertex[0])
- maxVertex[0] = nVertex[0];
- if (nVertex[1] > maxVertex[1])
- maxVertex[1] = nVertex[1];
- if (nVertex[2] > maxVertex[2])
- maxVertex[2] = nVertex[2];
- }
- // is there a new min
- if (minVertex[0] == afFLOAT_MAX)
- minVertex = nVertex;
- else
- {
- if (nVertex[0] < minVertex[0])
- minVertex[0] = nVertex[0];
- if (nVertex[1] < minVertex[1])
- minVertex[1] = nVertex[1];
- if (nVertex[2] < minVertex[2])
- minVertex[2] = nVertex[2];
- }
- }
- inline void afAABBox::addBox(const afAABBox& nOther)
- {
- addVertex(nOther.maxVertex);
- addVertex(nOther.minVertex);
- }
- inline bool afAABBox::doesOverlap(const afAABBox& nOther) const
- {
- return !(minVertex.x>nOther.maxVertex.x || nOther.minVertex.x>maxVertex.x ||
- minVertex.y>nOther.maxVertex.y || nOther.minVertex.y>maxVertex.y ||
- minVertex.z>nOther.maxVertex.z || nOther.minVertex.z>maxVertex.z);
- }
- inline void afAABBox::exentedByRotationY(float nAngle)
- {
- float cosA = (float)cos(nAngle),
- sinA = (float)sin(nAngle);
- int i;
- afVec3 corners[8], dest;
- corners[0].set(minVertex.x, minVertex.y, minVertex.z);
- corners[1].set(maxVertex.x, minVertex.y, minVertex.z);
- corners[2].set(minVertex.x, maxVertex.y, minVertex.z);
- corners[3].set(maxVertex.x, maxVertex.y, minVertex.z);
- corners[4].set(minVertex.x, minVertex.y, maxVertex.z);
- corners[5].set(maxVertex.x, minVertex.y, maxVertex.z);
- corners[6].set(minVertex.x, maxVertex.y, maxVertex.z);
- corners[7].set(maxVertex.x, maxVertex.y, maxVertex.z);
- for(i=0; i<8; i++)
- {
- dest.x = corners[i].x * cosA + corners[i].z * sinA;
- dest.y = corners[i].y;
- dest.z = -corners[i].x * sinA + corners[i].z * cosA;
- addVertex(dest);
- }
- }
- #endif