+ t[4] = vg_maxf(t[0],t[1]);
+ t[5] = vg_maxf(t[2],t[3]);
+ t[6] = vg_minf(t[4],t[5]);
+}
+
+static void player_walkgrid_iter(struct walkgrid *wg, int iter)
+{
+
+ /*
+ * For each walkgrid iteration we are stepping through cells and determining
+ * the intersections with the grid, and any edges that are present
+ */
+
+ u32 icolours[] = { 0xffff00ff, 0xff00ffff, 0xffffff00 };
+
+ v3f pa, pb, pc, pd, pl0, pl1;
+ pa[0] = wg->region[0][0] + (float)wg->cell_id[0] *k_gridscale;
+ pa[1] = (wg->region[0][1] + wg->region[1][1]) * 0.5f + k_gridscale;
+ pa[2] = wg->region[0][2] + (float)wg->cell_id[1] *k_gridscale;
+ pb[0] = pa[0];
+ pb[1] = pa[1];
+ pb[2] = pa[2] + k_gridscale;
+ pc[0] = pa[0] + k_gridscale;
+ pc[1] = pa[1];
+ pc[2] = pa[2] + k_gridscale;
+ pd[0] = pa[0] + k_gridscale;
+ pd[1] = pa[1];
+ pd[2] = pa[2];
+#if 0
+ /* if you want to draw the current cell */
+ vg_line( pa, pb, 0xff00ffff );
+ vg_line( pb, pc, 0xff00ffff );
+ vg_line( pc, pd, 0xff00ffff );
+ vg_line( pd, pa, 0xff00ffff );
+#endif
+ pl0[0] = pa[0] + wg->pos[0]*k_gridscale;
+ pl0[1] = pa[1];
+ pl0[2] = pa[2] + wg->pos[1]*k_gridscale;
+
+ /*
+ * If there are edges present, we need to create a 'substep' event, where
+ * we find the intersection point, find the fully resolved position,
+ * then the new pos dir is the intersection->resolution
+ *
+ * the resolution is applied in non-discretized space in order to create a
+ * suitable vector for finding outflow, we want it to leave the cell so it
+ * can be used by the quad
+ */
+
+ v2f pos, dir;
+ v2_copy( wg->pos, pos );
+ v2_muls( wg->dir, wg->move, dir );
+
+ struct grid_sample *corners[4];
+ v2f corners2d[4] = {{0.0f,0.0f},{0.0f,1.0f},{1.0f,1.0f},{1.0f,0.0f}};
+ const struct conf *conf = player_walkgrid_conf( wg, wg->cell_id, corners );
+
+ float t[7];
+ player_walkgrid_min_cell( t, pos, dir );
+
+ for( int i=0; i<conf->edge_count; i++ )
+ {
+ const struct confedge *edge = &conf->edges[i];
+
+ v2f e0, e1, n, r, target, res, tangent;
+ e0[0] = corners2d[edge->i0][0] + corners[edge->d0]->clip[edge->a0][0];
+ e0[1] = corners2d[edge->i0][1] + corners[edge->d0]->clip[edge->a0][2];
+ e1[0] = corners2d[edge->i1][0] + corners[edge->d1]->clip[edge->a1][0];
+ e1[1] = corners2d[edge->i1][1] + corners[edge->d1]->clip[edge->a1][2];
+
+ v3f pe0 = { pa[0] + e0[0]*k_gridscale,
+ pa[1],
+ pa[2] + e0[1]*k_gridscale };
+ v3f pe1 = { pa[0] + e1[0]*k_gridscale,
+ pa[1],
+ pa[2] + e1[1]*k_gridscale };
+
+ v2_sub( e1, e0, tangent );
+ n[0] = -tangent[1];
+ n[1] = tangent[0];
+ v2_normalize( n );
+
+ /*
+ * If we find ourselfs already penetrating the edge, move back out a
+ * little
+ */
+ v2_sub( e0, pos, r );
+ float p1 = v2_dot(r,n);
+
+ if( -p1 < 0.0001f )
+ {
+ v2_muladds( pos, n, p1+0.0001f, pos );
+ v2_copy( pos, wg->pos );
+ v3f p_new = { pa[0] + pos[0]*k_gridscale,
+ pa[1],
+ pa[2] + pos[1]*k_gridscale };
+ v3_copy( p_new, pl0 );
+ }
+
+ v2_add( pos, dir, target );
+
+ v2f v1, v2, v3;
+ v2_sub( e0, pos, v1 );
+ v2_sub( target, pos, v2 );
+
+ v2_copy( n, v3 );
+
+ v2_sub( e0, target, r );
+ float p = v2_dot(r,n),
+ t1 = v2_dot(v1,v3)/v2_dot(v2,v3);
+
+ if( t1 < t[6] && t1 > 0.0f && -p < 0.001f )
+ {
+ v2_muladds( target, n, p+0.0001f, res );
+
+ v2f intersect;
+ v2_muladds( pos, dir, t1, intersect );
+ v2_copy( intersect, pos );
+ v2_sub( res, intersect, dir );
+
+ v3f p_res = { pa[0] + res[0]*k_gridscale,
+ pa[1],
+ pa[2] + res[1]*k_gridscale };
+ v3f p_int = { pa[0] + intersect[0]*k_gridscale,
+ pa[1],
+ pa[2] + intersect[1]*k_gridscale };
+
+ vg_line( pl0, p_int, icolours[iter%3] );
+ v3_copy( p_int, pl0 );
+ v2_copy( pos, wg->pos );
+
+ player_walkgrid_min_cell( t, pos, dir );
+ }
+ }