TrinityCore
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
G3D::Box2D Class Reference

#include <Box2D.h>

Public Member Functions

 Box2D (const Vector2 &center=Vector2(0, 0), float w=0, float h=0, float angle=0)
 
 Box2D (const AABox2D &b)
 
 Box2D (const Vector2 &min, const Vector2 &max)
 
 Box2D (const CFrame &frame, Box2D &b)
 
bool contains (const Vector2 &v) const
 
const Vector2extent () const
 Distance from corner(0) to the next corner along the box's local axis a. More...
 
const Vector2axis (int a) const
 Unit length vector along axis a. More...
 
float area () const
 Surface area. More...
 
const Vector2corner (int i) const
 
const Vector2center () const
 
bool overlaps (const Box2D &other) const
 

Private Member Functions

bool overlaps1Way (const Box2D &other) const
 
void computeAxes ()
 

Private Attributes

Vector2 m_corner [4]
 
Vector2 m_axisin [2]
 
Vector2 m_axis [2]
 
Vector2 m_center
 
float origin [2]
 
float m_area
 
Vector2 m_extent
 

Detailed Description

2D oriented box [http://www.flipcode.com/archives/2D_OBB_Intersection.shtml]

Constructor & Destructor Documentation

G3D::Box2D::Box2D ( const Vector2 center = Vector2(0, 0),
float  w = 0,
float  h = 0,
float  angle = 0 
)
Parameters
centerWorld-space center
wWidth along object-space x-axis
hHeight along object-space y-axis
angleCounter-clockwise angle from object-space x-axis in radians
75  {
76  Vector2 X( cos(angle), sin(angle));
77  Vector2 Y(-sin(angle), cos(angle));
78 
79  X *= w / 2;
80  Y *= h / 2;
81 
82  m_corner[0] = center - X - Y;
83  m_corner[1] = center + X - Y;
84  m_corner[2] = center + X + Y;
85  m_corner[3] = center - X + Y;
86 
87  computeAxes();
88 }
Vector2 m_corner[4]
Definition: Box2D.h:33
#define X
Definition: CollisionDetection.cpp:2281
void computeAxes()
Definition: Box2D.cpp:52
const Vector2 & center() const
Definition: Box2D.h:110
#define Y
Definition: CollisionDetection.cpp:2282

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

G3D::Box2D::Box2D ( const AABox2D b)
91  {
92  for (int i = 0; i < 4; ++i) {
93  m_corner[i] = b.corner(i);
94  }
95 
96  computeAxes();
97 }
Vector2 m_corner[4]
Definition: Box2D.h:33
void computeAxes()
Definition: Box2D.cpp:52

+ Here is the call graph for this function:

G3D::Box2D::Box2D ( const Vector2 min,
const Vector2 max 
)
100  {
101  *this = Box2D(Rect2D::xyxy(min, max));
102 }
T max(const T &x, const T &y)
Definition: g3dmath.h:320
T min(const T &x, const T &y)
Definition: g3dmath.h:305
Box2D(const Vector2 &center=Vector2(0, 0), float w=0, float h=0, float angle=0)
Definition: Box2D.cpp:75
static Rect2D xyxy(float x0, float y0, float x1, float y1)
Definition: Rect2D.h:240

+ Here is the call graph for this function:

G3D::Box2D::Box2D ( const CFrame frame,
Box2D b 
)

Transform b by frame, discarding the Z components, and compute the new box.

105  {
106  for (int i = 0; i < 4; ++i) {
107  m_corner[i] = frame.pointToWorldSpace(Vector3(b.corner(i), 0)).xy();
108  }
109  computeAxes();
110 }
Vector2 m_corner[4]
Definition: Box2D.h:33
Vector2 xy() const
Definition: Vector2.cpp:183
void computeAxes()
Definition: Box2D.cpp:52

+ Here is the call graph for this function:

Member Function Documentation

float G3D::Box2D::area ( ) const
inline

Surface area.

101  {
102  return m_area;
103  }
float m_area
Definition: Box2D.h:49
const Vector2& G3D::Box2D::axis ( int  a) const
inline

Unit length vector along axis a.

95  {
96  debugAssert(a == 0 || a == 1);
97  return m_axis[a];
98  }
#define debugAssert(exp)
Definition: debugAssert.h:160
Vector2 m_axis[2]
Definition: Box2D.h:40
const Vector2& G3D::Box2D::center ( ) const
inline
110  {
111  return m_center;
112  }
Vector2 m_center
Definition: Box2D.h:43
void G3D::Box2D::computeAxes ( )
private

Updates the axes after the m_corners move. Assumes the m_corners actually form a rectangle.

52  {
53  m_axis[0] = m_corner[1] - m_corner[0];
54  m_axis[1] = m_corner[3] - m_corner[0];
55 
56  // Make the length of each m_axisin = 1/edge length so we know any
57  // dot product must be less than 1 to fall within the edge.
58  float len[2];
59  for (int a = 0; a < 2; ++a) {
60  float lenSq = m_axis[a].squaredLength();
61  m_axisin[a] = m_axis[a] / lenSq;
62  origin[a] = m_corner[0].dot(m_axisin[a]);
63  len[a] = sqrt(lenSq);
64  m_axis[a] /= len[a];
65  }
66 
67  // w * h
68  m_area = len[0] * len[1];
69 
70 
71  m_center = (m_corner[0] + m_corner[2]) * 0.5f;
72 }
Vector2 m_corner[4]
Definition: Box2D.h:33
Vector2 m_axisin[2]
Definition: Box2D.h:37
Vector2 m_center
Definition: Box2D.h:43
float origin[2]
Definition: Box2D.h:46
float squaredLength() const
Definition: Vector2.h:414
Vector2 m_axis[2]
Definition: Box2D.h:40
float m_area
Definition: Box2D.h:49

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

bool G3D::Box2D::contains ( const Vector2 v) const
inline
79  {
80  // Take to object space:
81  const Vector2& p = v - m_center;
82  float x = p.dot(m_axisin[0]);
83  float y = p.dot(m_axisin[1]);
84 
85  // Must be within extent/2 on both axes in object space
86  return (abs(x) <= 0.5f) && (abs(y) <= 0.5f);
87  }
Vector2 m_axisin[2]
Definition: Box2D.h:37
double abs(double fValue)
Definition: g3dmath.h:617
Vector2 m_center
Definition: Box2D.h:43
G3D::int16 y
Definition: Vector2int16.h:38
G3D::int16 x
Definition: Vector2int16.h:37

+ Here is the call graph for this function:

const Vector2& G3D::Box2D::corner ( int  i) const
inline
105  {
106  debugAssert(i >=0 && i <= 3);
107  return m_corner[i];
108  }
Vector2 m_corner[4]
Definition: Box2D.h:33
#define debugAssert(exp)
Definition: debugAssert.h:160

+ Here is the caller graph for this function:

const Vector2& G3D::Box2D::extent ( ) const
inline

Distance from corner(0) to the next corner along the box's local axis a.

90  {
91  return m_extent;
92  }
Vector2 m_extent
Definition: Box2D.h:51
bool G3D::Box2D::overlaps ( const Box2D other) const
inline

Returns true if the intersection of the boxes is non-empty.

115  {
116  return overlaps1Way(other) && other.overlaps1Way(*this);
117  }
bool overlaps1Way(const Box2D &other) const
Definition: Box2D.cpp:17

+ Here is the call graph for this function:

bool G3D::Box2D::overlaps1Way ( const Box2D other) const
private

Returns true if other overlaps one dimension of this.

17  {
18  for (int a = 0; a < 2; ++a) {
19 
20  float t = other.m_corner[0].dot(m_axisin[a]);
21 
22  // Find the extent of box 2 on m_axisin a
23  float tMin = t;
24  float tMax = t;
25 
26  for (int c = 1; c < 4; ++c) {
27  t = other.m_corner[c].dot(m_axisin[a]);
28 
29  if (t < tMin) {
30  tMin = t;
31  } else if (t > tMax) {
32  tMax = t;
33  }
34  }
35 
36  // We have to subtract off the origin
37 
38  // See if [tMin, tMax] intersects [0, 1]
39  if ((tMin > 1 + origin[a]) || (tMax < origin[a])) {
40  // There was no intersection along this dimension;
41  // the boxes cannot possibly overlap.
42  return false;
43  }
44  }
45 
46  // There was no dimension along which there is no intersection.
47  // Therefore the boxes overlap.
48  return true;
49 }
Vector2 m_axisin[2]
Definition: Box2D.h:37
float origin[2]
Definition: Box2D.h:46

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

Member Data Documentation

float G3D::Box2D::m_area
private

Surface area

Vector2 G3D::Box2D::m_axis[2]
private

Two edges of the box extended away from corner[0], with unit length

Vector2 G3D::Box2D::m_axisin[2]
private

Two edges of the box extended away from corner[0], with length = 1 / extentSquared

Vector2 G3D::Box2D::m_center
private

Centroid of the box

Vector2 G3D::Box2D::m_corner[4]
private

Corners of the box, where 0 is the lower left.

Vector2 G3D::Box2D::m_extent
private
float G3D::Box2D::origin[2]
private

origin[a] = m_corner[0].dot(m_axisin[a]);


The documentation for this class was generated from the following files: