4 // - line of sight algorithm for the fire ant AI
6 // it is simple, returns a value of 1 or 2
13 #include <gl\gl.h> // Header File For The OpenGL32 Library
14 #include <gl\glu.h> // Header File For The GLu32 Library
15 #include <gl\glaux.h> // Header File For The Glaux Library
19 #include "gldrawlib.h"
24 #define DIRECTION(dir, val) dir = val
26 // Our line of sight is simple, it is composed of a triangle
27 // if the point is within that triangle, return a one
28 // otherwise return a zero
32 // where y is actually the z
34 void DrawLineOfSight(float x1
, float y1
, float x2
, float y2
)
38 glDisable(GL_LIGHTING
);
39 glDisable(GL_TEXTURE_2D
);
41 glColor3f(1.0f
, 1.0f
, 1.0f
);
42 glBegin(GL_LINE_LOOP
);
45 glVertex3f(x1
, height
, y1
); // left bottom
46 glVertex3f(x2
, height
, y2
); // right bottom
53 glEnable(GL_LIGHTING
);
55 } // end of the function
60 // The algo just checks the y=mx+b of the line
61 // and depending on the location of the vertex, etc
62 // it is in the triangle or not
65 bool CheckSight(DriverBotPtr bot
, DriverBotPtr nme
)
73 float a1
, b1
, a2
, b2
, a3
, b3
, a4
, b4
;
76 float center_x
, center_y
;
83 AB_vert
= BC_vert
= CA_vert
= false;
85 // now the next point of the triangle
86 // shift by 45 degrees
87 tmp_heading
= bot
->heading
+ 125.0f
;
88 if (tmp_heading
> 360.0f
)
89 tmp_heading
-= 360.0f
;
91 rad
= tmp_heading
/ RAD_TO_DEG
;
93 tmp_x2
= LINE_OF_SIGHT
* (float)cos(rad
);
94 tmp_y2
= LINE_OF_SIGHT
* (float)sin(rad
);
96 tmp_x2
= tmp_x2
+ bot
->x
;
97 tmp_y2
= (-tmp_y2
) + bot
->y
;
99 // Now the final point
100 tmp_heading
= bot
->heading
+ 55.0f
;
101 if (tmp_heading
> 360.0f
)
102 tmp_heading
-= 360.0f
;
104 rad
= tmp_heading
/ RAD_TO_DEG
;
106 tmp_x3
= LINE_OF_SIGHT
* (float)cos(rad
);
107 tmp_y3
= LINE_OF_SIGHT
* (float)sin(rad
);
109 tmp_x3
= tmp_x3
+ bot
->x
;
110 tmp_y3
= (-tmp_y3
) + bot
->y
;
126 // find the slope of the different lines
127 // have to check for divide by zero also
130 m1
= (b2
- b1
) / (a2
- a1
); // a->b
131 bb1
= (b1
)-(m1
* a1
);
132 } else if ((a2
- a1
) == 0) {
134 } // end of the if-else
139 m2
= (b3
- b2
) / (a3
- a2
);
140 bb2
= (b2
)-(m2
* a2
);
141 } else if ((a3
- a2
) == 0) {
148 m3
= (b1
- b3
) / (a1
- a3
);
149 bb3
= (b3
) - (m3
* a3
);
150 } else if ((a3
- a2
) == 0) {
152 } // end of the if-else
154 center_x
= (a1
+ a2
+ a3
) / 3.0f
;
155 center_y
= (b1
+ b2
+ b3
) / 3.0f
;
158 if (((m1
* center_x
) + bb1
) >= center_y
)
159 DIRECTION(direction
, UP
);
161 DIRECTION(direction
, DOWN
);
163 if (AB_vert
== true) {
164 if ((a1
< a4
) && (a1
< center_x
))
166 else if ((a1
> a4
) && (a1
> center_x
))
170 if (direction
== UP
) {
171 if (b4
<= ((m1
* a4
) + bb1
))
173 } else if (direction
== DOWN
) {
174 if (b4
>= ((m1
* a4
) + bb1
))
178 } // end of the if - else
181 if (((m2
* center_x
)+bb2
) >= center_y
)
182 DIRECTION(direction
,UP
);
184 DIRECTION(direction
, DOWN
);
186 if (BC_vert
== true) {
187 if (( a2
< a4
) && (a2
< center_x
))
189 else if ((a2
> a4
) && (a2
> center_x
))
193 if (direction
== UP
) {
194 if (b4
<= ((m2
* a4
) + bb2
))
197 } else if (direction
== DOWN
) {
199 if (b4
>= ((m2
* a4
) + bb2
))
202 } // end of the if-else
204 } // end of the if-else
207 if (((m3
* center_x
) + bb3
) >= center_y
)
208 DIRECTION(direction
, UP
);
210 DIRECTION(direction
, DOWN
);
212 if (CA_vert
== true) {
213 if ((a3
< a4
) && (a3
< center_x
))
215 else if ((a3
> a4
) && (a3
> center_x
))
219 if (direction
== UP
) {
220 if (b4
<= ((m3
* a4
) + bb3
))
223 } else if (direction
== DOWN
) {
224 if (b4
>= ((m3
* a4
) + bb3
))
226 } // end of the if-else
228 } // end of the if-else
231 //DrawLineOfSight(bot->x, bot->y, tmp_x, tmp_y);
232 DrawLineOfSight(tmp_x2
, tmp_y2
, tmp_x3
, tmp_y3
);
233 DrawLineOfSight(bot
->x
, bot
->y
, tmp_x2
, tmp_y2
);
234 DrawLineOfSight(bot
->x
, bot
->y
, tmp_x3
, tmp_y3
);
237 // check if point lies inside
246 } // end of the function