- 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 )