Browse Source

Add general test

Miloslav Ciz 3 năm trước cách đây
mục cha
commit
eb1cb0950d
5 tập tin đã thay đổi với 169 bổ sung9 xóa
  1. 9 7
      TODO.txt
  2. 2 0
      programs/helper.h
  3. 1 1
      programs/make.sh
  4. 147 0
      programs/testGeneral.c
  5. 10 1
      tinyphysicsengine.h

+ 9 - 7
TODO.txt

@@ -1,11 +1,7 @@
 TODO:
 TODO:
-- body being pushed (e.g. by gravity) onto a sharp edge will likely not resist
-  (due to only linear forces in connections) and will very easily be split very
-  wide -- TRY AND FIX! Fix could be e.g. in a special function that checks
-  whether any connection is over some tension limit and if so inverts velocities
-  of its joints? OR MAYBE RATHER start applying proportional (as opposed to
-  current linear) acceleration due to tension after some tension limit.
 - demo: angry-birds-like game
 - demo: angry-birds-like game
+- elasticity doesn't really work in cases when there is an opposing joint to the
+  colliding joint, the speeds always cancel out -- leave as is or somehow fix?
 - function that tests a validity of an environemnt function, i.e. if it behaves
 - function that tests a validity of an environemnt function, i.e. if it behaves
   mathematically correct + test built in env functions with it
   mathematically correct + test built in env functions with it
 - demo: testing different frictions on skewed plane, also different elasticities
 - demo: testing different frictions on skewed plane, also different elasticities
@@ -14,7 +10,6 @@ TODO:
 - test env functions with a single distinct point near camera
 - test env functions with a single distinct point near camera
 - env function: heightmap
 - env function: heightmap
 - demo: soft net (water surface?)
 - demo: soft net (water surface?)
-- demo: car
 - bounding box/sphere test functions/macros for optimization of environment
 - bounding box/sphere test functions/macros for optimization of environment
   building, plus a demo that tests if it actually accelerates it
   building, plus a demo that tests if it actually accelerates it
 - test ray casting (e.g. the hit of an outside ray should always be outside)
 - test ray casting (e.g. the hit of an outside ray should always be outside)
@@ -26,6 +21,13 @@ TODO:
 - bug? bodies stuck inside each other resist falling down by gravity (stack.c)
 - bug? bodies stuck inside each other resist falling down by gravity (stack.c)
 
 
 DONE:
 DONE:
+- demo: car
+- body being pushed (e.g. by gravity) onto a sharp edge will likely not resist
+  (due to only linear forces in connections) and will very easily be split very
+  wide -- TRY AND FIX! Fix could be e.g. in a special function that checks
+  whether any connection is over some tension limit and if so inverts velocities
+  of its joints? OR MAYBE RATHER start applying proportional (as opposed to
+  current linear) acceleration due to tension after some tension limit.
 - try this: when reshaping body, also try to subtract velocities of joints that
 - try this: when reshaping body, also try to subtract velocities of joints that
   go against each other along a connection, i.e. velocities that should cancel
   go against each other along a connection, i.e. velocities that should cancel
   out... maybe this reduces shaking?
   out... maybe this reduces shaking?

+ 2 - 0
programs/helper.h

@@ -65,6 +65,8 @@
 
 
 #define PIXELS_SIZE (S3L_RESOLUTION_X * S3L_RESOLUTION_Y * 4)
 #define PIXELS_SIZE (S3L_RESOLUTION_X * S3L_RESOLUTION_Y * 4)
 
 
+#define helper_lastBody tpe_world.bodies[tpe_world.bodyCount - 1]
+
 S3L_Unit cubeVertices[] = { S3L_CUBE_VERTICES(TPE_FRACTIONS_PER_UNIT) };  
 S3L_Unit cubeVertices[] = { S3L_CUBE_VERTICES(TPE_FRACTIONS_PER_UNIT) };  
 S3L_Index cubeTriangles[] = { S3L_CUBE_TRIANGLES };
 S3L_Index cubeTriangles[] = { S3L_CUBE_TRIANGLES };
 S3L_Model3D cubeModel;
 S3L_Model3D cubeModel;

+ 1 - 1
programs/make.sh

@@ -1,5 +1,5 @@
 #!/bin/bash
 #!/bin/bash
 
 
-PROGRAM=car # change this to name of a program you want to compile :)
+PROGRAM=testGeneral # 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
 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

+ 147 - 0
programs/testGeneral.c

@@ -0,0 +1,147 @@
+#define CAMERA_STEP 200
+
+#include "helper.h"
+
+TPE_Unit
+  ramp1[6] = { 5000,0, 5000,5000, 0,0 },
+  ramp2[6] = { 5000,0, 5000,3000, 0,0 },
+  ramp3[6] = { 5000,0, 5000,1500, 0,0 },
+  impale[6] = { 1500,0, 0,6000, -1500,0 };
+
+TPE_Vec3 environmentDistance(TPE_Vec3 p, TPE_Unit maxD)
+{
+  TPE_ENV_START( TPE_envGround(p,0), p)
+  TPE_ENV_NEXT ( TPE_envAATriPrism(p,TPE_vec3(1000,0,0),ramp1,5000,2), p)
+  TPE_ENV_NEXT ( TPE_envAATriPrism(p,TPE_vec3(-4000,0,0),ramp2,3500,2), p)
+  TPE_ENV_NEXT ( TPE_envAATriPrism(p,TPE_vec3(-7500,0,0),ramp3,3500,2), p)
+  TPE_ENV_NEXT ( TPE_envAATriPrism(p,TPE_vec3(8000,0,-2400),impale,3500,2), p)
+  TPE_ENV_END
+}
+
+uint8_t simulating = 0;
+
+TPE_Body *controlledBody;
+
+int main(void)
+{
+  helper_init();
+
+  puts("press P to start");
+
+  helper_debugDrawOn = 1;
+
+  tpe_world.environmentFunction = environmentDistance;
+
+  s3l_scene.camera.transform.translation.z = -3000;
+  s3l_scene.camera.transform.translation.y = 2000;
+  s3l_scene.camera.transform.translation.x = 0;
+  s3l_scene.camera.transform.rotation.y = TPE_FRACTIONS_PER_UNIT / 16;
+
+#define addBody(x,y,z,f) \
+  helper_addBox(700,700,700,300,500); \
+  TPE_bodyMove(&helper_lastBody,TPE_vec3(x,y,z)); \
+  helper_lastBody.elasticity = 255; \
+  helper_lastBody.friction = f;
+
+  // cubes on ramps:
+
+  addBody(1800,5200,4000,0)
+  addBody(400,5200,4000,128)
+  addBody(-1100,5200,4000,511)
+  addBody(-2800,3600,4000,300)
+  addBody(-4300,3600,4000,10)
+  addBody(-6700,2000,4000,300)
+  addBody(-8400,2000,4000,10)
+
+  addBody(7500,6200,-2400,10) // impaled cube
+
+#define addBody(x,y,z,e) \
+  helper_addRect(700,700,300,500); \
+  TPE_bodyMove(&helper_lastBody,TPE_vec3(x,y,z)); \
+  helper_lastBody.elasticity = e;
+
+  // falling bodies
+
+  addBody(7000,5000,0,511)
+  addBody(8800,5000,0,255)
+  addBody(10600,5000,0,0)
+
+  // two colliding spheres:
+
+  helper_addBall(800,2000);
+  TPE_bodyMove(&helper_lastBody,TPE_vec3(200,4000,-4800));
+  helper_lastBody.elasticity = 255;
+  helper_lastBody.friction = 255;
+  helper_lastBody.joints[0].velocity[0] = 10;
+  
+  helper_addBall(800,200);
+  TPE_bodyMove(&helper_lastBody,TPE_vec3(3200,3800,-4800));
+  helper_lastBody.elasticity = 255;
+  helper_lastBody.friction = 255;
+  helper_lastBody.joints[0].velocity[0] = -300;
+
+  helper_addBall(500,200);
+  controlledBody = &helper_lastBody;
+  TPE_bodyMove(&helper_lastBody,TPE_vec3(200,2000,-3000));
+
+  // two colliding bodies:
+
+  helper_addCenterBox(700,700,700,300,500);
+  TPE_bodyMove(&helper_lastBody,TPE_vec3(-4000,5000,-3000));
+  helper_lastBody.elasticity = 255;
+  helper_lastBody.friction = 255;
+  //helper_lastBody.flags |= TPE_BODY_FLAG_SOFT;
+  TPE_bodyAccelerate(&helper_lastBody,TPE_vec3(-300,0,0));
+
+  helper_addCenterBox(700,700,700,300,500);
+  TPE_bodyMove(&helper_lastBody,TPE_vec3(-8000,5000,-3000));
+  helper_lastBody.elasticity = 255;
+  helper_lastBody.friction = 255;
+  helper_lastBody.flags |= TPE_BODY_FLAG_SOFT;
+  TPE_bodyAccelerate(&helper_lastBody,TPE_vec3(300,0,0));
+
+  while (helper_running)
+  {
+    helper_frameStart();
+
+    helper_cameraFreeMovement();
+
+if (simulating)
+{
+    TPE_worldStep(&tpe_world);
+
+for (int i = 0; i < 11; ++i)
+  TPE_bodyApplyGravity(&tpe_world.bodies[i],5);
+}
+
+    if (helper_debugDrawOn)
+      helper_debugDraw(1);
+
+if (sdl_keyboard[SDL_SCANCODE_P])
+  simulating = 1;
+
+
+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));
+
+
+    helper_frameEnd();
+  }
+
+  helper_end();
+
+  return 0;
+}

+ 10 - 1
tinyphysicsengine.h

@@ -138,7 +138,7 @@ typedef int16_t TPE_UnitReduced;        ///< Like TPE_Unit but saving space
 
 
 #ifndef TPE_COLLISION_RESOLUTION_ITERATIONS
 #ifndef TPE_COLLISION_RESOLUTION_ITERATIONS
 /** Maximum number of iterations to try to uncollide two colliding bodies. */
 /** Maximum number of iterations to try to uncollide two colliding bodies. */
-  #define TPE_COLLISION_RESOLUTION_ITERATIONS 3
+  #define TPE_COLLISION_RESOLUTION_ITERATIONS 16
 #endif
 #endif
 
 
 #ifndef TPE_COLLISION_RESOLUTION_MARGIN
 #ifndef TPE_COLLISION_RESOLUTION_MARGIN
@@ -417,6 +417,7 @@ TPE_Vec3 TPE_envBox(TPE_Vec3 point, TPE_Vec3 center, TPE_Vec3 maxCornerVec,
 TPE_Vec3 TPE_envSphere(TPE_Vec3 point, TPE_Vec3 center, TPE_Unit radius);
 TPE_Vec3 TPE_envSphere(TPE_Vec3 point, TPE_Vec3 center, TPE_Unit radius);
 TPE_Vec3 TPE_envSphereInside(TPE_Vec3 point, TPE_Vec3 center, TPE_Unit radius);
 TPE_Vec3 TPE_envSphereInside(TPE_Vec3 point, TPE_Vec3 center, TPE_Unit radius);
 TPE_Vec3 TPE_envHalfPlane(TPE_Vec3 point, TPE_Vec3 center, TPE_Vec3 normal);
 TPE_Vec3 TPE_envHalfPlane(TPE_Vec3 point, TPE_Vec3 center, TPE_Vec3 normal);
+TPE_Vec3 TPE_envGround(TPE_Vec3 point, TPE_Unit height);
 TPE_Vec3 TPE_envInfiniteCylinder(TPE_Vec3 point, TPE_Vec3 center, TPE_Vec3
 TPE_Vec3 TPE_envInfiniteCylinder(TPE_Vec3 point, TPE_Vec3 center, TPE_Vec3
   direction, TPE_Unit radius);
   direction, TPE_Unit radius);
 TPE_Vec3 TPE_envCylinder(TPE_Vec3 point, TPE_Vec3 center, TPE_Vec3 direction,
 TPE_Vec3 TPE_envCylinder(TPE_Vec3 point, TPE_Vec3 center, TPE_Vec3 direction,
@@ -2761,4 +2762,12 @@ TPE_Vec3 TPE_envAATriPrism(TPE_Vec3 point, TPE_Vec3 center,
   return TPE_vec3Plus(point,center);
   return TPE_vec3Plus(point,center);
 }
 }
 
 
+TPE_Vec3 TPE_envGround(TPE_Vec3 point, TPE_Unit height)
+{
+  if (point.y > height)
+    point.y = height;
+
+  return point;
+}
+
 #endif // guard
 #endif // guard