937c30558cb7bb95dc742390f54d24d3bf189cae
1 #ifndef PLAYER_WALKGRID_H
2 #define PLAYER_WALKGRID_H
8 * Walkgrid implementation,
9 * loosely based of cmuratoris youtube video 'Killing the Walkmonster'
12 #define WALKGRID_SIZE 16
19 k_sample_type_air
, /* Nothing was hit. */
20 k_sample_type_invalid
, /* The point is invalid, but there is a sample
21 underneath that can be used */
22 k_sample_type_valid
, /* This point is good */
31 k_traverse_none
= 0x00,
37 samples
[WALKGRID_SIZE
][WALKGRID_SIZE
];
41 float move
; /* Current amount of movement we have left to apply */
42 v2f dir
; /* The movement delta */
43 v2i cell_id
;/* Current cell */
44 v2f pos
; /* Local position (in cell) */
48 static int player_walkgrid_tri_walkable( u32 tri
[3] )
50 return tri
[0] > world
.sm_geo_std_oob
.vertex_count
;
54 * Get a sample at this pole location, will return 1 if the sample is valid,
55 * and pos will be updated to be the intersection location.
57 static void player_walkgrid_samplepole( struct grid_sample
*s
)
59 boxf region
= {{ s
->pos
[0] -0.01f
, s
->pos
[1] - 4.0f
, s
->pos
[2] -0.01f
},
60 { s
->pos
[0] +0.01f
, s
->pos
[1] + 4.0f
, s
->pos
[2] +0.01f
}};
64 int len
= bh_select( &world
.geo
.bhtris
, region
, geo
, 256 );
66 const float k_minworld_y
= -2000.0f
;
68 float walk_height
= k_minworld_y
,
69 block_height
= k_minworld_y
;
71 s
->type
= k_sample_type_air
;
73 for( int i
=0; i
<len
; i
++ )
75 u32
*ptri
= &world
.geo
.indices
[ geo
[i
]*3 ];
77 for( int j
=0; j
<3; j
++ )
78 v3_copy( world
.geo
.verts
[ptri
[j
]].co
, tri
[j
] );
80 v3f vdown
= {0.0f
,-1.0f
,0.0f
};
82 v3_copy( s
->pos
, sample_from
);
83 sample_from
[1] = region
[1][1];
86 if( ray_tri( tri
, sample_from
, vdown
, &dist
))
89 v3_muladds( sample_from
, vdown
, dist
, p0
);
91 if( player_walkgrid_tri_walkable(ptri
) )
93 if( p0
[1] > walk_height
)
100 if( p0
[1] > block_height
)
101 block_height
= p0
[1];
106 s
->pos
[1] = walk_height
;
108 if( walk_height
> k_minworld_y
)
109 if( block_height
> walk_height
)
110 s
->type
= k_sample_type_invalid
;
112 s
->type
= k_sample_type_valid
;
114 s
->type
= k_sample_type_air
;
117 float const k_gridscale
= 0.5f
;
125 static void player_walkgrid_clip_blocker( struct grid_sample
*sa
,
126 struct grid_sample
*sb
,
127 struct grid_sample
*st
,
131 int valid_a
= sa
->type
== k_sample_type_valid
,
132 valid_b
= sb
->type
== k_sample_type_valid
;
133 struct grid_sample
*target
= valid_a
? sa
: sb
,
134 *other
= valid_a
? sb
: sa
;
135 v3_copy( target
->pos
, pos
);
136 v3_sub( other
->pos
, target
->pos
, clipdir
);
139 v3_muladds( pos
, (v3f
){1.0f
,1.0f
,1.0f
}, -k_gridscale
*2.1f
, cell_region
[0]);
140 v3_muladds( pos
, (v3f
){1.0f
,1.0f
,1.0f
}, k_gridscale
*2.1f
, cell_region
[1]);
144 int len
= bh_select( &world
.geo
.bhtris
, cell_region
, geo
, 256 );
146 float start_time
= v3_length( clipdir
),
147 min_time
= start_time
;
148 v3_normalize( clipdir
);
149 v3_muls( clipdir
, 0.0001f
, st
->clip
[dir
] );
151 for( int i
=0; i
<len
; i
++ )
153 u32
*ptri
= &world
.geo
.indices
[ geo
[i
]*3 ];
154 for( int j
=0; j
<3; j
++ )
155 v3_copy( world
.geo
.verts
[ptri
[j
]].co
, tri
[j
] );
157 if( player_walkgrid_tri_walkable(ptri
) )
161 if(ray_tri( tri
, pos
, clipdir
, &dist
))
163 if( dist
> 0.0f
&& dist
< min_time
)
166 sb
->type
= k_sample_type_air
;
171 if( !(min_time
< start_time
) )
172 min_time
= 0.5f
* k_gridscale
;
174 min_time
= vg_clampf( min_time
/k_gridscale
, 0.01f
, 0.99f
);
176 v3_muls( clipdir
, min_time
, st
->clip
[dir
] );
179 v3_muladds( target
->pos
, st
->clip
[dir
], k_gridscale
, p0
);
182 static void player_walkgrid_clip_edge( struct grid_sample
*sa
,
183 struct grid_sample
*sb
,
184 struct grid_sample
*st
, /* data store */
187 v3f clipdir
= { 0.0f
, 0.0f
, 0.0f
}, pos
;
188 int valid_a
= sa
->type
== k_sample_type_valid
,
189 valid_b
= sb
->type
== k_sample_type_valid
;
191 struct grid_sample
*target
= valid_a
? sa
: sb
,
192 *other
= valid_a
? sb
: sa
;
194 v3_sub( other
->pos
, target
->pos
, clipdir
);
197 v3_copy( target
->pos
, pos
);
200 v3_muladds( pos
, (v3f
){1.0f
,1.0f
,1.0f
}, -k_gridscale
*1.1f
, cell_region
[0]);
201 v3_muladds( pos
, (v3f
){1.0f
,1.0f
,1.0f
}, k_gridscale
*1.1f
, cell_region
[1]);
204 int len
= bh_select( &world
.geo
.bhtris
, cell_region
, geo
, 256 );
206 float max_dist
= 0.0f
;
209 v3_cross( clipdir
,(v3f
){0.0f
,1.0f
,0.0f
},perp
);
210 v3_muls( clipdir
, 0.001f
, st
->clip
[dir
] );
212 for( int i
=0; i
<len
; i
++ )
214 u32
*ptri
= &world
.geo
.indices
[ geo
[i
]*3 ];
215 for( int j
=0; j
<3; j
++ )
216 v3_copy( world
.geo
.verts
[ptri
[j
]].co
, tri
[j
] );
218 if( !player_walkgrid_tri_walkable(ptri
) )
221 for( int k
=0; k
<3; k
++ )
227 v3_sub( tri
[ia
], pos
, v0
);
228 v3_sub( tri
[ib
], pos
, v1
);
230 if( (clipdir
[2]*v0
[0] - clipdir
[0]*v0
[2]) *
231 (clipdir
[2]*v1
[0] - clipdir
[0]*v1
[2]) < 0.0f
)
233 float da
= v3_dot(v0
,perp
),
234 db
= v3_dot(v1
,perp
),
239 v3_muls( v1
, qa
, p0
);
240 v3_muladds( p0
, v0
, 1.0f
-qa
, p0
);
242 float h
= v3_dot(p0
,clipdir
)/v3_dot(clipdir
,clipdir
);
244 if( h
>= max_dist
&& h
<= 1.0f
)
247 float l
= 1.0f
/v3_length(clipdir
);
248 v3_muls( p0
, l
, st
->clip
[dir
] );
255 static const struct conf
262 * o: the 'other' point to do a A/B test with
263 * if its -1, all AB is done.
273 k_walkgrid_configs
[16] = {
275 {{{ 3,3, 3,0, 1,0, -1,-1 }}, 1},
276 {{{ 2,2, 1,3, 0,1, -1,-1 }}, 1},
277 {{{ 2,3, 1,0, 0,0, 3,-1 }}, 1},
279 {{{ 1,1, 0,1, 1,0, -1,-1 }}, 1},
280 {{{ 3,3, 3,0, 1,0, -1,-1 },
281 { 1,1, 0,1, 1,0, -1,-1 }}, 2},
282 {{{ 1,2, 0,3, 1,1, 2,-1 }}, 1},
283 {{{ 1,3, 0,0, 1,0, 2, 2 }}, 1},
285 {{{ 0,0, 0,0, 0,1, -1,-1 }}, 1},
286 {{{ 3,0, 3,0, 1,1, 0,-1 }}, 1},
287 {{{ 2,2, 1,3, 0,1, -1,-1 },
288 { 0,0, 0,0, 0,1, -1,-1 }}, 2},
289 {{{ 2,0, 1,0, 0,1, 3, 3 }}, 1},
291 {{{ 0,1, 0,1, 0,0, 1,-1 }}, 1},
292 {{{ 3,1, 3,1, 1,0, 0, 0 }}, 1},
293 {{{ 0,2, 0,3, 0,1, 1, 1 }}, 1},
298 * Get a buffer of edges from cell location
300 static const struct conf
*player_walkgrid_conf( struct walkgrid
*wg
,
302 struct grid_sample
*corners
[4] )
304 corners
[0] = &wg
->samples
[cell
[1] ][cell
[0] ];
305 corners
[1] = &wg
->samples
[cell
[1]+1][cell
[0] ];
306 corners
[2] = &wg
->samples
[cell
[1]+1][cell
[0]+1];
307 corners
[3] = &wg
->samples
[cell
[1] ][cell
[0]+1];
309 u32 vd0
= corners
[0]->type
== k_sample_type_valid
,
310 vd1
= corners
[1]->type
== k_sample_type_valid
,
311 vd2
= corners
[2]->type
== k_sample_type_valid
,
312 vd3
= corners
[3]->type
== k_sample_type_valid
,
313 config
= (vd0
<<3) | (vd1
<<2) | (vd2
<<1) | vd3
;
315 return &k_walkgrid_configs
[ config
];
318 static void player_walkgrid_floor(v3f pos
)
320 v3_muls( pos
, 1.0f
/k_gridscale
, pos
);
321 v3_floor( pos
, pos
);
322 v3_muls( pos
, k_gridscale
, pos
);
326 * Computes the barycentric coordinate of location on a triangle (vertical),
327 * then sets the Y position to the interpolation of the three points
329 static void player_walkgrid_stand_tri( v3f a
, v3f b
, v3f c
, v3f pos
)
334 v3_sub( pos
, a
, v2
);
336 float d
= v0
[0]*v1
[2] - v1
[0]*v0
[2],
337 v
= (v2
[0]*v1
[2] - v1
[0]*v2
[2]) / d
,
338 w
= (v0
[0]*v2
[2] - v2
[0]*v0
[2]) / d
,
341 vg_line( pos
, a
, 0xffff0000 );
342 vg_line( pos
, b
, 0xff00ff00 );
343 vg_line( pos
, c
, 0xff0000ff );
344 pos
[1] = u
*a
[1] + v
*b
[1] + w
*c
[1];
348 * Get the minimum time value of pos+dir until a cell edge
350 * t[0] -> t[3] are the individual time values
351 * t[5] & t[6] are the maximum axis values
352 * t[6] is the minimum value
355 static void player_walkgrid_min_cell( float t
[7], v2f pos
, v2f dir
)
357 v2f frac
= { 1.0f
/dir
[0], 1.0f
/dir
[1] };
364 if( fabsf(dir
[0]) > 0.0001f
)
366 t
[0] = (0.0f
-pos
[0]) * frac
[0];
367 t
[1] = (1.0f
-pos
[0]) * frac
[0];
369 if( fabsf(dir
[1]) > 0.0001f
)
371 t
[2] = (0.0f
-pos
[1]) * frac
[1];
372 t
[3] = (1.0f
-pos
[1]) * frac
[1];
375 t
[4] = vg_maxf(t
[0],t
[1]);
376 t
[5] = vg_maxf(t
[2],t
[3]);
377 t
[6] = vg_minf(t
[4],t
[5]);
380 static void player_walkgrid_iter(struct walkgrid
*wg
, int iter
)
384 * For each walkgrid iteration we are stepping through cells and determining
385 * the intersections with the grid, and any edges that are present
388 u32 icolours
[] = { 0xffff00ff, 0xff00ffff, 0xffffff00 };
390 v3f pa
, pb
, pc
, pd
, pl0
, pl1
;
391 pa
[0] = wg
->region
[0][0] + (float)wg
->cell_id
[0] *k_gridscale
;
392 pa
[1] = (wg
->region
[0][1] + wg
->region
[1][1]) * 0.5f
+ k_gridscale
;
393 pa
[2] = wg
->region
[0][2] + (float)wg
->cell_id
[1] *k_gridscale
;
397 pb
[2] = pa
[2] + k_gridscale
;
398 pc
[0] = pa
[0] + k_gridscale
;
400 pc
[2] = pa
[2] + k_gridscale
;
401 pd
[0] = pa
[0] + k_gridscale
;
404 /* if you want to draw the current cell */
405 vg_line( pa
, pb
, 0xff00ffff );
406 vg_line( pb
, pc
, 0xff00ffff );
407 vg_line( pc
, pd
, 0xff00ffff );
408 vg_line( pd
, pa
, 0xff00ffff );
410 pl0
[0] = pa
[0] + wg
->pos
[0]*k_gridscale
;
412 pl0
[2] = pa
[2] + wg
->pos
[1]*k_gridscale
;
415 * If there are edges present, we need to create a 'substep' event, where
416 * we find the intersection point, find the fully resolved position,
417 * then the new pos dir is the intersection->resolution
419 * the resolution is applied in non-discretized space in order to create a
420 * suitable vector for finding outflow, we want it to leave the cell so it
421 * can be used by the quad
425 v2_copy( wg
->pos
, pos
);
426 v2_muls( wg
->dir
, wg
->move
, dir
);
428 struct grid_sample
*corners
[4];
429 v2f corners2d
[4] = {{0.0f
,0.0f
},{0.0f
,1.0f
},{1.0f
,1.0f
},{1.0f
,0.0f
}};
430 const struct conf
*conf
= player_walkgrid_conf( wg
, wg
->cell_id
, corners
);
433 player_walkgrid_min_cell( t
, pos
, dir
);
435 for( int i
=0; i
<conf
->edge_count
; i
++ )
437 const struct confedge
*edge
= &conf
->edges
[i
];
439 v2f e0
, e1
, n
, r
, target
, res
, tangent
;
440 e0
[0] = corners2d
[edge
->i0
][0] + corners
[edge
->d0
]->clip
[edge
->a0
][0];
441 e0
[1] = corners2d
[edge
->i0
][1] + corners
[edge
->d0
]->clip
[edge
->a0
][2];
442 e1
[0] = corners2d
[edge
->i1
][0] + corners
[edge
->d1
]->clip
[edge
->a1
][0];
443 e1
[1] = corners2d
[edge
->i1
][1] + corners
[edge
->d1
]->clip
[edge
->a1
][2];
445 v3f pe0
= { pa
[0] + e0
[0]*k_gridscale
,
447 pa
[2] + e0
[1]*k_gridscale
};
448 v3f pe1
= { pa
[0] + e1
[0]*k_gridscale
,
450 pa
[2] + e1
[1]*k_gridscale
};
452 v2_sub( e1
, e0
, tangent
);
458 * If we find ourselfs already penetrating the edge, move back out a
461 v2_sub( e0
, pos
, r
);
462 float p1
= v2_dot(r
,n
);
466 v2_muladds( pos
, n
, p1
+0.0001f
, pos
);
467 v2_copy( pos
, wg
->pos
);
468 v3f p_new
= { pa
[0] + pos
[0]*k_gridscale
,
470 pa
[2] + pos
[1]*k_gridscale
};
471 v3_copy( p_new
, pl0
);
474 v2_add( pos
, dir
, target
);
477 v2_sub( e0
, pos
, v1
);
478 v2_sub( target
, pos
, v2
);
482 v2_sub( e0
, target
, r
);
483 float p
= v2_dot(r
,n
),
484 t1
= v2_dot(v1
,v3
)/v2_dot(v2
,v3
);
486 if( t1
< t
[6] && t1
> 0.0f
&& -p
< 0.001f
)
488 v2_muladds( target
, n
, p
+0.0001f
, res
);
491 v2_muladds( pos
, dir
, t1
, intersect
);
492 v2_copy( intersect
, pos
);
493 v2_sub( res
, intersect
, dir
);
495 v3f p_res
= { pa
[0] + res
[0]*k_gridscale
,
497 pa
[2] + res
[1]*k_gridscale
};
498 v3f p_int
= { pa
[0] + intersect
[0]*k_gridscale
,
500 pa
[2] + intersect
[1]*k_gridscale
};
502 vg_line( pl0
, p_int
, icolours
[iter
%3] );
503 v3_copy( p_int
, pl0
);
504 v2_copy( pos
, wg
->pos
);
506 player_walkgrid_min_cell( t
, pos
, dir
);
511 * Compute intersection with grid cell moving outwards
513 t
[6] = vg_minf( t
[6], 1.0f
);
515 pl1
[0] = pl0
[0] + dir
[0]*k_gridscale
*t
[6];
517 pl1
[2] = pl0
[2] + dir
[1]*k_gridscale
*t
[6];
518 vg_line( pl0
, pl1
, icolours
[iter
%3] );
523 * To figure out what t value created the clip so we know which edge
529 wg
->pos
[1] = pos
[1] + dir
[1]*t
[6];
531 if( t
[0] > t
[1] ) /* left edge */
533 wg
->pos
[0] = 0.9999f
;
536 if( wg
->cell_id
[0] == 0 )
539 else /* Right edge */
541 wg
->pos
[0] = 0.0001f
;
544 if( wg
->cell_id
[0] == WALKGRID_SIZE
-2 )
550 wg
->pos
[0] = pos
[0] + dir
[0]*t
[6];
552 if( t
[2] > t
[3] ) /* bottom edge */
554 wg
->pos
[1] = 0.9999f
;
557 if( wg
->cell_id
[1] == 0 )
562 wg
->pos
[1] = 0.0001f
;
565 if( wg
->cell_id
[1] == WALKGRID_SIZE
-2 )
574 v2_muladds( wg
->pos
, dir
, wg
->move
, wg
->pos
);
579 static void player_walkgrid_stand_cell(struct walkgrid
*wg
)
582 * NOTE: as opposed to the other function which is done in discretized space
583 * this use a combination of both.
587 world
[0] = wg
->region
[0][0]+((float)wg
->cell_id
[0]+wg
->pos
[0])*k_gridscale
;
588 world
[1] = player
.rb
.co
[1];
589 world
[2] = wg
->region
[0][2]+((float)wg
->cell_id
[1]+wg
->pos
[1])*k_gridscale
;
591 struct grid_sample
*corners
[4];
592 const struct conf
*conf
= player_walkgrid_conf( wg
, wg
->cell_id
, corners
);
594 if( conf
!= k_walkgrid_configs
)
596 if( conf
->edge_count
== 0 )
600 /* Split the basic quad along the shortest diagonal */
601 if( fabsf(corners
[2]->pos
[1] - corners
[0]->pos
[1]) <
602 fabsf(corners
[3]->pos
[1] - corners
[1]->pos
[1]) )
604 vg_line( corners
[2]->pos
, corners
[0]->pos
, 0xffaaaaaa );
606 if( wg
->pos
[0] > wg
->pos
[1] )
607 player_walkgrid_stand_tri( corners
[0]->pos
,
609 corners
[2]->pos
, world
);
611 player_walkgrid_stand_tri( corners
[0]->pos
,
613 corners
[1]->pos
, world
);
617 vg_line( corners
[3]->pos
, corners
[1]->pos
, 0xffaaaaaa );
619 if( wg
->pos
[0] < 1.0f
-wg
->pos
[1] )
620 player_walkgrid_stand_tri( corners
[0]->pos
,
622 corners
[1]->pos
, world
);
624 player_walkgrid_stand_tri( corners
[3]->pos
,
626 corners
[1]->pos
, world
);
631 for( int i
=0; i
<conf
->edge_count
; i
++ )
633 const struct confedge
*edge
= &conf
->edges
[i
];
636 v3_muladds( corners
[edge
->i0
]->pos
,
637 corners
[edge
->d0
]->clip
[edge
->a0
], k_gridscale
, p0
);
638 v3_muladds( corners
[edge
->i1
]->pos
,
639 corners
[edge
->d1
]->clip
[edge
->a1
], k_gridscale
, p1
);
642 * Find penetration distance between player position and the edge
645 v2f normal
= { -(p1
[2]-p0
[2]), p1
[0]-p0
[0] },
646 rel
= { world
[0]-p0
[0], world
[2]-p0
[2] };
650 /* No subregions (default case), just use triangle created by
652 player_walkgrid_stand_tri( corners
[edge
->i0
]->pos
,
659 * Test if we are in the first region, which is
660 * edge.i0, edge.e0, edge.o0,
663 v3_sub( p0
, corners
[edge
->o0
]->pos
, ref
);
664 v3_sub( world
, corners
[edge
->o0
]->pos
, v0
);
666 vg_line( corners
[edge
->o0
]->pos
, p0
, 0xffffff00 );
667 vg_line( corners
[edge
->o0
]->pos
, world
, 0xff000000 );
669 if( ref
[0]*v0
[2] - ref
[2]*v0
[0] < 0.0f
)
671 player_walkgrid_stand_tri( corners
[edge
->i0
]->pos
,
673 corners
[edge
->o0
]->pos
, world
);
680 * No other edges mean we just need to use the opposite
682 * e0, e1, o0 (in our case, also i1)
684 player_walkgrid_stand_tri( p0
,
686 corners
[edge
->o0
]->pos
, world
);
691 * Note: this v0 calculation can be ommited with the
694 * the last two triangles we have are:
699 v3_sub( p1
, corners
[edge
->o1
]->pos
, ref
);
700 v3_sub( world
, corners
[edge
->o1
]->pos
, v0
);
701 vg_line( corners
[edge
->o1
]->pos
, p1
, 0xff00ffff );
703 if( ref
[0]*v0
[2] - ref
[2]*v0
[0] < 0.0f
)
705 player_walkgrid_stand_tri( p0
,
707 corners
[edge
->o1
]->pos
,
712 player_walkgrid_stand_tri( p1
,
713 corners
[edge
->i1
]->pos
,
714 corners
[edge
->o1
]->pos
,
724 v3_copy( world
, player
.rb
.co
);
727 static void player_walkgrid_getsurface(void)
729 float const k_stepheight
= 0.5f
;
730 float const k_miny
= 0.6f
;
731 float const k_height
= 1.78f
;
732 float const k_region_size
= (float)WALKGRID_SIZE
/2.0f
* k_gridscale
;
734 static struct walkgrid wg
;
737 v3_copy( player
.rb
.co
, cell
);
738 player_walkgrid_floor( cell
);
740 v3_muladds( cell
, (v3f
){-1.0f
,-1.0f
,-1.0f
}, k_region_size
, wg
.region
[0] );
741 v3_muladds( cell
, (v3f
){ 1.0f
, 1.0f
, 1.0f
}, k_region_size
, wg
.region
[1] );
745 * Create player input vector
747 v3f delta
= {0.0f
,0.0f
,0.0f
};
748 v3f fwd
= { -sinf(-player
.angles
[0]), 0.0f
, -cosf(-player
.angles
[0]) },
749 side
= { -fwd
[2], 0.0f
, fwd
[0] };
752 if( !vg_console_enabled() )
754 if( glfwGetKey( vg_window
, GLFW_KEY_W
) )
755 v3_muladds( delta
, fwd
, ktimestep
*k_walkspeed
, delta
);
756 if( glfwGetKey( vg_window
, GLFW_KEY_S
) )
757 v3_muladds( delta
, fwd
, -ktimestep
*k_walkspeed
, delta
);
759 if( glfwGetKey( vg_window
, GLFW_KEY_A
) )
760 v3_muladds( delta
, side
, -ktimestep
*k_walkspeed
, delta
);
761 if( glfwGetKey( vg_window
, GLFW_KEY_D
) )
762 v3_muladds( delta
, side
, ktimestep
*k_walkspeed
, delta
);
764 v3_muladds( delta
, fwd
,
765 vg_get_axis("vertical")*-ktimestep
*k_walkspeed
, delta
);
766 v3_muladds( delta
, side
,
767 vg_get_axis("horizontal")*ktimestep
*k_walkspeed
, delta
);
771 * Create our move in grid space
773 wg
.dir
[0] = delta
[0] * (1.0f
/k_gridscale
);
774 wg
.dir
[1] = delta
[2] * (1.0f
/k_gridscale
);
779 (player
.rb
.co
[0] - wg
.region
[0][0]) * (1.0f
/k_gridscale
),
780 (player
.rb
.co
[2] - wg
.region
[0][2]) * (1.0f
/k_gridscale
)
783 v2_floor( region_pos
, region_cell_pos
);
784 v2_sub( region_pos
, region_cell_pos
, wg
.pos
);
786 wg
.cell_id
[0] = region_cell_pos
[0];
787 wg
.cell_id
[1] = region_cell_pos
[1];
789 for(int y
=0; y
<WALKGRID_SIZE
; y
++ )
791 for(int x
=0; x
<WALKGRID_SIZE
; x
++ )
793 struct grid_sample
*s
= &wg
.samples
[y
][x
];
794 v3_muladds( wg
.region
[0], (v3f
){ x
, 0, y
}, k_gridscale
, s
->pos
);
795 s
->state
= k_traverse_none
;
796 s
->type
= k_sample_type_air
;
797 v3_zero( s
->clip
[0] );
798 v3_zero( s
->clip
[1] );
802 v2i border
[WALKGRID_SIZE
*WALKGRID_SIZE
];
803 v2i
*cborder
= border
;
804 u32 border_length
= 1;
806 struct grid_sample
*base
= NULL
;
808 v2i starters
[] = {{0,0},{1,1},{0,1},{1,0}};
810 for( int i
=0;i
<4;i
++ )
813 v2i_add( wg
.cell_id
, starters
[i
], test
);
814 v2i_copy( test
, border
[0] );
815 base
= &wg
.samples
[test
[1]][test
[0]];
817 base
->pos
[1] = cell
[1];
818 player_walkgrid_samplepole( base
);
820 if( base
->type
== k_sample_type_valid
)
823 base
->type
= k_sample_type_air
;
826 vg_line_pt3( base
->pos
, 0.1f
, 0xffffffff );
830 while( border_length
)
832 v2i directions
[] = {{1,0},{0,1},{-1,0},{0,-1}};
834 v2i
*old_border
= cborder
;
835 int len
= border_length
;
838 cborder
= old_border
+len
;
840 for( int i
=0; i
<len
; i
++ )
843 v2i_copy( old_border
[i
], co
);
844 struct grid_sample
*sa
= &wg
.samples
[co
[1]][co
[0]];
846 for( int j
=0; j
<4; j
++ )
849 v2i_add( co
, directions
[j
], newp
);
851 if( newp
[0] < 0 || newp
[1] < 0 ||
852 newp
[0] == WALKGRID_SIZE
|| newp
[1] == WALKGRID_SIZE
)
855 struct grid_sample
*sb
= &wg
.samples
[newp
[1]][newp
[0]];
856 enum traverse_state thismove
= j
%2==0? 1: 2;
858 if( (sb
->state
& thismove
) == 0x00 ||
859 sb
->type
== k_sample_type_air
)
861 sb
->pos
[1] = sa
->pos
[1];
863 player_walkgrid_samplepole( sb
);
865 if( sb
->type
!= k_sample_type_air
)
868 * Need to do a blocker pass
871 struct grid_sample
*store
= (j
>>1 == 0)? sa
: sb
;
872 player_walkgrid_clip_blocker( sa
, sb
, store
, j
%2 );
875 if( sb
->type
!= k_sample_type_air
)
877 vg_line( sa
->pos
, sb
->pos
, 0xffffffff );
879 if( sb
->state
== k_traverse_none
)
880 v2i_copy( newp
, cborder
[ border_length
++ ] );
885 v3_muladds( sa
->pos
, store
->clip
[j
%2], k_gridscale
, p1
);
886 vg_line( sa
->pos
, p1
, 0xffffffff );
892 * A clipping pass is now done on the edge of the walkable
896 struct grid_sample
*store
= (j
>>1 == 0)? sa
: sb
;
897 player_walkgrid_clip_edge( sa
, sb
, store
, j
%2 );
900 v3_muladds( sa
->pos
, store
->clip
[j
%2], k_gridscale
, p1
);
901 vg_line( sa
->pos
, p1
, 0xffffffff );
904 sb
->state
|= thismove
;
908 sa
->state
= k_traverse_h
|k_traverse_v
;
912 if( iter
== walk_grid_iterations
)
916 /* Draw connections */
917 struct grid_sample
*corners
[4];
918 for( int x
=0; x
<WALKGRID_SIZE
-1; x
++ )
920 for( int z
=0; z
<WALKGRID_SIZE
-1; z
++ )
922 const struct conf
*conf
=
923 player_walkgrid_conf( &wg
, (v2i
){x
,z
}, corners
);
925 for( int i
=0; i
<conf
->edge_count
; i
++ )
927 const struct confedge
*edge
= &conf
->edges
[i
];
930 v3_muladds( corners
[edge
->i0
]->pos
,
931 corners
[edge
->d0
]->clip
[edge
->a0
], k_gridscale
, p0
);
932 v3_muladds( corners
[edge
->i1
]->pos
,
933 corners
[edge
->d1
]->clip
[edge
->a1
], k_gridscale
, p1
);
935 vg_line( p0
, p1
, 0xff0000ff );
941 * Commit player movement into the grid
944 if( v3_length2(delta
) <= 0.00001f
)
948 for(; i
<8 && wg
.move
> 0.001f
; i
++ )
949 player_walkgrid_iter( &wg
, i
);
951 player_walkgrid_stand_cell( &wg
);
954 static void player_walkgrid(void)
956 player_walkgrid_getsurface();
958 m4x3_mulv( player
.rb
.to_world
, (v3f
){0.0f
,1.8f
,0.0f
}, player
.camera_pos
);
960 rb_update_transform( &player
.rb
);
963 #endif /* PLAYER_WALKGRID_H */