Browse Source

Continue coll

Miloslav Číž 4 years ago
parent
commit
4780bd4b25
2 changed files with 51 additions and 6 deletions
  1. 10 4
      test_sdl.c
  2. 41 2
      tinyphysicsengine.h

+ 10 - 4
test_sdl.c

@@ -346,7 +346,7 @@ int main()
 //  addBody(TPE_SHAPE_CAPSULE,256,0,0);
 //addBody(TPE_SHAPE_CAPSULE,300,1024,0);
 
-  addBody(TPE_SHAPE_CUBOID,1500,1000,900);
+  addBody(TPE_SHAPE_CUBOID,512,512,512);
   addBody(TPE_SHAPE_CUBOID,800,1100,1200);
 
   //-------
@@ -369,12 +369,17 @@ bodies[1].body.position.z = -100;
 
 bodies[0].body.position = TPE_vec4(2875,-950,0,0);
 bodies[1].body.position = TPE_vec4(-1725,-550,-100,0);
-bodies[0].body.velocity = TPE_vec4(15,0,0,0);
+bodies[0].body.velocity = TPE_vec4(0,0,0,0);
+
+TPE_bodyApplyImpulse(&(bodies[0].body),TPE_vec4(256,0,0,0),TPE_vec4(-1,-1,-1,0));
+
+printf("%d\n",bodies[0].body.rotation.axisVelocity.w);
+
 //bodies[1].body.velocity = TPE_vec4(0,100,0,0);
 
 //bodies[0].body.velocity = TPE_vec4(150,100,0,0);
 
-TPE_bodySetRotation(&(bodies[0].body),TPE_vec4(0,128,0,0),10);
+//TPE_bodySetRotation(&(bodies[0].body),TPE_vec4(0,128,0,0),10);
 //TPE_bodySetRotation( &(bodies[1].body),TPE_vec4(210,50,1,0),5);
 /*
 TPE_Vec4 quat;
@@ -415,10 +420,11 @@ for (int i = 0; i < bodyCount; ++i)
     bodies[i].body.velocity.z *= -1;
 }
 
+/*
 printf("\nkin. energy: %d\n",
   TPE_bodyGetKineticEnergy(&bodies[0].body) +
   TPE_bodyGetKineticEnergy(&bodies[1].body));
-
+*/
 
     TPE_Unit collDepth = TPE_bodyCollides(&(bodies[1].body),&(bodies[0].body),&p,&n);
 

+ 41 - 2
tinyphysicsengine.h

@@ -232,6 +232,12 @@ void TPE_bodyAddRotation(TPE_Body *body, TPE_Vec4 axis, TPE_Unit velocity);
   body center), which will change its linear and/or angular velocity. This is
   similar to an impulse but doesn't take mass into account, only velocity. */
 void TPE_bodyApplyVelocity(TPE_Body *body, TPE_Vec4 point, TPE_Vec4 velocity);
+  // TODO: DELETE THIS SHIT ^
+
+/** Applies impulse (force in short time) to a body at a specified point
+  (relative to its center), which will potentially change its linear and/or
+  angular velocity. */
+  void TPE_bodyApplyImpulse(TPE_Body *body, TPE_Vec4 point, TPE_Vec4 impulse);
 
 /** Computes and returns a body's bounding sphere radius, i.e. the maximum
   extent from its center point. */
@@ -556,6 +562,41 @@ TPE_Vec4 TPE_vec3Cross(TPE_Vec4 a, TPE_Vec4 b)
   return a;
 }
 
+void TPE_bodyApplyImpulse(TPE_Body *body, TPE_Vec4 point, TPE_Vec4 impulse)
+{
+  TPE_Unit pointDistance = TPE_vec3Len(point);
+
+  if (pointDistance != 0)  
+  {
+    impulse.x = (impulse.x * TPE_FRACTIONS_PER_UNIT) / body->mass;
+    impulse.y = (impulse.y * TPE_FRACTIONS_PER_UNIT) / body->mass;
+    impulse.z = (impulse.z * TPE_FRACTIONS_PER_UNIT) / body->mass;
+  
+    TPE_vec3Add(body->velocity,impulse,&(body->velocity));
+
+    /* normalize the point, we don't use the function as we don't want to    
+       recompute the vector length */
+
+    point.x = (point.x * TPE_FRACTIONS_PER_UNIT) / pointDistance;
+    point.y = (point.y * TPE_FRACTIONS_PER_UNIT) / pointDistance;
+    point.z = (point.z * TPE_FRACTIONS_PER_UNIT) / pointDistance;
+
+    /* for simplicity we'll suppose angular momentum of a sphere: */
+
+    impulse = TPE_vec3Cross(impulse,point);
+
+    TPE_Unit r = TPE_bodyGetMaxExtent(body);
+
+    r = (2 * r * r) / TPE_FRACTIONS_PER_UNIT;
+
+    impulse.x = (5 * impulse.x * TPE_FRACTIONS_PER_UNIT) / r;   
+    impulse.y = (5 * impulse.y * TPE_FRACTIONS_PER_UNIT) / r;   
+    impulse.z = (5 * impulse.z * TPE_FRACTIONS_PER_UNIT) / r;   
+
+    TPE_bodyAddRotation(body,impulse,TPE_vec3Len(impulse));
+  }
+}
+
 void TPE_bodyApplyVelocity(TPE_Body *body, TPE_Vec4 point, TPE_Vec4 velocity)
 { 
   TPE_Unit pointDistance = TPE_vec3Len(point);
@@ -1847,8 +1888,6 @@ if (r == 0 && body->rotation.axisVelocity.w != 0)
   r = 1;
 
   return v + r;
-
-  // TODO: rot
 }
 
 TPE_Unit TPE_bodyGetMaxExtent(const TPE_Body *body)