Prechádzať zdrojové kódy

Clean examples more

Miloslav Ciz 3 rokov pred
rodič
commit
8bf5a761e3
6 zmenil súbory, kde vykonal 116 pridanie a 94 odobranie
  1. 1 0
      TODO.txt
  2. 1 1
      programs/levelModel.h
  3. 59 41
      programs/shoot.c
  4. 23 29
      programs/stack.c
  5. 25 23
      programs/testGeneral.c
  6. 7 0
      tinyphysicsengine.h

+ 1 - 0
TODO.txt

@@ -3,6 +3,7 @@ TODO:
 - add/remove log with TPE_LOG so that its nice, useful and not spammy
 - demos for showing basic use (without using helper.h)
 - FOR GODS SAKE clean the code
+- valgrind, cppcheck, read the whole code, refactor, test on embedded, ...
 
 DONE:
 - update the player demo (new 3D environment)

+ 1 - 1
programs/levelModel.h

@@ -241,7 +241,7 @@ const S3L_Index levelTriangleIndices[LEVEL_TRIANGLE_COUNT * 3] = {
 
 S3L_Model3D levelModel;
 
-void levelModelInit()
+void levelModelInit(void)
 {
   S3L_model3DInit(
     levelVertices,

+ 59 - 41
programs/shoot.c

@@ -1,15 +1,10 @@
-#include "helper.h"
+/** Tiny catapult Angry Birds like shooter demo. */
 
-#define CATAPULT_HEIGHT 1300
-#define CATAPULT_WIDTH 700
-#define BALL_RADIUS 200
+#include "helper.h"
 
-TPE_Unit sides[6] =
-{
-  0,0,
-  1000,1000,
-  -1000,2000
-};
+#define CATAPULT_HEIGHT (TPE_F * 2)
+#define CATAPULT_WIDTH (3 * TPE_F / 2)
+#define BALL_RADIUS (2 * TPE_F / 5)
 
 uint8_t collisionCallback(uint16_t b1, uint16_t j1, uint16_t b2, uint16_t j2,
   TPE_Vec3 p)
@@ -28,6 +23,7 @@ TPE_Connection connections[2];
 TPE_Body catapult;
 
 uint8_t released = 0;
+uint8_t anythingHit = 0;
 
 int main(void)
 {
@@ -54,32 +50,36 @@ int main(void)
 
   TPE_bodyInit(&tpe_bodies[0],joints,3,connections,2,10);
 
-  joints[1].position.x -= 800;
-  joints[1].position.y -= 200;
+  joints[1].position.x -= 2 * TPE_F;
+  joints[1].position.y -= TPE_F / 2;
 
   tpe_bodies[0].flags |= TPE_BODY_FLAG_SOFT;
-  tpe_bodies[0].flags |= TPE_BODY_FLAG_SIMPLE_CONN; // this makes the spring faster
+  tpe_bodies[0].flags |= TPE_BODY_FLAG_SIMPLE_CONN; // this makes the string faster
 
   tpe_world.bodyCount++;
 
-  helper_addBall(BALL_RADIUS,1000);
+  helper_addBall(BALL_RADIUS,2 * TPE_F);
 
-helper_addCenterBox(500,500,800,200,300);
-TPE_bodyMoveBy(&helper_lastBody,TPE_vec3(4000,600,-600));
-TPE_bodyDeactivate(&helper_lastBody);
+  helper_addCenterBox(TPE_F,TPE_F,3 * TPE_F / 2,2 * TPE_F / 5,TPE_F);
+  helper_lastBody.joints[8].sizeDivided *= 2;
+  TPE_bodyMoveBy(&helper_lastBody,TPE_vec3(8 * TPE_F,6 * TPE_F / 5,-6 * TPE_F / 5));
+  TPE_bodyDeactivate(&helper_lastBody);
 
-helper_addCenterBox(500,500,800,200,300);
-TPE_bodyMoveBy(&helper_lastBody,TPE_vec3(4000,600,600));
-TPE_bodyDeactivate(&helper_lastBody);
+  helper_addCenterBox(TPE_F,TPE_F,3 * TPE_F / 2,2 * TPE_F / 5,TPE_F);
+  helper_lastBody.joints[8].sizeDivided *= 2;
+  TPE_bodyMoveBy(&helper_lastBody,TPE_vec3(8 * TPE_F,6 * TPE_F / 5,6 * TPE_F / 5));
+  TPE_bodyDeactivate(&helper_lastBody);
 
-helper_addCenterRect(500,1800,300,300);
-TPE_bodyMoveBy(&helper_lastBody,TPE_vec3(4000,1400,0));
-TPE_bodyDeactivate(&helper_lastBody);
-
-helper_addCenterBox(600,600,900,200,300);
-TPE_bodyMoveBy(&helper_lastBody,TPE_vec3(4000,2200,0));
-TPE_bodyDeactivate(&helper_lastBody);
+  helper_addCenterRect(TPE_F,3 * TPE_F,TPE_F / 2,TPE_F);
+  helper_lastBody.joints[4].sizeDivided *= 3;
+  helper_lastBody.joints[4].sizeDivided /= 2;
+  TPE_bodyMoveBy(&helper_lastBody,TPE_vec3(8 * TPE_F,3 * TPE_F - TPE_F / 4,0));
+  TPE_bodyDeactivate(&helper_lastBody);
 
+  helper_addCenterBox(6 * TPE_F / 5,6 * TPE_F / 5,2 * TPE_F - TPE_F / 6,TPE_F / 3,TPE_F);
+  helper_lastBody.joints[8].sizeDivided *= 2;
+  TPE_bodyMoveBy(&helper_lastBody,TPE_vec3(8 * TPE_F,4 * TPE_F + TPE_F / 3,0));
+  TPE_bodyDeactivate(&helper_lastBody);
 
   while (helper_running)
   {
@@ -96,7 +96,8 @@ TPE_bodyDeactivate(&helper_lastBody);
       for (int i = 1; i < tpe_world.bodyCount; ++i)
         TPE_bodyApplyGravity(&tpe_world.bodies[i],6);
 
-      TPE_bodyMultiplyNetSpeed(&tpe_world.bodies[0],500);
+      // make the string lose energy over time:
+      TPE_bodyMultiplyNetSpeed(&tpe_world.bodies[0],(19 * TPE_F) / 20);
     }
 
     if (!released || tpe_world.bodies[1].joints[0].position.x < 0)
@@ -112,7 +113,7 @@ TPE_bodyDeactivate(&helper_lastBody);
 
     if (!released)
     {
-#define OFFSET 20
+#define OFFSET (TPE_F / 20)
       if (sdl_keyboard[SDL_SCANCODE_W])
         joints[1].position.y += OFFSET;
       else if (sdl_keyboard[SDL_SCANCODE_S] && joints[1].position.y > 0)
@@ -127,28 +128,42 @@ TPE_bodyDeactivate(&helper_lastBody);
         joints[1].position.z -= OFFSET;
       else if (sdl_keyboard[SDL_SCANCODE_A])
         joints[1].position.z += OFFSET;
+#undef OFFSET
+    }
+    else if (!anythingHit)
+    {
+      /* here we check if any of the tower bodies is active which means it has
+         been hit in which case we activate all the bodies so that they do their
+         thing and none stays hanging in the air :) */
+      for (int i = 2; i < tpe_world.bodyCount; ++i)
+      {
+        if (TPE_bodyIsActive(&tpe_world.bodies[i]))
+        {
+          puts("someting was hit, activating all bodies");
+          TPE_worldActivateAll(&tpe_world);
+          anythingHit = 1;
+          break;
+        }
+      } 
     }
-
-    if (helper_debugDrawOn)
-      helper_debugDraw(1);
 
     helper_set3DColor(200,200,255);
 
     helper_draw3DSphere(tpe_world.bodies[1].joints[0].position,TPE_vec3(BALL_RADIUS,BALL_RADIUS,BALL_RADIUS),TPE_vec3(0,0,0));
     
-helper_set3DColor(200,255,200);
+    helper_set3DColor(200,255,200);
 
-helper_draw3DBox(TPE_bodyGetCenterOfMass(&tpe_world.bodies[2]),TPE_vec3(700,1000,700),
-  TPE_bodyGetRotation(&tpe_world.bodies[2],0,1,2));
+    helper_draw3DBox(TPE_bodyGetCenterOfMass(&tpe_world.bodies[2]),TPE_vec3(700,1000,700),
+      TPE_bodyGetRotation(&tpe_world.bodies[2],0,1,2));
 
-helper_draw3DBox(TPE_bodyGetCenterOfMass(&tpe_world.bodies[3]),TPE_vec3(700,1000,700),
-  TPE_bodyGetRotation(&tpe_world.bodies[3],0,1,2));
+    helper_draw3DBox(TPE_bodyGetCenterOfMass(&tpe_world.bodies[3]),TPE_vec3(700,1000,700),
+      TPE_bodyGetRotation(&tpe_world.bodies[3],0,1,2));
 
-helper_draw3DBox(TPE_bodyGetCenterOfMass(&tpe_world.bodies[4]),TPE_vec3(2000,500,700),
-  TPE_bodyGetRotation(&tpe_world.bodies[4],0,1,2));
+    helper_draw3DBox(TPE_bodyGetCenterOfMass(&tpe_world.bodies[4]),TPE_vec3(2000,500,700),
+      TPE_bodyGetRotation(&tpe_world.bodies[4],0,1,2));
 
-helper_draw3DBox(TPE_bodyGetCenterOfMass(&tpe_world.bodies[5]),TPE_vec3(800,1100,800),
-  TPE_bodyGetRotation(&tpe_world.bodies[5],0,1,2));
+    helper_draw3DBox(TPE_bodyGetCenterOfMass(&tpe_world.bodies[5]),TPE_vec3(800,1100,800),
+      TPE_bodyGetRotation(&tpe_world.bodies[5],0,1,2));
 
     helper_drawLine3D(TPE_vec3(0,0,0),TPE_vec3(0,CATAPULT_HEIGHT / 2,0),255,0,0 );
     helper_drawLine3D(TPE_vec3(0,CATAPULT_HEIGHT / 2,0),TPE_vec3(0,CATAPULT_HEIGHT,-1 * CATAPULT_WIDTH / 2),255,0,0);
@@ -156,6 +171,9 @@ helper_draw3DBox(TPE_bodyGetCenterOfMass(&tpe_world.bodies[5]),TPE_vec3(800,1100
     helper_drawLine3D(TPE_vec3(0,CATAPULT_HEIGHT,-1 * CATAPULT_WIDTH / 2),joints[1].position,0,255,0);
     helper_drawLine3D(TPE_vec3(0,CATAPULT_HEIGHT,CATAPULT_WIDTH / 2),joints[1].position,0,255,0);
 
+    if (helper_debugDrawOn)
+      helper_debugDraw(1);
+
     helper_frameEnd();
   }
 

+ 23 - 29
programs/stack.c

@@ -1,12 +1,16 @@
+/** Program that drops many bodies so that they stack onto each other to test
+  this kind of behavior as well as a performance during it, also applies some
+  basic smoothing of movement and rotation. */
+
 //#define FPS 10
 
 #include "helper.h"
 
 TPE_Vec3 environmentDistance(TPE_Vec3 p, TPE_Unit maxD)
 {
-  TPE_ENV_START( TPE_envHalfPlane(p,TPE_vec3(0,0,0),TPE_vec3(256,256,0)),p )
-  TPE_ENV_NEXT( TPE_envHalfPlane(p,TPE_vec3(0,0,0),TPE_vec3(-256,256,-256)),p )
-  TPE_ENV_NEXT( TPE_envHalfPlane(p,TPE_vec3(0,0,0),TPE_vec3(-256,256,256)),p )
+  TPE_ENV_START( TPE_envHalfPlane(p,TPE_vec3(0,0,0),TPE_vec3(TPE_F / 2,TPE_F / 2,0)),p )
+  TPE_ENV_NEXT( TPE_envHalfPlane(p,TPE_vec3(0,0,0),TPE_vec3(-1 * TPE_F / 2,TPE_F / 2,-1 * TPE_F / 2)),p )
+  TPE_ENV_NEXT( TPE_envHalfPlane(p,TPE_vec3(0,0,0),TPE_vec3(-1 * TPE_F / 2,TPE_F / 2,TPE_F / 2)),p )
   TPE_ENV_END
 }
 
@@ -15,6 +19,12 @@ unsigned long timeMeasure = 0;
 TPE_Vec3 bodyPositions[16];
 TPE_Vec3 bodyOrientations[16];
 
+// tries to smooth orientation by averaging it over 2 frames
+TPE_Unit updateOrientation(TPE_Unit new, TPE_Unit old)
+{
+  return TPE_abs(new - old) < 20 ? (new + old) / 2 : new;
+}
+
 int main(void)
 {
   helper_init();
@@ -48,6 +58,8 @@ int main(void)
 
     if (helper_frame % 16 == 0)
     {
+      printf("frame %d:\n",helper_frame);
+
       helper_printCPU();
 
       if (sdl_keyboard[SDL_SCANCODE_L])
@@ -59,6 +71,7 @@ int main(void)
         }
 
       printf("world update (us): %lu\n",timeMeasure / 16);
+      printf("hash: %lu\n",TPE_worldHash(&tpe_world));
 
       timeMeasure = 0;
     }
@@ -69,14 +82,6 @@ int main(void)
 
     timeMeasure += helper_getMicroSecs() - t1;
 
-
-
-
-
-
-if (helper_frame == 200)
-  printf("hash: %lu\n",TPE_worldHash(&tpe_world));
-
     for (int i = 0; i < tpe_world.bodyCount; ++i)
     {
       TPE_bodyApplyGravity(&tpe_world.bodies[i],(5 * 30) / FPS);
@@ -101,24 +106,13 @@ if (helper_frame == 200)
     
       helper_set3DColor(100 + i * 5,16 - i,100 - i * 5); 
 
-bodyPositions[i] =
-TPE_vec3KeepWithinBox(
-bodyPositions[i],
-TPE_bodyGetCenterOfMass(&tpe_world.bodies[i]),
-TPE_vec3(20,20,20)
-);
-
-bodyOrientations[i].x = 
-(TPE_abs(bodyOrientations[i].x - orient.x) < 20) ?
-((bodyOrientations[i].x + orient.x) / 2) : orient.x;
-
-bodyOrientations[i].y = 
-(TPE_abs(bodyOrientations[i].y - orient.y) < 20) ?
-((bodyOrientations[i].y + orient.y) / 2) : orient.y;
-
-bodyOrientations[i].z = 
-(TPE_abs(bodyOrientations[i].z - orient.z) < 20) ?
-((bodyOrientations[i].z + orient.z) / 2) : orient.z;
+      // this smoothes the movement a bit:
+      bodyPositions[i] = TPE_vec3KeepWithinBox(bodyPositions[i],
+        TPE_bodyGetCenterOfMass(&tpe_world.bodies[i]),TPE_vec3(20,20,20));
+      
+      bodyOrientations[i].x = updateOrientation(orient.x,bodyOrientations[i].x);
+      bodyOrientations[i].y = updateOrientation(orient.y,bodyOrientations[i].y);
+      bodyOrientations[i].z = updateOrientation(orient.z,bodyOrientations[i].z);
 
       switch (i % 5)
       {

+ 25 - 23
programs/testGeneral.c

@@ -1,3 +1,6 @@
+/** A bigger testing playground, just to see that everyhing works OK. The code
+  is not very nice :) */
+
 #define CAMERA_STEP 200
 
 #include "helper.h"
@@ -110,37 +113,36 @@ int main(void)
 
     helper_cameraFreeMovement();
 
-if (simulating)
-{
-    TPE_worldStep(&tpe_world);
+    if (simulating)
+    {
+      TPE_worldStep(&tpe_world);
 
-for (int i = 0; i < 12; ++i)
-  TPE_bodyApplyGravity(&tpe_world.bodies[i],5);
-}
+      for (int i = 0; i < 12; ++i)
+        TPE_bodyApplyGravity(&tpe_world.bodies[i],5);
+    }
 
     if (helper_debugDrawOn)
       helper_debugDraw(1);
 
-if (sdl_keyboard[SDL_SCANCODE_P])
-  simulating = 1;
+    if (sdl_keyboard[SDL_SCANCODE_P])
+      simulating = 1;
 
-
-TPE_bodyMultiplyNetSpeed(controlledBody,255);
+    TPE_bodyMultiplyNetSpeed(controlledBody,255);
 
 #define ACC 100
-if (sdl_keyboard[SDL_SCANCODE_W])
-  TPE_bodyAccelerate(controlledBody,TPE_vec3(0,0,ACC));
-else if (sdl_keyboard[SDL_SCANCODE_S])
-  TPE_bodyAccelerate(controlledBody,TPE_vec3(0,0,-1 * ACC));
-else if (sdl_keyboard[SDL_SCANCODE_D])
-  TPE_bodyAccelerate(controlledBody,TPE_vec3(ACC,0,0));
-else if (sdl_keyboard[SDL_SCANCODE_A])
-  TPE_bodyAccelerate(controlledBody,TPE_vec3(-1 * ACC,0,0));
-else if (sdl_keyboard[SDL_SCANCODE_C])
-  TPE_bodyAccelerate(controlledBody,TPE_vec3(0,ACC,0));
-else if (sdl_keyboard[SDL_SCANCODE_X])
-  TPE_bodyAccelerate(controlledBody,TPE_vec3(0,-1 * ACC,0));
-
+    if (sdl_keyboard[SDL_SCANCODE_W])
+      TPE_bodyAccelerate(controlledBody,TPE_vec3(0,0,ACC));
+    else if (sdl_keyboard[SDL_SCANCODE_S])
+      TPE_bodyAccelerate(controlledBody,TPE_vec3(0,0,-1 * ACC));
+    else if (sdl_keyboard[SDL_SCANCODE_D])
+      TPE_bodyAccelerate(controlledBody,TPE_vec3(ACC,0,0));
+    else if (sdl_keyboard[SDL_SCANCODE_A])
+      TPE_bodyAccelerate(controlledBody,TPE_vec3(-1 * ACC,0,0));
+    else if (sdl_keyboard[SDL_SCANCODE_C])
+      TPE_bodyAccelerate(controlledBody,TPE_vec3(0,ACC,0));
+    else if (sdl_keyboard[SDL_SCANCODE_X])
+      TPE_bodyAccelerate(controlledBody,TPE_vec3(0,-1 * ACC,0));
+#undef ACC
 
     helper_frameEnd();
   }

+ 7 - 0
tinyphysicsengine.h

@@ -456,6 +456,7 @@ TPE_Vec3 TPE_castBodyRay(TPE_Vec3 rayPos, TPE_Vec3 rayDir, int16_t excludeBody,
 void TPE_worldStep(TPE_World *world);
 
 void TPE_worldDeactivateAll(TPE_World *world);
+void TPE_worldActivateAll(TPE_World *world);
 
 TPE_Unit TPE_worldGetNetSpeed(const TPE_World *world);
 TPE_Unit TPE_bodyGetNetSpeed(const TPE_Body *body);
@@ -2823,6 +2824,12 @@ void TPE_worldDeactivateAll(TPE_World *world)
     TPE_bodyDeactivate(&world->bodies[i]);
 }
 
+void TPE_worldActivateAll(TPE_World *world)
+{
+  for (uint16_t i = 0; i < world->bodyCount; ++i)
+    TPE_bodyActivate(&world->bodies[i]);
+}
+
 TPE_Unit TPE_worldGetNetSpeed(const TPE_World *world)
 {
   TPE_Unit result = 0;