Browse Source

Refactor a bit

Miloslav Ciz 3 years ago
parent
commit
540fd9ad80
5 changed files with 47 additions and 65 deletions
  1. 31 55
      programs/conservation.c
  2. 1 1
      programs/make.sh
  3. 6 4
      programs/player.c
  4. 7 4
      programs/stack.c
  5. 2 1
      tinyphysicsengine.h

+ 31 - 55
programs/conservation.c

@@ -10,8 +10,8 @@
 
 TPE_Vec3 environmentDistance(TPE_Vec3 p, TPE_Unit maxD)
 {
-TPE_ENV_START( TPE_envSphereInside(p,TPE_vec3(0,0,0),SPHERE_R),p )
-TPE_ENV_END
+  TPE_ENV_START( TPE_envSphereInside(p,TPE_vec3(0,0,0),SPHERE_R),p )
+  TPE_ENV_END
 }
 
 uint8_t debugDrawOn = 1;
@@ -26,7 +26,6 @@ int main(void)
 
   s3l_scene.camera.transform.translation.z = -1 * SPHERE_R - 1000;
 
-
   for (int i = 0; i < 4; ++i)
   {
     switch (i)
@@ -38,24 +37,23 @@ int main(void)
       default: break;
     }
 
-TPE_Body *b = &tpe_world.bodies[tpe_world.bodyCount - 1];
-
-    TPE_bodyMove(b,TPE_vec3((i - 2) * 1200,0,0));
-
+  TPE_Body *b = &tpe_world.bodies[tpe_world.bodyCount - 1];
 
-b->friction = 0;
-b->elasticity = TPE_FRACTIONS_PER_UNIT;
+  TPE_bodyMove(b,TPE_vec3((i - 2) * 1200,0,0));
 
-TPE_bodyAccelerate(b,
+  /* We don't want any energy losses due to friction and non-elastic collision,
+  so we'll turn them off. This alone won't be enough though as numeric errors
+  will still change the total energy. */
 
-TPE_vec3Plus(
-TPE_vec3Times(TPE_bodyGetCenterOfMass(b),50),
-TPE_vec3(0,10,0))
+  b->friction = 0;
+  b->elasticity = TPE_FRACTIONS_PER_UNIT;
 
-);
-  } 
+  TPE_bodyAccelerate(b, // give some initial velocity
+    TPE_vec3Plus(TPE_vec3Times(TPE_bodyGetCenterOfMass(b),55),TPE_vec3(0,8,0)));
+  }
 
-TPE_Unit sTotal = TPE_worldGetNetSpeed(&tpe_world);
+  TPE_Unit sTotal = TPE_worldGetNetSpeed(&tpe_world);
+  // ^ keep the record of total speed
 
   while (helper_running)
   {
@@ -63,45 +61,23 @@ TPE_Unit sTotal = TPE_worldGetNetSpeed(&tpe_world);
 
     helper_cameraFreeMovement();
 
-    if (helper_frame % 16 == 0)
-    {
-      //helper_printCPU();
-      //helper_printCamera();
-
-      if (sdl_keyboard[SDL_SCANCODE_L])
-        for (int i = 0; i < tpe_world.bodyCount; ++i)
-        {
-          TPE_bodyActivate(&tpe_world.bodies[i]);
-          TPE_bodyAccelerate(&tpe_world.bodies[i],
-            TPE_vec3(0,(500 * 30) / FPS,0));
-        }
-
-
-      timeMeasure = 0;
-    }
-
-unsigned long t1 = helper_getMicroSecs();
-
     TPE_worldStep(&tpe_world);
 
-timeMeasure += helper_getMicroSecs() - t1;
-
-    helper_set3dColor(180,10,10); 
-
-TPE_Unit s = TPE_worldGetNetSpeed(&tpe_world);
-
-TPE_Unit ratio = (sTotal * TPE_FRACTIONS_PER_UNIT) / TPE_nonZero(s);
+    TPE_Unit s = TPE_worldGetNetSpeed(&tpe_world);
+    TPE_Unit ratio = (sTotal * TPE_FRACTIONS_PER_UNIT) / TPE_nonZero(s);
 
-if (ratio < (4 * TPE_FRACTIONS_PER_UNIT) / 5 ||
-  ratio > (6 * TPE_FRACTIONS_PER_UNIT) / 5)
-{
+    if (ratio < (4 * TPE_FRACTIONS_PER_UNIT) / 5 ||
+      ratio > (6 * TPE_FRACTIONS_PER_UNIT) / 5)
+    {
+      // if total speed changed by more than 1/5, we adjust it
 
-printf("net speed is now %d but needs to be %d, correcting!\n",s,sTotal);
+      printf("net speed is now %d but needs to be %d, correcting!\n",s,sTotal);
 
-for (int i = 0; i < tpe_world.bodyCount; ++i)
-  TPE_bodyMultiplyNetSpeed(&tpe_world.bodies[i],ratio);
+      for (int i = 0; i < tpe_world.bodyCount; ++i)
+        TPE_bodyMultiplyNetSpeed(&tpe_world.bodies[i],ratio);
+    }
 
-}
+    helper_set3dColor(200,10,10); 
 
     for (int i = 0; i < tpe_world.bodyCount; ++i)
     {
@@ -110,10 +86,10 @@ for (int i = 0; i < tpe_world.bodyCount; ++i)
       TPE_Vec3 right = TPE_vec3(512,0,0);
       TPE_Vec3 forw = TPE_vec3(0,0,512);
 
-TPE_bodyActivate(&tpe_world.bodies[i]);
+      TPE_bodyActivate(&tpe_world.bodies[i]); // don't let bodies deactivate
 
-      if (i != 1)
-      { 
+      if (i != 1) // ugly code to get the correct orientation :)
+      {
         if (i != 3)
         {
           forw = TPE_vec3Minus(joints[2].position,joints[0].position);
@@ -125,7 +101,7 @@ TPE_bodyActivate(&tpe_world.bodies[i]);
 
       TPE_Vec3 orient = TPE_rotationFromVecs(forw,right);
 
-      switch (i % 5)
+      switch (i % 5) // and draw the correct shape
       {
         case 0: helper_draw3dBox(pos,TPE_vec3(1200,1200,1200),orient); break;
         case 1: helper_draw3dSphere(pos,TPE_vec3(500,500,500),orient); break;
@@ -137,8 +113,8 @@ TPE_bodyActivate(&tpe_world.bodies[i]);
 
     helper_set3dColor(200,200,200); 
 
-helper_draw3dSphereInside(TPE_vec3(0,0,0),TPE_vec3(SPHERE_R,SPHERE_R,SPHERE_R),
-  TPE_vec3(0,0,0) );
+    helper_draw3dSphereInside(TPE_vec3(0,0,0),
+      TPE_vec3(SPHERE_R,SPHERE_R,SPHERE_R),TPE_vec3(0,0,0));
 
     if (helper_debugDrawOn)
       helper_debugDraw(1);

+ 1 - 1
programs/make.sh

@@ -1,5 +1,5 @@
 #!/bin/bash
 
-PROGRAM=conservation # change this to name of a program you want to compile :)
+PROGRAM=stack # change this to name of a program you want to compile :)
 
 clear; clear; g++ -x c -g -fmax-errors=5 -pedantic -O3 -Wall -Wextra -Wstrict-prototypes -Wold-style-definition -Wno-unused-parameter -Wno-missing-field-initializers -o $PROGRAM $PROGRAM.c -lm -lSDL2 && ./$PROGRAM

+ 6 - 4
programs/player.c

@@ -1,5 +1,8 @@
 #define SCALE_3D_RENDERING 1
 
+#define S3L_NEAR_CROSS_STRATEGY 2
+#define S3L_PERSPECTIVE_CORRECTION 1
+
 #include "helper.h"
 
 #define ROOM_SIZE 10000
@@ -43,8 +46,6 @@ int main(void)
 {
   helper_init();
 
-  helper_debugDrawOn = 1;
-
   updateDirection();
 
   ballRot = TPE_vec3(0,0,0);
@@ -159,8 +160,9 @@ ballPreviousPos = tpe_world.bodies[1].joints[0].position;
 
     updateDirection();
 
-    helper_set3dColor(100,100,100);
+    helper_set3dColor(180,180,180);
     helper_draw3dBoxInside(TPE_vec3(0,ROOM_SIZE / 4,0),TPE_vec3(ROOM_SIZE,ROOM_SIZE / 2,ROOM_SIZE),TPE_vec3(0,0,0));
+    helper_set3dColor(100,200,180);
     helper_draw3dBox(TPE_vec3(4000,160,4000),TPE_vec3(2000,320,2000),TPE_vec3(0,0,0));
     helper_draw3dBox(TPE_vec3(4000,80,2500),TPE_vec3(2000,160,1000),TPE_vec3(0,0,0));
     helper_draw3dBox(TPE_vec3(-1000,270,4500),TPE_vec3(8000,540,500),TPE_vec3(0,0,0));
@@ -194,7 +196,7 @@ for (int i = 0; i < tpe_world.bodyCount; ++i)
     TPE_bodyApplyGravity(&tpe_world.bodies[i],5);
 
     if (helper_debugDrawOn)
-      helper_debugDraw();
+      helper_debugDraw(1);
 
     helper_frameEnd();
   }

+ 7 - 4
programs/stack.c

@@ -20,8 +20,9 @@ int main(void)
 
   tpe_world.environmentFunction = environmentDistance;
 
-  s3l_scene.camera.transform.translation.y = 3000;
-  s3l_scene.camera.transform.translation.z = -1000;
+  s3l_scene.camera.transform.translation.y = 6000;
+  s3l_scene.camera.transform.translation.z = -2000;
+  s3l_scene.camera.transform.rotation.x = -70;
 
   for (int i = 0; i < 16; ++i)
   {
@@ -71,7 +72,6 @@ unsigned long t1 = helper_getMicroSecs();
 
 timeMeasure += helper_getMicroSecs() - t1;
 
-    helper_set3dColor(180,10,10); 
 
 
 
@@ -96,6 +96,9 @@ timeMeasure += helper_getMicroSecs() - t1;
       }
 
       TPE_Vec3 orient = TPE_rotationFromVecs(forw,right);
+    
+    
+helper_set3dColor(100 + i * 5,16 - i,100 - i * 5); 
 
       switch (i % 5)
       {
@@ -116,7 +119,7 @@ timeMeasure += helper_getMicroSecs() - t1;
     helper_draw3dTriangle(TPE_vec3(0,0,0),TPE_vec3(-5000,5000,-10000),TPE_vec3(5000,5000,0));
 
     if (helper_debugDrawOn)
-      helper_debugDraw();
+      helper_debugDraw(1);
 
     helper_frameEnd();
   }

+ 2 - 1
tinyphysicsengine.h

@@ -401,7 +401,8 @@ TPE_Vec3 TPE_envInfiniteCylinder(TPE_Vec3 point, TPE_Vec3 center, TPE_Vec3
   direction, TPE_Unit radius);
 
 #define TPE_ENV_START(test,point) TPE_Vec3 _pBest = test, _pTest; \
-  TPE_Unit _dBest = TPE_DISTANCE(_pBest,point), _dTest;
+  TPE_Unit _dBest = TPE_DISTANCE(_pBest,point), _dTest; \
+  (void)(_pBest); (void)(_dBest); (void)(_dTest); (void)(_pTest); // supress war
 
 #define TPE_ENV_NEXT(test,point) \
  { if (_pBest.x == point.x && _pBest.y == point.y && _pBest.z == point.z) \