ScummVM API documentation
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
ai.h
1 /* ScummVM - Graphic Adventure Engine
2  *
3  * ScummVM is the legal property of its developers, whose names
4  * are too numerous to list here. Please refer to the COPYRIGHT
5  * file distributed with this source distribution.
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation, either version 3 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program. If not, see <http://www.gnu.org/licenses/>.
19  *
20  */
21 
22 #ifndef SCUMM_HE_BASKETBALL_AI_H
23 #define SCUMM_HE_BASKETBALL_AI_H
24 
25 #ifdef ENABLE_HE
26 
27 #include "scumm/he/intern_he.h"
28 #include "scumm/he/logic_he.h"
29 
30 #include "scumm/he/basketball/collision/bball_collision_support_obj.h"
31 
32 
33 namespace Scumm {
34 
35 #define FLOAT_EPSILON 0.001F
36 
37 inline int square(int x) { return x * x; }
38 
39 class Line2D {
40 public:
41  Line2D(float x_coef, float y_coef, float constant) {
42  _a = x_coef;
43  _b = y_coef;
44  _c = constant;
45  }
46 
47  Line2D(const U32FltVector2D &point1, const U32FltVector2D &point2) {
48  LineFromTwoPoints(point1, point2);
49  }
50 
51  void LineFromTwoPoints(U32FltVector2D pt1, U32FltVector2D pt2) {
52  float temp = (pt2.x - pt1.x);
53 
54  if (fabs(temp) < FLOAT_EPSILON) {
55  assert(((fabs(pt2.y - pt1.y) >= FLOAT_EPSILON)));
56  _a = 1;
57  _b = 0;
58  } else {
59  float m = (pt2.y - pt1.y) / temp;
60  _a = -m;
61  _b = 1;
62  }
63 
64  _c = -(_a * pt2.x + _b * pt2.y);
65  }
66 
67  inline float distance(U32FltVector2D point) {
68  return fabs((_a * point.x + _b * point.y + _c) / sqrt(square(_a) + square(_b)));
69  }
70 
71  inline float distance2(U32FltVector2D point) {
72  return fabs(square(_a * point.x + _b * point.y + _c) / (square(_a) + square(_b)));
73  }
74 
75  inline float angle() {
76  return atan2(-_a, _b);
77  }
78 
79  bool inBetween(U32FltVector2D point, U32FltVector2D end1, U32FltVector2D end2) {
80  assert((!onLine(end1) || !onLine(end2)));
81 
82  point = projectPoint(point);
83  float distance2 = end1.distance2(end2);
84 
85  return (point.distance2(end1) <= distance2 && point.distance2(end2) <= distance2) ? true : false;
86  }
87 
88  bool onLine(U32FltVector2D point) {
89  return (distance2(point) < 1.0F) ? true : false;
90  }
91 
92  U32FltVector2D projectPoint(U32FltVector2D point) {
93  return intersection(perpendicular(point));
94  }
95 
96  float getY(float x) {
97  if (_b != 0.0F)
98  return (-_a * x - _c) / _b;
99 
100  return 0.0F;
101  }
102 
103  float getX(float y) {
104  if (_a != 0.0F)
105  return (-_b * y - _c) / _a;
106 
107  return 0.0F;
108  }
109 
110  U32FltVector2D intersection(Line2D line) {
111  U32FltVector2D result(0.0F, 0.0F);
112 
113  assert(!sameSlope(line));
114 
115  if (_b == 0.0F) {
116  result.x = -_c / _a;
117  result.y = line.getY(result.x);
118  return result;
119  }
120 
121  if (line._b == 0.0F) {
122  result.x = -line._c / line._a;
123  result.y = getY(result.y);
124  return result;
125  }
126 
127  result.x = (_c * line._b - _b * line._c) / (line._a * _b - _a * line._b);
128  result.y = getY(result.x);
129  return result;
130  }
131 
132  Line2D perpendicular(U32FltVector2D point) {
133  return Line2D(_b, -_a, _a * point.y - _b * point.x);
134  }
135 
136  Line2D shiftY(float val) {
137  return Line2D(_a, _b, _c - val * _b);
138  }
139 
140  Line2D shiftX(float val) {
141  return Line2D(_a, _b, _c - val * _a);
142  }
143 
144  // Returns whether the projection of point1 is closer to targPoint than the projection of point2
145  bool isPointCloserToPointOnLine(U32FltVector2D point1, U32FltVector2D point2, U32FltVector2D targPoint) {
146 
147  assert(!onLine(targPoint));
148 
149  point1 = projectPoint(point1);
150  point2 = projectPoint(point2);
151 
152  return (point1.distance(targPoint) < point2.distance(targPoint)) ? true : false;
153  }
154 
155  bool halfPlaneTest(U32FltVector2D point) {
156  if (_b == 0)
157  return (point.x < -_c / _a) ? true : false;
158 
159  return (point.y > getY(point.x)) ? true : false;
160  }
161 
162  bool sameSlope(Line2D line) {
163  return ((_b == 0 && line._b == 0) || ((_a / _b) == (line._a / line._b))) ? true : false;
164  }
165 
166 private:
167  float _a, _b, _c; // The three coeffs in the line equation:
168  // Ax + By + C = 0
169 };
170 
171 } // End of namespace Scumm
172 
173 #endif // ENABLE_HE
174 
175 #endif // SCUMM_HE_BASKETBALL_AI_H
Definition: actor.h:30