- float timestep = 1.0f/60.0f;
-
- if( freecam )
- {
- m4x3f cam_rot;
- m4x3_identity( cam_rot );
- m4x3_rotate_y( cam_rot, -player.look_dir[0] );
- m4x3_rotate_x( cam_rot, -player.look_dir[1] );
-
- v3f lookdir = { 0.0f, 0.0f, -1.0f },
- sidedir = { 1.0f, 0.0f, 0.0f };
-
- m4x3_mulv( cam_rot, lookdir, lookdir );
- m4x3_mulv( cam_rot, sidedir, sidedir );
-
- float movespeed = 5.0f;
- static v2f mouse_last,
- view_vel = { 0.0f, 0.0f };
- static v3f move_vel = { 0.0f, 0.0f, 0.0f };
-
- if( vg_get_button_down( "primary" ) )
- {
- v2_copy( vg_mouse, mouse_last );
- }
- else if( vg_get_button( "primary" ) )
- {
- v2f delta;
- v2_sub( vg_mouse, mouse_last, delta );
- v2_copy( vg_mouse, mouse_last );
-
- v2_muladds( view_vel, delta, 0.005f, view_vel );
- }
-
- v2_muls( view_vel, 0.75f, view_vel );
- v2_add( view_vel, player.look_dir, player.look_dir );
- player.look_dir[1] =
- vg_clampf( player.look_dir[1], -VG_PIf*0.5f, VG_PIf*0.5f );
-
- if( vg_get_button( "forward" ) )
- v3_muladds( move_vel, lookdir, timestep * movespeed, move_vel );
- if( vg_get_button( "back" ) )
- v3_muladds( move_vel, lookdir, timestep *-movespeed, move_vel );
- if( vg_get_button( "left" ) )
- v3_muladds( move_vel, sidedir, timestep *-movespeed, move_vel );
- if( vg_get_button( "right" ) )
- v3_muladds( move_vel, sidedir, timestep * movespeed, move_vel );
-
- v3_muls( move_vel, 0.75f, move_vel );
- v3_add( move_vel, player.view, player.view );
- }
-
-
- static int in_air = 1;
-
- v3f ground_pos, ground_norm;
- v3_copy( player.co, ground_pos );
-
- if( sample_scene_height( &world.geo, ground_pos, ground_norm ) )
- {
-#if 0
- v3f localup;
- m3x3_mulv( player.to_world, (v3f){0.0f,1.0f,0.0f}, localup );
- v3_normalize(localup);
- v3_normalize(ground_norm);
-
- float angle = v3_dot( localup, ground_norm );
- v3f axis;
- v3_cross( localup, ground_norm, axis );
-
- if( angle < 0.999f && !in_air )
- {
- v4f correction;
- q_axis_angle( correction, axis, acosf(angle) );
- q_mul( correction, player.rot, player.rot );
- }
-#endif
- }
-
- if( freecam )
- return;
-
- if( in_air )
- {
- v3f pco, pco1, pv;
-
- float pstep = timestep*10.0f;
-
- v3f gravity = { 0.0f, -9.6f, 0.0f };
- v3_copy( player.co, pco );
- v3_copy( player.v, pv );
-
- v3f targetn;
-
- for( int i=0; i<20; i++ )
- {
- v3_copy( pco, pco1 );
- v3_muladds( pv, gravity, pstep, pv );
- v3_muladds( pco, pv, pstep, pco );
-
- vg_line( pco, pco1, i&0x1?0xff000000:0xffffffff );
-
- v3f sh;
- v3_copy( pco, sh );
- sample_scene_height( &world.geo, sh, targetn );
-
- if( sh[1] >= pco[1] )
- {
- v3f localup;
- m3x3_mulv( player.to_world, (v3f){0.0f,1.0f,0.0f}, localup );
-
- float angle = v3_dot( localup, targetn );
- v3f axis;
- v3_cross( localup, targetn, axis );
-
- if( angle < 0.99f )
- {
- v4f correction;
- q_axis_angle( correction, axis, acosf(angle)*0.1f );
- q_mul( correction, player.rot, player.rot );
- }
-
- break;
- }
- }
-
- if( ground_pos[1] > player.co[1] )
- {
- in_air = 0;
- }
- }
-
- if( vg_get_button( "forward" ) )
- {
- v3f dir = { 0.0f, 0.0f, -1.0f };
-
- m3x3_mulv( player.to_world, dir, dir );
- v3_muladds( player.v, dir, 5.0f * timestep, player.v );
- }
-
- /* Get front and back contact points */
-
- v3f contact_front, contact_back, fwd, fwd1, contact_norm, vup, vside;
- v3f axis;
-
- m3x3_mulv( player.to_world, (v3f){0.0f,0.0f,-1.0f}, fwd );
- m4x3_mulv( player.to_world, (v3f){ 0.15f,0.0f,-1.0f}, contact_norm );
- m4x3_mulv( player.to_world, (v3f){-0.15f,0.0f,-1.0f}, contact_front );
- m4x3_mulv( player.to_world, (v3f){ 0.00f,0.0f, 1.0f}, contact_back );
- m3x3_mulv( player.to_world, (v3f){0.0f,1.0f,0.0f}, vup );
- m3x3_mulv( player.to_world, (v3f){1.0f,0.0f,0.0f}, vside );
-
- int all_contacting =
- sample_scene_height( &world.geo, contact_front, NULL ) &&
- sample_scene_height( &world.geo, contact_back, NULL ) &&
- sample_scene_height( &world.geo, contact_norm, NULL );
-
- v3f norm;
- v3f v0, v1;
- v3_sub( contact_norm, contact_front, v0 );
- v3_sub( contact_back, contact_front, v1 );
- v3_cross( v1, v0, norm );
- v3_normalize( norm );
-
- vg_line( contact_norm, contact_front, 0xff00ff00 );
- vg_line( contact_back, contact_front, 0xff0000ff );
-
- /* Surface alignment */
- float angle = v3_dot( vup, norm );
- v3_cross( vup, norm, axis );
-
- if( angle < 0.999f && !in_air )
- {
- v4f correction;
- q_axis_angle( correction, axis, acosf(angle) );
- q_mul( correction, player.rot, player.rot );
- }
-
-
- v3f gravity = { 0.0f, -9.6f, 0.0f };
- v3_muladds( player.v, gravity, timestep, player.v );
-
- if( !in_air )
- {
- float resistance = v3_dot( norm, player.v );
-
- if( resistance >= 0.0f )
- in_air = 1;
- else
- {
- v3_muladds( player.v, norm, -resistance, player.v );
- }
- }
-
- /* vg_info( "%.3f | %.3f\n", player.v[1], resistance ); */
- v3_muladds( player.co, player.v, timestep, player.co );
-
- float slip = 0.0f;
- float yawamt = 0.0f;
-
- if( !in_air )
- {
- player.co[1] = (contact_front[1]+contact_back[1])*0.5f;
-
- v3f vel;
- m3x3_mulv( player.to_local, player.v, vel );
-
- /* Calculate local forces */
-
- slip = (-vel[0] / vel[2]) * player.vswitch;
- if( fabsf( slip ) > 1.2f )
- slip = vg_signf( slip ) * 1.2f;
-
-
- if( player.slip_last*slip < 0.0f && fabsf(slip) > 0.7f )
- {
- vg_warn( "SWITCH\n" );
- player.vswitch = -player.vswitch;
- slip = -slip;
- }
-
- player.slip_last = slip;
-
- float substep = timestep * 0.2f;
-
- for( int i=0; i<5; i++ )
- {
- if( fabsf(vel[2]) >= 0.02f*substep )
- vel[2] += vg_signf( vel[2] ) * -0.02f * substep;
- if( fabsf(vel[0]) >= 6.0f*substep )
- vel[0] += vg_signf( vel[0] ) * -6.0f * substep;
- }
-
- m3x3_mulv( player.to_world, vel, player.v );
-
- if( vg_get_button( "yawl" ) )
- yawamt = 1.6f * timestep;
- if( vg_get_button( "yawr" ) )
- yawamt = -1.6f * timestep;
-
- yawamt -= vg_get_axis( "horizontal" ) * 1.6f * timestep;
-
- }
- else
- {
- yawamt -= vg_get_axis( "horizontal" ) * 3.6f * timestep;
-
- v4f pitch;
- q_axis_angle( pitch, vside, vg_get_axis( "vertical" ) * 3.6f *timestep );
- q_mul( pitch, player.rot, player.rot );
- }
-
- v4f rotate;
- q_axis_angle( rotate, vup, yawamt );
- q_mul( rotate, player.rot, player.rot );
-
- player.look_dir[0] = atan2f( player.v[0], -player.v[2] );
-
- /* Creating a skeleton of the player dynamically */
- float kheight = 1.8f,
- kleg = 0.6f;
-
- v2f ac;
-
- static v3f last_vel = { 0.0f, 0.0f, 0.0f };
- static v3f momentum, bob;
-
- v3_sub( player.v, last_vel, player.a );
- v3_copy( player.v, last_vel );
-
- v3_add( momentum, player.a, momentum );
-
- v3_lerp( momentum, (v3f){0.0f,0.0f,0.0f}, 0.1f, momentum );
- v3f target;
-
- momentum[0] = vg_clampf( momentum[0], -2.0f, 2.0f );
- momentum[1] = vg_clampf( momentum[1], -0.2f, 5.0f );
- momentum[2] = vg_clampf( momentum[2], -2.0f, 2.0f );
- v3_copy( momentum, target );
-
- v3_lerp( bob, target, 0.2f, bob );
-
- /* Head */
- float lslip = fabsf(slip); //vg_minf( 0.4f, slip );
- head[0] = 0.0f;//(-sinf(lslip)*0.9f * kheight) * 0.44f;
- head[1] = (0.3f + cosf(lslip)*0.5f) * kheight;
- head[2] = 0.0f;
-
- v3f offset;
- m3x3_mulv( player.to_local, bob, offset );
- offset[0] *= 0.25f;
- offset[1] *= -0.25f;
- offset[2] *= 0.7f;
- v3_muladds( head, offset, 1.0f, head );
-
- player_transform_update();
-
- m4x3_mulv( player.to_world, head, head );
- v3_copy( head, player.view );
-
- q_normalize(player.rot);