25 return val > 0 ? 1 : val < 0 ? -1 : 0;
35 float scale_u, scale_v;
40 float oneOverdu, oneOverdv;
42 float dp, oneOverdp, ep;
44 float t, h, cell_height;
46 unsigned int width, height;
48 bool firsttime, vertical, verticalhit;
53 : start(start), end(end), pos(cell->
GetPosition ()), size(cell->GetSize ()),
54 vertical (false), verticalhit (false)
57 const float rootOf2 = 1.414213f;
58 const float halfRoot2 = rootOf2 / 2;
62 width = cell->GetGridWidth ();
63 height = cell->GetGridHeight ();
65 scale_u = size.
x / (width - 1);
66 scale_v = size.
z / (height - 1);
69 const csVector2 gridOffsetStart = csVector2(start.
x - pos.x,
70 -(start.
z - (pos.y + size.
z)));
71 const csVector2 gridOffsetEnd = csVector2(end.
x - pos.x,
72 -(end.
z - (pos.y + size.
z)));
75 u0 = (gridOffsetStart.x) / scale_u;
76 v0 = (gridOffsetStart.y) / scale_v;
80 u1 = (gridOffsetEnd.x) / scale_u;
81 v1 = (gridOffsetEnd.y) / scale_v;
90 if (du == 0 && dv == 0)
94 float height = cell->GetHeight (gridOffsetStart);
95 if (
csMin(h0, h1) <= height &&
96 csMax(h0, h1) >= height)
113 dp = (du + dv) / rootOf2;
118 ep = fabs(dp) * (1 - eu - ev) / (dv + du);
121 if (du > 0) eu = 1 - eu;
122 if (dv > 0) ev = 1 - ev;
123 if (ep < 0) ep += halfRoot2;
128 cell_height = cell->GetHeight (gridOffsetStart);
139 const float rootOf2 = 1.414213f;
140 const float halfRoot2 = rootOf2 / 2;
142 if (t < 0 || t >= 1)
return -1;
143 if (!(t < 0) && !(t >= 0))
return -1;
150 if (verticalhit && firsttime)
157 result.
x = pos.x + cell_result.x * scale_u;
158 result.
y = cell_height;
159 result.
z = pos.y + height - cell_result.y * scale_v;
171 float tToU = eu * fabs (oneOverdu);
172 float tToV = ev * fabs (oneOverdv);
173 float tToP = ep * fabs (oneOverdp);
175 if (tToU <= tToV && tToU <= tToP)
178 if (t + tToU > 1) tToU = 1 - t;
185 ev -= fabs(dv) * tToU;
186 ep -= fabs(dp) * tToU;
191 else if (tToV <= tToU && tToV <= tToP)
194 if (t + tToV > 1) tToV = 1 - t;
200 eu -= fabs(du) * tToV;
202 ep -= fabs(dp) * tToV;
210 if (t + tToP > 1) tToP = 1 - t;
216 eu -= fabs(du) * tToP;
217 ev -= fabs(dv) * tToP;
227 if (ep <= 0) ep = halfRoot2;
234 float h_h0 = cell_height;
236 csVector2 uv = csVector2 ((u0 + du * t) * scale_u,
237 (v0 + dv * t) * scale_v);
238 float h_h1 = cell->GetHeight (uv);
244 int cmp_h0 = sign (r_h0 - h_h0);
245 int cmp_h1 = sign (r_h1 - h_h1);
247 if (cmp_h0 * cmp_h1 == -1 || fabs (r_h1 - h_h1) <
EPSILON)
251 if (cmp_h0 * cmp_h1 == -1)
257 float coeff = fabs(r_h0 - h_h0) / fabs(r_h1 - h_h1);
264 correct_t -= tstep / (coeff + 1);
267 cell_result.x = u0 + du * correct_t;
268 cell_result.y = v0 + dv * correct_t;
270 result.
x = pos.x + cell_result.x * scale_u;
271 result.
y = h0 + dh * correct_t;
272 result.
z = pos.y + height - cell_result.y * scale_v;
274 return (cell_result.x >=0 && cell_result.x <= width - 1 &&
275 cell_result.y >=0 && cell_result.y <= height - 1);