Selaa lähdekoodia

Update level demo

Miloslav Ciz 3 vuotta sitten
vanhempi
sitoutus
0a6f54dc14
6 muutettua tiedostoa jossa 351 lisäystä ja 30 poistoa
  1. 4 3
      TODO.txt
  2. 2 2
      programs/heightmap.c
  3. 254 0
      programs/levelModel.h
  4. 72 23
      programs/player.c
  5. 19 0
      programs/test.c
  6. 0 2
      tinyphysicsengine.h

+ 4 - 3
TODO.txt

@@ -5,11 +5,10 @@ TODO:
 - demo: ragdoll
 - FOR GODS SAKE clean the code
 - current dist approx fails e.g. with heightmaps -- maybe just use the
-  non-approx version at all times in places where the approx fails?
+  non-approx version at all times in places where the approx fails? Also
+  TEST THE APPROX DIST with different env functions
 - bounding box/sphere test functions/macros for optimization of environment
   building, plus a demo that tests if it actually accelerates it
-- fast and accurate distance computation as a compile time option, dist comp is
-  prolly a bottleneck
 
 DONE:
 - env function: heightmap
@@ -19,6 +18,8 @@ DONE:
 - try to make better car physics
 - demo: testing different frictions on skewed plane, also different elasticities
 - test env functions with a single distinct point near camera
+- fast and accurate distance computation as a compile time option, dist comp is
+  prolly a bottleneck
 - function that tests validity of an environemnt function, i.e. if it behaves
   mathematically correct + test built in env functions with it
 - bug? bodies stuck inside each other resist falling down by gravity (stack.c)

+ 2 - 2
programs/heightmap.c

@@ -14,8 +14,8 @@ TPE_Unit height(int32_t x, int32_t y)
   y *= 8;
 
   return 
-   TPE_sin(x + TPE_cos(y * 2)) * TPE_sin(y * 2 + TPE_cos(x * 4)) /
-    (TPE_FRACTIONS_PER_UNIT / 2);
+    TPE_sin(x + TPE_cos(y * 2)) * TPE_sin(y * 2 + TPE_cos(x * 4)) /
+     (TPE_FRACTIONS_PER_UNIT / 2);
 }
 
 TPE_Vec3 environmentDistance(TPE_Vec3 p, TPE_Unit maxD)

+ 254 - 0
programs/levelModel.h

@@ -0,0 +1,254 @@
+#ifndef LEVEL_MODEL_H
+#define LEVEL_MODEL_H
+
+#define LEVEL_VERTEX_COUNT 84
+const S3L_Unit levelVertices[LEVEL_VERTEX_COUNT * 3] = {
+  -1990,  1285, -2716,        // 0
+  -1422,  1285, -2951,        // 3
+   5395,     4, -2716,        // 6
+  -5409,  4626,  2716,        // 9
+  -5409,     4,  2716,        // 12
+  -5409,  4626, -2716,        // 15
+  -5409,     0,  -598,        // 18
+  -1187,     4,  2716,        // 21
+  -1187,  1285, -3519,        // 24
+    964,  1285, -2716,        // 27
+  -5409,  2326,  2716,        // 30
+    964,  1285, -6473,        // 33
+  -5409,  4626,     0,        // 36
+   5395,     4,   513,        // 39
+    964,  1285,  -598,        // 42
+  -3302,  1285,  -598,        // 45
+  -3302,     4,  -598,        // 48
+   3179,  2102, -6473,        // 51
+   5395,  4626, -6473,        // 54
+  -1187,  4626, -6473,        // 57
+  -1187,  1285, -6473,        // 60
+    964,     4, -2716,        // 63
+  -5409,  1285, -2716,        // 66
+   3194,     4,  2716,        // 69
+  -5409,  1285,  -598,        // 72
+    964,     4,  -598,        // 75
+   2103,  4626, -6473,        // 78
+  -1187,  4626, -3519,        // 81
+  -1990,  4626, -2716,        // 84
+  -1422,  4626, -2951,        // 87
+  -3302,     4,  1353,        // 90
+  -5409,     4,  1353,        // 93
+   5395,     4, -4594,        // 96
+    964,     4, -4594,        // 99
+   3179,     4, -4594,        // 102
+   5395,  2102, -6473,        // 105
+   5395,  2102, -4594,        // 108
+   3179,  2102, -4594,        // 111
+    964,  1285, -4594,        // 114
+    196,     8,   681,        // 117
+    196,  4626,   681,        // 120
+    558,     8,   531,        // 123
+    558,  4626,   531,        // 126
+    708,     8,   169,        // 129
+    708,  4626,   169,        // 132
+    558,     8,  -192,        // 135
+    558,  4626,  -192,        // 138
+    196,     8,  -342,        // 141
+    196,  4626,  -342,        // 144
+   -165,     8,  -192,        // 147
+   -165,  4626,  -192,        // 150
+   -315,     8,   169,        // 153
+   -315,  4626,   169,        // 156
+   -165,     8,   531,        // 159
+   -165,  4626,   531,        // 162
+   5395,  4626,   513,        // 165
+   3194,  4626,  2716,        // 168
+   2216,   869, -2759,        // 171
+   2216,  1002, -3306,        // 174
+   2603,   869, -2919,        // 177
+   2887,   507, -2636,        // 180
+   2990,    13, -2532,        // 183
+   2764,   869, -3306,        // 186
+   3164,   507, -3306,        // 189
+   3311,    13, -3306,        // 192
+   2603,   869, -3693,        // 195
+   2887,   507, -3976,        // 198
+   2990,    13, -4080,        // 201
+   2216,   869, -3853,        // 204
+   2216,   507, -4254,        // 207
+   2216,    13, -4400,        // 210
+   1830,   869, -3693,        // 213
+   1546,   507, -3976,        // 216
+   1443,    13, -4080,        // 219
+   1669,   869, -3306,        // 222
+   1269,   507, -3306,        // 225
+   1122,    13, -3306,        // 228
+   1830,   869, -2919,        // 231
+   1546,   507, -2636,        // 234
+   1443,    13, -2532,        // 237
+   2216,   507, -2358,        // 240
+   2216,    13, -2212,        // 243
+  -1187,  4626,  2716,        // 246
+   5395,  4626, -2716         // 249
+}; // levelVertices
+
+#define LEVEL_TRIANGLE_COUNT 146
+const S3L_Index levelTriangleIndices[LEVEL_TRIANGLE_COUNT * 3] = {
+     25,     9,    21,        // 0
+     10,    12,    24,        // 3
+      1,    14,     0,        // 6
+     16,    14,    25,        // 9
+      8,     9,     1,        // 12
+     24,    30,    31,        // 15
+     22,    15,    24,        // 18
+     13,    23,    25,        // 21
+     15,    16,    30,        // 24
+     24,     5,    22,        // 27
+     27,    20,     8,        // 30
+     11,    38,    20,        // 33
+     14,     1,     9,        // 36
+     37,    35,    36,        // 39
+     14,    15,     0,        // 42
+     29,     0,    28,        // 45
+     27,     1,    29,        // 48
+     22,    28,     0,        // 51
+      4,    30,     7,        // 54
+     24,    31,    10,        // 57
+     10,    31,     4,        // 60
+     17,    38,    11,        // 63
+     20,    38,     8,        // 66
+      9,     8,    38,        // 69
+     38,    34,    33,        // 72
+      9,    38,    21,        // 75
+     38,    33,    21,        // 78
+     38,    37,    34,        // 81
+     36,    34,    37,        // 84
+     16,    47,    49,        // 87
+     18,    17,    26,        // 90
+     11,    26,    17,        // 93
+      7,    30,    23,        // 96
+     47,    25,    45,        // 99
+     40,    41,    39,        // 102
+     41,    44,    43,        // 105
+     44,    45,    43,        // 108
+     46,    47,    45,        // 111
+     48,    49,    47,        // 114
+     50,    51,    49,        // 117
+     52,    53,    51,        // 120
+     54,    39,    53,        // 123
+     25,    43,    45,        // 126
+     43,    23,    41,        // 129
+     41,    23,    39,        // 132
+     23,    30,    39,        // 135
+     39,    30,    53,        // 138
+     53,    30,    51,        // 141
+     49,    51,    16,        // 144
+     30,    16,    51,        // 147
+     13,    56,    23,        // 150
+     80,    61,    81,        // 153
+     57,    58,    59,        // 156
+     80,    59,    60,        // 159
+     60,    64,    61,        // 162
+     59,    58,    62,        // 165
+     60,    62,    63,        // 168
+     63,    67,    64,        // 171
+     62,    58,    65,        // 174
+     62,    66,    63,        // 177
+     65,    58,    68,        // 180
+     65,    69,    66,        // 183
+     67,    69,    70,        // 186
+     68,    72,    69,        // 189
+     69,    73,    70,        // 192
+     68,    58,    71,        // 195
+     71,    75,    72,        // 198
+     72,    76,    73,        // 201
+     71,    58,    74,        // 204
+     74,    78,    75,        // 207
+     76,    78,    79,        // 210
+     74,    58,    77,        // 213
+     77,    80,    78,        // 216
+     79,    80,    81,        // 219
+     77,    58,    57,        // 222
+     70,    73,    67,        // 225
+     33,    34,    70,        // 228
+     73,    33,    70,        // 231
+     76,    33,    73,        // 234
+     33,    76,    21,        // 237
+     76,    79,    21,        // 240
+     21,    79,    25,        // 243
+     79,    81,    25,        // 246
+     25,    81,    13,        // 249
+     81,    61,    13,        // 252
+     13,    61,     2,        // 255
+     61,    64,     2,        // 258
+      2,    64,    32,        // 261
+     64,    67,    32,        // 264
+     34,    32,    67,        // 267
+     67,    70,    34,        // 270
+     82,    10,     4,        // 273
+      4,     7,    82,        // 276
+     10,    82,     3,        // 279
+      7,    23,    82,        // 282
+     23,    56,    82,        // 285
+     13,     2,    83,        // 288
+     55,    13,    83,        // 291
+     36,    83,     2,        // 294
+     32,    36,     2,        // 297
+     36,    18,    83,        // 300
+     35,    18,    36,        // 303
+     25,    14,     9,        // 306
+     10,     3,    12,        // 309
+     16,    15,    14,        // 312
+     24,    15,    30,        // 315
+     22,     0,    15,        // 318
+     24,    12,     5,        // 321
+     27,    19,    20,        // 324
+     37,    17,    35,        // 327
+     29,     1,     0,        // 330
+     27,     8,     1,        // 333
+     22,     5,    28,        // 336
+      4,    31,    30,        // 339
+     17,    37,    38,        // 342
+     36,    32,    34,        // 345
+     16,    25,    47,        // 348
+     18,    35,    17,        // 351
+     40,    42,    41,        // 354
+     41,    42,    44,        // 357
+     44,    46,    45,        // 360
+     46,    48,    47,        // 363
+     48,    50,    49,        // 366
+     50,    52,    51,        // 369
+     52,    54,    53,        // 372
+     54,    40,    39,        // 375
+     25,    23,    43,        // 378
+     13,    55,    56,        // 381
+     80,    60,    61,        // 384
+     80,    57,    59,        // 387
+     60,    63,    64,        // 390
+     60,    59,    62,        // 393
+     63,    66,    67,        // 396
+     62,    65,    66,        // 399
+     65,    68,    69,        // 402
+     67,    66,    69,        // 405
+     68,    71,    72,        // 408
+     69,    72,    73,        // 411
+     71,    74,    75,        // 414
+     72,    75,    76,        // 417
+     74,    77,    78,        // 420
+     76,    75,    78,        // 423
+     77,    57,    80,        // 426
+     79,    78,    80,        // 429
+     19,    11,    20,        // 432
+     19,    26,    11         // 435
+}; // levelTriangleIndices
+
+S3L_Model3D levelModel;
+
+void levelModelInit()
+{
+  S3L_model3DInit(
+    levelVertices,
+    LEVEL_VERTEX_COUNT,
+    levelTriangleIndices,
+    LEVEL_TRIANGLE_COUNT,
+    &levelModel);
+}
+
+#endif // guard

+ 72 - 23
programs/player.c

@@ -4,21 +4,53 @@
 #define S3L_PERSPECTIVE_CORRECTION 2
 
 #include "helper.h"
+#include "levelModel.h"
 
 #define ROOM_SIZE 10000
 #define CUBE_SIZE 800
 
 TPE_Unit elevatorHeight;
 
+TPE_Unit ramp[6] = { 1600,0, -500,1400, -700,0 };
+TPE_Unit ramp2[6] = { 2000,-5000, 1500,1700, -5000,-500 };
+
 TPE_Vec3 environmentDistance(TPE_Vec3 p, TPE_Unit maxD)
 {
-  TPE_ENV_START( TPE_envAABoxInside(p,TPE_vec3(0,ROOM_SIZE / 4,0),TPE_vec3(ROOM_SIZE,ROOM_SIZE / 2,ROOM_SIZE)),p )
-  TPE_ENV_NEXT( TPE_envAABox(p,TPE_vec3(4000,160,4000),TPE_vec3(1000,160,1000)),p )
-  TPE_ENV_NEXT( TPE_envAABox(p,TPE_vec3(4000,80,2500),TPE_vec3(1000,80,500)),p )
-  TPE_ENV_NEXT( TPE_envAABox(p,TPE_vec3(-1000,270,4500),TPE_vec3(4000,270,250)),p )
-  TPE_ENV_NEXT( TPE_envAABox(p,TPE_vec3(-4000,elevatorHeight,0),TPE_vec3(1000,elevatorHeight,1000)),p )
-  TPE_ENV_NEXT( TPE_envHalfPlane(p,TPE_vec3(0,0,-2000),TPE_vec3(0,255,255)),p )
-  TPE_ENV_NEXT( TPE_envInfiniteCylinder(p,TPE_vec3(2000,0,-1100),TPE_vec3(0,255,0),400),p )
+  TPE_ENV_START( TPE_envAABoxInside(p,TPE_vec3(0,2450,-2100),TPE_vec3(12600,5000,10800)),p )
+//  TPE_ENV_NEXT( TPE_envAABox(p,TPE_vec3(-10000,-10000,-10000),TPE_vec3(-1386,10000,-3160)),p )
+  TPE_ENV_NEXT( TPE_envAABox(p,TPE_vec3(-5693,0,-6580),TPE_vec3(4307,20000,3420)),p )
+  TPE_ENV_NEXT( TPE_envAABox(p,TPE_vec3(-10000,-1000,-10000),TPE_vec3(11085,2500,9295)),p )
+
+
+  TPE_ENV_NEXT ( TPE_envAATriPrism(p,TPE_vec3(-5400,0,0),ramp,3000,2), p)
+  
+
+TPE_ENV_NEXT ( 
+TPE_envAATriPrism(p,
+TPE_vec3(2076,651,-6780),ramp2,3000,0), p)
+
+  
+TPE_ENV_NEXT( TPE_envAABox(p,
+TPE_vec3(7000,0,-8500),
+TPE_vec3(3405,2400,3183)),p )
+
+
+TPE_ENV_NEXT( TPE_envSphere(p,TPE_vec3(2521,-100,-3799),
+1200),p )
+
+// 2521 650 -3799
+
+//  TPE_ENV_NEXT( TPE_envAABox(p,TPE_vec3(4000,80,2500),TPE_vec3(1000,80,500)),p )
+//  TPE_ENV_NEXT( TPE_envAABox(p,TPE_vec3(-1000,270,4500),TPE_vec3(4000,270,250)),p )
+// 5298 651 -4432
+
+TPE_ENV_NEXT( 
+TPE_envAABox(p,TPE_vec3(5300,elevatorHeight,-4400),
+TPE_vec3(1000,elevatorHeight,1000)),p )
+
+
+  TPE_ENV_NEXT( TPE_envHalfPlane(p,TPE_vec3(5051,0,1802),TPE_vec3(-255,0,-255)),p )
+  TPE_ENV_NEXT( TPE_envInfiniteCylinder(p,TPE_vec3(320,0,170),TPE_vec3(0,255,0),530),p )
   TPE_ENV_END
 }
 
@@ -37,6 +69,7 @@ void updateDirection(void) // updates player direction vector
 int main(void)
 {
   helper_init();
+  levelModelInit();
 
   updateDirection();
 
@@ -48,7 +81,7 @@ int main(void)
 
   playerBody = &(tpe_world.bodies[0]);
 
-  TPE_bodyMoveBy(&tpe_world.bodies[0],TPE_vec3(0,1000,0));
+  TPE_bodyMoveBy(&tpe_world.bodies[0],TPE_vec3(1000,1000,1500));
   TPE_bodyRotateByAxis(&tpe_world.bodies[0],TPE_vec3(0,0,TPE_FRACTIONS_PER_UNIT / 4));
   playerBody->elasticity = 0;
   playerBody->friction = 0;   
@@ -76,16 +109,31 @@ int main(void)
     if (jumpCountdown > 0)
       jumpCountdown--;
 
-    /* Check whether on ground. This can be done in several ways, e.g. by
-    checking recent collisions. We'll do it by casting a downwards ray: */
 
-    onGround = (playerBody->flags & TPE_BODY_FLAG_DEACTIVATED) ||
-      TPE_DISTANCE( playerBody->joints[0].position,
+
+TPE_Vec3 groundPoint = environmentDistance(playerBody->joints[0].position,groundDist);
+
+onGround = (playerBody->flags & TPE_BODY_FLAG_DEACTIVATED) ||
+ (TPE_DISTANCE(playerBody->joints[0].position,groundPoint)
+ <= groundDist && groundPoint.y < playerBody->joints[0].position.y - groundDist / 2 
+  );
+
+if (!onGround)
+{
+  /* it's possible that the closest point is e.g. was a perpend wall so also
+     additionally check directly below */
+
+  onGround = TPE_DISTANCE( playerBody->joints[0].position,
       TPE_castEnvironmentRay(playerBody->joints[0].position,
       TPE_vec3(0,-1 * TPE_FRACTIONS_PER_UNIT,0),tpe_world.environmentFunction, 
       128,512,512)) <= groundDist;
+}
+
 
-    elevatorHeight = TPE_sin(helper_frame * 4) + TPE_FRACTIONS_PER_UNIT;
+
+    elevatorHeight =
+(1250 * (TPE_sin(helper_frame * 4) + TPE_FRACTIONS_PER_UNIT)) /
+(2 * TPE_FRACTIONS_PER_UNIT);
 
     s3l_scene.camera.transform.translation.x = playerBody->joints[0].position.x;
     s3l_scene.camera.transform.translation.z = playerBody->joints[0].position.z;
@@ -151,21 +199,22 @@ int main(void)
       playerRotation += 8;
 
     if (helper_frame % 64 == 0)
+    {
       helper_printCPU();
+      helper_printCamera();
+    }
 
     // draw the 3D environment
 
-    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));
-    helper_draw3DBox(TPE_vec3(-4000,elevatorHeight,0),TPE_vec3(2000,2 * elevatorHeight,2000),TPE_vec3(0,0,0));
-    helper_draw3DCylinder(TPE_vec3(2000,5000,-1100),TPE_vec3(400,10000,400),TPE_vec3(0,0,0));
+    helper_set3DColor(200,200,200);
+
+    helper_drawModel(&levelModel,TPE_vec3(0,0,0),TPE_vec3(600,600,600), 
+      TPE_vec3(0,0,0));
+
+helper_draw3DBox(
+TPE_vec3(5300,elevatorHeight,-4400),
+TPE_vec3(2000,2 * elevatorHeight,2000),TPE_vec3(0,0,0));
 
-    helper_draw3DPlane(TPE_vec3(0,1500,-3500),TPE_vec3(10000,512,4000),TPE_vec3(-64,0,0));
-    
     helper_set3DColor(200,50,0);
 
     helper_draw3DBox(TPE_bodyGetCenterOfMass(&tpe_world.bodies[2]),

+ 19 - 0
programs/test.c

@@ -55,6 +55,21 @@ TPE_Vec3 envSimple(TPE_Vec3 p, TPE_Unit maxD)
   TPE_ENV_END
 }
 
+TPE_Unit heightMap(int32_t x, int32_t y)
+{
+  x *= 16;
+  y *= 16;
+
+  return 
+    TPE_sin(x + TPE_cos(y * 2)) * TPE_sin(y * 2 + TPE_cos(x * 4)) /
+     (TPE_FRACTIONS_PER_UNIT / 2);
+}
+
+TPE_Vec3 envFuncHeightmap(TPE_Vec3 p, TPE_Unit maxD)
+{
+  return TPE_envHeightmap(p,TPE_vec3(10,20,30),500,heightMap,maxD);
+}
+
 int main(void)
 {
   puts("== testing tinyphysicsengine ==");
@@ -147,6 +162,10 @@ int main(void)
 
     ass(TPE_testClosestPointFunction(envFunc2,TPE_vec3(-2000,-1000,-5000),
       TPE_vec3(5000,6000,7000),32,30,0),"env function");
+    
+    ass(TPE_testClosestPointFunction(envFuncHeightmap,
+      TPE_vec3(-2000,-1000,-5000),TPE_vec3(4000,6000,7000),6,50,0),
+      "env function (heightmap)");
 
     ass(!TPE_testClosestPointFunction(envFuncBad,TPE_vec3(-1000,-1000,-1000),
       TPE_vec3(2000,3000,100),32,40,0),"env function bad");

+ 0 - 2
tinyphysicsengine.h

@@ -75,8 +75,6 @@ typedef int16_t TPE_UnitReduced;        ///< Like TPE_Unit but saving space
   #define TPE_LENGTH TPE_vec3LenApprox
 #endif
 
-// TODO: faster and more accurate distance approx function based on regions/LUT
-
 #ifndef TPE_LOG
   #define TPE_LOG(s) ;
 #endif