- v2f frac = { 1.0f/dir[0], 1.0f/dir[1] };
-
- t[0] = 999.9f;
- t[1] = 999.9f;
- t[2] = 999.9f;
- t[3] = 999.9f;
-
- if( fabsf(dir[0]) > 0.0001f )
- {
- t[0] = (0.0f-pos[0]) * frac[0];
- t[1] = (1.0f-pos[0]) * frac[0];
- }
- if( fabsf(dir[1]) > 0.0001f )
- {
- t[2] = (0.0f-pos[1]) * frac[1];
- t[3] = (1.0f-pos[1]) * frac[1];
- }
-
- 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
- */
-
-#if 0
- if( wg->cell_id[0] < 0 || wg->cell_id[0] >= WALKGRID_SIZE-1 ||
- wg->cell_id[1] < 0 || wg->cell_id[1] >= WALKGRID_SIZE-1 )
- {
- /*
- * This condition should never be reached if the grid size is big
- * enough
- */
- wg->move = -1.0f;
- return;
- }
-#endif
-
- 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
- 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 );
- }
- }
-
- /*
- * Compute intersection with grid cell moving outwards
- */
- t[6] = vg_minf( t[6], 1.0f );
-
- pl1[0] = pl0[0] + dir[0]*k_gridscale*t[6];
- pl1[1] = pl0[1];
- pl1[2] = pl0[2] + dir[1]*k_gridscale*t[6];
- vg_line( pl0, pl1, icolours[iter%3] );
-
- if( t[6] < 1.0f )
- {
- /*
- * To figure out what t value created the clip so we know which edge
- * to wrap around
- */
-
- if( t[4] < t[5] )
- {
- wg->pos[1] = pos[1] + dir[1]*t[6];
-
- if( t[0] > t[1] ) /* left edge */
- {
- wg->pos[0] = 0.9999f;
- wg->cell_id[0] --;
-
- if( wg->cell_id[0] == 0 )
- wg->move = -1.0f;
- }
- else /* Right edge */
- {
- wg->pos[0] = 0.0001f;
- wg->cell_id[0] ++;
-
- if( wg->cell_id[0] == WALKGRID_SIZE-2 )
- wg->move = -1.0f;
- }
- }
- else
- {
- wg->pos[0] = pos[0] + dir[0]*t[6];
-
- if( t[2] > t[3] ) /* bottom edge */
- {
- wg->pos[1] = 0.9999f;
- wg->cell_id[1] --;
-
- if( wg->cell_id[1] == 0 )
- wg->move = -1.0f;
- }
- else /* top edge */
- {
- wg->pos[1] = 0.0001f;
- wg->cell_id[1] ++;
-
- if( wg->cell_id[1] == WALKGRID_SIZE-2 )
- wg->move = -1.0f;
- }
- }
-
- wg->move -= t[6];
- }
- else
- {
- v2_muladds( wg->pos, dir, wg->move, wg->pos );
- wg->move = 0.0f;
- }
-}
-
-static void player_walkgrid_stand_cell(struct walkgrid *wg)
-{
- /*
- * NOTE: as opposed to the other function which is done in discretized space
- * this use a combination of both.
- */
-
- v3f world;
- world[0] = wg->region[0][0]+((float)wg->cell_id[0]+wg->pos[0])*k_gridscale;
- world[1] = player.co[1];
- world[2] = wg->region[0][2]+((float)wg->cell_id[1]+wg->pos[1])*k_gridscale;
-
- struct grid_sample *corners[4];
- const struct conf *conf = player_walkgrid_conf( wg, wg->cell_id, corners );
-
- if( conf != k_walkgrid_configs )
- {
- if( conf->edge_count == 0 )
- {
- v3f v0;
-
- /* Split the basic quad along the shortest diagonal */
- if( fabsf(corners[2]->pos[1] - corners[0]->pos[1]) <
- fabsf(corners[3]->pos[1] - corners[1]->pos[1]) )
- {
- vg_line( corners[2]->pos, corners[0]->pos, 0xffaaaaaa );
-
- if( wg->pos[0] > wg->pos[1] )
- player_walkgrid_stand_tri( corners[0]->pos,
- corners[3]->pos,
- corners[2]->pos, world );
- else
- player_walkgrid_stand_tri( corners[0]->pos,
- corners[2]->pos,
- corners[1]->pos, world );
- }
- else
- {
- vg_line( corners[3]->pos, corners[1]->pos, 0xffaaaaaa );
-
- if( wg->pos[0] < 1.0f-wg->pos[1] )
- player_walkgrid_stand_tri( corners[0]->pos,
- corners[3]->pos,
- corners[1]->pos, world );
- else
- player_walkgrid_stand_tri( corners[3]->pos,
- corners[2]->pos,
- corners[1]->pos, world );
- }
- }
- else
- {
- for( int i=0; i<conf->edge_count; i++ )
- {
- const struct confedge *edge = &conf->edges[i];
-
- v3f p0, p1;
- v3_muladds( corners[edge->i0]->pos,
- corners[edge->d0]->clip[edge->a0], k_gridscale, p0 );
- v3_muladds( corners[edge->i1]->pos,
- corners[edge->d1]->clip[edge->a1], k_gridscale, p1 );
-
- /*
- * Find penetration distance between player position and the edge
- */
-
- v2f normal = { -(p1[2]-p0[2]), p1[0]-p0[0] },
- rel = { world[0]-p0[0], world[2]-p0[2] };
-
- if( edge->o0 == -1 )
- {
- /* No subregions (default case), just use triangle created by
- * i0, e0, e1 */
- player_walkgrid_stand_tri( corners[edge->i0]->pos,
- p0,
- p1, world );
- }
- else
- {
- /*
- * Test if we are in the first region, which is
- * edge.i0, edge.e0, edge.o0,
- */
- v3f v0, ref;
- v3_sub( p0, corners[edge->o0]->pos, ref );
- v3_sub( world, corners[edge->o0]->pos, v0 );
-
- vg_line( corners[edge->o0]->pos, p0, 0xffffff00 );
- vg_line( corners[edge->o0]->pos, world, 0xff000000 );
-
- if( ref[0]*v0[2] - ref[2]*v0[0] < 0.0f )
- {
- player_walkgrid_stand_tri( corners[edge->i0]->pos,
- p0,
- corners[edge->o0]->pos, world );
- }
- else
- {
- if( edge->o1 == -1 )
- {
- /*
- * No other edges mean we just need to use the opposite
- *
- * e0, e1, o0 (in our case, also i1)
- */
- player_walkgrid_stand_tri( p0,
- p1,
- corners[edge->o0]->pos, world );
- }
- else
- {
- /*
- * Note: this v0 calculation can be ommited with the
- * current tileset.
- *
- * the last two triangles we have are:
- * e0, e1, o1
- * and
- * e1, i1, o1
- */
- v3_sub( p1, corners[edge->o1]->pos, ref );
- v3_sub( world, corners[edge->o1]->pos, v0 );
- vg_line( corners[edge->o1]->pos, p1, 0xff00ffff );
-
- if( ref[0]*v0[2] - ref[2]*v0[0] < 0.0f )
- {
- player_walkgrid_stand_tri( p0,
- p1,
- corners[edge->o1]->pos,
- world );
- }
- else
- {
- player_walkgrid_stand_tri( p1,
- corners[edge->i1]->pos,
- corners[edge->o1]->pos,
- world );
- }
- }
- }
- }
- }
- }
- }
-
- v3_copy( world, player.co );
-}