MENY
[carveJwlIkooP6JGAAIwe30JlM.git] / rigidbody.h
index 282643a1eb0b01f6672207ea6e894d40f4beae46..b9b863aad2db7c12ddbdb07ae5d15d4c95998204 100644 (file)
@@ -24,8 +24,8 @@ static bh_system bh_system_rigidbodies;
  */
 
 static const float 
-   k_rb_rate          = 60.0f,
-   k_rb_delta         = (1.0f/k_rb_rate),
+   k_rb_rate          = (1.0/VG_TIMESTEP_FIXED),
+   k_rb_delta         = (1.0/k_rb_rate),
    k_friction         = 0.6f,
    k_damp_linear      = 0.05f,               /* scale velocity 1/(1+x) */
    k_damp_angular     = 0.1f,                /* scale angular  1/(1+x) */
@@ -330,6 +330,40 @@ static void rb_update_transform( rigidbody *rb )
    rb_update_bounds( rb );
 }
 
+/* 
+ * Extrapolate rigidbody into a transform based on vg accumulator.
+ * Useful for rendering
+ */
+static void rb_extrapolate_transform( rigidbody *rb, m4x3f transform )
+{
+   float substep = vg_clampf( vg.accumulator / k_rb_delta, 0.0f, 1.0f );
+
+   v3f co;
+   v4f q;
+
+   v3_muladds( rb->co, rb->v, k_rb_delta*substep, co );
+
+   if( v3_length2( rb->w ) > 0.0f )
+   {
+      v4f rotation;
+      v3f axis;
+      v3_copy( rb->w, axis );
+      
+      float mag = v3_length( axis );
+      v3_divs( axis, mag, axis );
+      q_axis_angle( rotation, axis, mag*k_rb_delta*substep );
+      q_mul( rotation, rb->q, q );
+      q_normalize( q );
+   }
+   else
+   {
+      v4_copy( rb->q, q );
+   }
+
+   q_m3x3( q, transform );
+   v3_copy( co, transform[3] );
+}
+
 /*
  * Initialize rigidbody and calculate masses, inertia
  */
@@ -1866,10 +1900,10 @@ static void rb_effect_simple_bouyency( rigidbody *ra, v4f plane,
    float depth  = v3_dot( plane, ra->co ) - plane[3],
          lambda = vg_clampf( -depth, 0.0f, 1.0f ) * amt;
 
-   v3_muladds( ra->v, plane, lambda * VG_TIMESTEP_FIXED, ra->v );
+   v3_muladds( ra->v, plane, lambda * k_rb_delta, ra->v );
 
    if( depth < 0.0f )
-      v3_muls( ra->v, 1.0f-(drag*VG_TIMESTEP_FIXED), ra->v );
+      v3_muls( ra->v, 1.0f-(drag*k_rb_delta), ra->v );
 }
 
 /*