revision 2
[carveJwlIkooP6JGAAIwe30JlM.git] / ent_traffic.c
1 #include "world.h"
2
3 static void ent_traffic_update( world_instance *world, v3f pos ){
4 for( u32 i=0; i<mdl_arrcount( &world->ent_traffic ); i++ ){
5 ent_traffic *traffic = mdl_arritm( &world->ent_traffic, i );
6
7 u32 i1 = traffic->index,
8 i0,
9 i2 = i1+1;
10
11 if( i1 == 0 ) i0 = traffic->node_count-1;
12 else i0 = i1-1;
13
14 if( i2 >= traffic->node_count ) i2 = 0;
15
16 i0 += traffic->start_node;
17 i1 += traffic->start_node;
18 i2 += traffic->start_node;
19
20 v3f h[3];
21
22 ent_route_node *rn0 = mdl_arritm( &world->ent_route_node, i0 ),
23 *rn1 = mdl_arritm( &world->ent_route_node, i1 ),
24 *rn2 = mdl_arritm( &world->ent_route_node, i2 );
25
26 v3_copy( rn1->co, h[1] );
27 v3_lerp( rn0->co, rn1->co, 0.5f, h[0] );
28 v3_lerp( rn1->co, rn2->co, 0.5f, h[2] );
29
30 float const k_sample_dist = 0.0025f;
31 v3f pc, pd;
32 eval_bezier3( h[0], h[1], h[2], traffic->t, pc );
33 eval_bezier3( h[0], h[1], h[2], traffic->t+k_sample_dist, pd );
34
35 v3f v0;
36 v3_sub( pd, pc, v0 );
37 float length = vg_maxf( 0.0001f, v3_length( v0 ) );
38 v3_muls( v0, 1.0f/length, v0 );
39
40 float mod = k_sample_dist / length;
41
42 traffic->t += traffic->speed * vg.time_delta * mod;
43
44 if( traffic->t > 1.0f ){
45 traffic->t -= 1.0f;
46
47 if( traffic->t > 1.0f ) traffic->t = 0.0f;
48
49 traffic->index ++;
50
51 if( traffic->index >= traffic->node_count )
52 traffic->index = 0;
53 }
54
55 v3_copy( pc, traffic->transform.co );
56
57 float a = atan2f( -v0[0], v0[2] );
58 q_axis_angle( traffic->transform.q, (v3f){0.0f,1.0f,0.0f}, -a );
59
60 vg_line_point( traffic->transform.co, 0.3f, VG__BLUE );
61 }
62 }