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