- 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 );
-
- return;
- }
-
- 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;
-
- m3x3_mulv( player.to_world, (v3f){0.0f,0.0f,-1.0f}, fwd );
- m3x3_mulv( player.to_world, (v3f){0.03f,0.0f,-1.0f}, fwd1 );
- m3x3_mulv( player.to_world, (v3f){0.0f,1.0f,0.0f}, vup );
-
- v3_muladds( player.co, fwd, 1.0f, contact_front );
- v3_muladds( player.co, fwd,-1.0f, contact_back );
- v3_muladds( player.co, fwd1, 1.0f, contact_norm );
-
- sample_scene_height( &world_scene, contact_front );
- sample_scene_height( &world_scene, contact_back );
- sample_scene_height( &world_scene, contact_norm );
-
- v3f norm;
- v3f v0, v1;
- v3_sub( contact_back, contact_norm, v0 );
- v3_sub( contact_front, contact_norm, v1 );
- v3_cross( v1, v0, norm );
- v3_normalize( norm );
-
- v3f gravity = { 0.0f, -9.6f, 0.0f };
- v3_muladds( player.v, gravity, timestep, player.v );
-
- v3f ground_pos;
- v3_copy( player.co, ground_pos );
- sample_scene_height( &world_scene, ground_pos );
-
- static int in_air = 1;
-
- if( in_air )
- {
- if( ground_pos[1] > player.co[1] )
- in_air = 0;
- }
-
- 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;
-
- vg_line( player.co, contact_front, 0xff00ffff );
- vg_line( player.co, contact_back, 0xff00ffa0 );
-
- /* Create the 'travel' vector */
- v3f travel;
- v3_sub( contact_front, contact_back, travel );
- v3_normalize( travel );
-
- /* Apply gravity */
-#if 0
- float gravity_conversion = -v3_dot( travel, gravity );
- vel[2] += gravity_conversion * substep;
-#endif
-
- /* Get localized (rotated) rigidbody forces
- * -z
- * ^
- * -|-
- * |
- * +z
- */
-
- v3f vel;
- m3x3_mulv( player.to_local, player.v, vel );
-
- /* Calculate local forces */
- slip = -vel[0] / vel[2];
- float substep = timestep * 0.2f;
-
- if( fabsf( slip ) > 1.2f )
- {
- slip = vg_signf( slip ) * 1.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 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 bob, bob1;
-
- v3_sub( player.v, last_vel, player.a );
- v3_copy( player.v, last_vel );
- v3_add( bob, player.a, bob );
-
- bob[0] = vg_clampf( bob[0], -0.4f, 1.0f );
- bob[1] = vg_clampf( bob[1], -0.4f, 1.3f );
- bob[2] = vg_clampf( bob[2], -0.4f, 1.0f );
-
- v3_lerp( bob, (v3f){ 0.0f, 0.0f, 0.0f }, 0.1f, bob );
- v3_lerp( bob1, bob, 0.1f, bob1 );
-
- /* Head */
- head[0] = (-sinf(slip)*0.9f * kheight + bob1[0]*0.6f) * 0.54f;
- head[1] = cosf(slip)*0.9f * kheight +-bob1[1]*1.4f;
- head[2] = 0.0f;
-
- m4x3_mulv( player.to_world, head, head );
- v3_copy( head, player.view );
-
- player_transform_update();