22 #ifndef SCUMM_HE_BASKETBALL_AI_H 23 #define SCUMM_HE_BASKETBALL_AI_H 27 #include "scumm/he/intern_he.h" 28 #include "scumm/he/logic_he.h" 30 #include "scumm/he/basketball/collision/bball_collision_support_obj.h" 35 #define FLOAT_EPSILON 0.001F 37 inline int square(
int x) {
return x * x; }
41 Line2D(
float x_coef,
float y_coef,
float constant) {
47 Line2D(
const U32FltVector2D &point1,
const U32FltVector2D &point2) {
48 LineFromTwoPoints(point1, point2);
51 void LineFromTwoPoints(U32FltVector2D pt1, U32FltVector2D pt2) {
52 float temp = (pt2.x - pt1.x);
54 if (fabs(temp) < FLOAT_EPSILON) {
55 assert(((fabs(pt2.y - pt1.y) >= FLOAT_EPSILON)));
59 float m = (pt2.y - pt1.y) / temp;
64 _c = -(_a * pt2.x + _b * pt2.y);
67 inline float distance(U32FltVector2D point) {
68 return fabs((_a * point.x + _b * point.y + _c) / sqrt(square(_a) + square(_b)));
71 inline float distance2(U32FltVector2D point) {
72 return fabs(square(_a * point.x + _b * point.y + _c) / (square(_a) + square(_b)));
75 inline float angle() {
76 return atan2(-_a, _b);
79 bool inBetween(U32FltVector2D point, U32FltVector2D end1, U32FltVector2D end2) {
80 assert((!onLine(end1) || !onLine(end2)));
82 point = projectPoint(point);
83 float distance2 = end1.distance2(end2);
85 return (point.distance2(end1) <= distance2 && point.distance2(end2) <= distance2) ?
true :
false;
88 bool onLine(U32FltVector2D point) {
89 return (distance2(point) < 1.0F) ? true :
false;
92 U32FltVector2D projectPoint(U32FltVector2D point) {
93 return intersection(perpendicular(point));
98 return (-_a * x - _c) / _b;
103 float getX(
float y) {
105 return (-_b * y - _c) / _a;
110 U32FltVector2D intersection(Line2D line) {
111 U32FltVector2D result(0.0F, 0.0F);
113 assert(!sameSlope(line));
117 result.y = line.getY(result.x);
121 if (line._b == 0.0F) {
122 result.x = -line._c / line._a;
123 result.y = getY(result.y);
127 result.x = (_c * line._b - _b * line._c) / (line._a * _b - _a * line._b);
128 result.y = getY(result.x);
132 Line2D perpendicular(U32FltVector2D point) {
133 return Line2D(_b, -_a, _a * point.y - _b * point.x);
136 Line2D shiftY(
float val) {
137 return Line2D(_a, _b, _c - val * _b);
140 Line2D shiftX(
float val) {
141 return Line2D(_a, _b, _c - val * _a);
145 bool isPointCloserToPointOnLine(U32FltVector2D point1, U32FltVector2D point2, U32FltVector2D targPoint) {
147 assert(!onLine(targPoint));
149 point1 = projectPoint(point1);
150 point2 = projectPoint(point2);
152 return (point1.distance(targPoint) < point2.distance(targPoint)) ?
true :
false;
155 bool halfPlaneTest(U32FltVector2D point) {
157 return (point.x < -_c / _a) ? true :
false;
159 return (point.y > getY(point.x)) ? true :
false;
162 bool sameSlope(Line2D line) {
163 return ((_b == 0 && line._b == 0) || ((_a / _b) == (line._a / line._b))) ? true :
false;
175 #endif // SCUMM_HE_BASKETBALL_AI_H