Miloslav Ciz 3 лет назад
Родитель
Сommit
627e81afa0
2 измененных файлов с 48 добавлено и 30 удалено
  1. 29 18
      programs/2d.c
  2. 19 12
      tinyphysicsengine.h

+ 29 - 18
programs/2d.c

@@ -1,17 +1,19 @@
 #define TPE_RESHAPE_ITERATIONS 5
 
-#define DEBUG_DRAW_DIVIDE 16
+#define DEBUG_DRAW_DIVIDE 8
 
 #include "helper.h"
 
-#define ROOM_SIZE 7000
-#define CUBE_SIZE 800
+#define ROOM_W 5100
+#define ROOM_H ((RES_Y * ROOM_W) / RES_X)
+#define SQUARE_SIZE 500
 
 TPE_Vec3 environmentDistance(TPE_Vec3 p, TPE_Unit maxD)
 {
-  return TPE_envAABoxInside(p,TPE_vec3(0,ROOM_SIZE / 4,0),TPE_vec3(ROOM_SIZE,ROOM_SIZE / 2,ROOM_SIZE));
+  return TPE_envAABoxInside(p,TPE_vec3(0,0,0),TPE_vec3(ROOM_W,ROOM_H,ROOM_W));
 }
 
+
 int main(void)
 {
   helper_init();
@@ -20,22 +22,27 @@ helper_debugDrawOn = 1;
 
   tpe_world.environmentFunction = environmentDistance;
 
-  s3l_scene.camera.transform.translation.z -= ROOM_SIZE / 2;
+  s3l_scene.camera.transform.translation.z -= ROOM_W / 2;
 
 s3l_scene.camera.focalLength = 0; // set orthographic projection
 
-for (int i = 0; i < 4; ++i)
-{
-  helper_addCenterRectFull(500,500,100,100);
+  for (int i = 0; i < 4; ++i)
+  {
 
-  TPE_bodyRotateByAxis(&tpe_world.bodies[i],TPE_vec3(TPE_FRACTIONS_PER_UNIT / 4,0,0));
+if (i != 2)
+{
+    helper_addCenterRectFull(SQUARE_SIZE,SQUARE_SIZE,100,100);
+    TPE_bodyRotateByAxis(&tpe_world.bodies[i],TPE_vec3(TPE_FRACTIONS_PER_UNIT / 4,0,0));
+    tpe_world.bodies[i].joints[4].sizeDivided = 300 / TPE_JOINT_SIZE_MULTIPLIER;
+}
+else
+    helper_addBall(600,100);
 
-tpe_world.bodies[i].joints[4].sizeDivided = 300 / TPE_JOINT_SIZE_MULTIPLIER;
 
-tpe_world.bodies[i].friction = 400;
+    tpe_world.bodies[i].friction = 400;
 
-  TPE_bodyMove(&tpe_world.bodies[i],TPE_vec3(-1000 + i * 800,2000,0));
-}
+    TPE_bodyMove(&tpe_world.bodies[i],TPE_vec3(-1000 + i * 800,ROOM_H / 4,0));
+  }
 
 
     
@@ -43,13 +50,13 @@ tpe_world.bodies[i].friction = 400;
   {
     helper_frameStart();
 
-    //helper_cameraFreeMovement();
-
-      
     if (sdl_keyboard[SDL_SCANCODE_LEFT])
       TPE_bodyAccelerate(&tpe_world.bodies[0],TPE_vec3(-10,0,0));
     else if (sdl_keyboard[SDL_SCANCODE_RIGHT])
       TPE_bodyAccelerate(&tpe_world.bodies[0],TPE_vec3(10,0,0));
+    
+    if (sdl_keyboard[SDL_SCANCODE_UP])
+      TPE_bodyAccelerate(&tpe_world.bodies[0],TPE_vec3(0,10,0));
 
     TPE_worldStep(&tpe_world);
 
@@ -69,8 +76,12 @@ for (int i = 0; i < tpe_world.bodyCount; ++i)
   }
 }
 
-    if (helper_debugDrawOn)
-      helper_debugDraw();
+
+
+
+
+  //  if (helper_debugDrawOn)
+      helper_debugDraw(0);
 
     helper_frameEnd();
   }

+ 19 - 12
tinyphysicsengine.h

@@ -258,6 +258,8 @@ void TPE_getVelocitiesAfterCollision(TPE_Unit *v1, TPE_Unit *v2, TPE_Unit m1,
 
 TPE_Unit TPE_sqrt(TPE_Unit value);
 
+TPE_Unit TPE_vec2Angle(TPE_Unit x, TPE_Unit y);
+
 TPE_Vec3 TPE_vec3(TPE_Unit x, TPE_Unit y, TPE_Unit z);
 TPE_Vec3 TPE_vec3Minus(TPE_Vec3 v1, TPE_Vec3 v2);
 TPE_Vec3 TPE_vec3Plus(TPE_Vec3 v1, TPE_Vec3 v2);
@@ -1716,7 +1718,7 @@ void _TPE_vec2Rotate(TPE_Unit *x, TPE_Unit *y, TPE_Unit angle)
   *y = (s * tmp + c * *y) / TPE_FRACTIONS_PER_UNIT;
 }
 
-TPE_Unit _TPE_vec2Angle(TPE_Unit x, TPE_Unit y)
+TPE_Unit TPE_vec2Angle(TPE_Unit x, TPE_Unit y)
 {
   TPE_Unit r = 0;
 
@@ -1747,7 +1749,7 @@ TPE_Vec3 TPE_rotationFromVecs(TPE_Vec3 forward, TPE_Vec3 right)
 
   // get rotation around Y:
 
-  result.y = _TPE_vec2Angle(forward.z,-1 * forward.x);
+  result.y = TPE_vec2Angle(forward.z,-1 * forward.x);
 
   // now rotate back by this angle to align with x = 0 plane:
 
@@ -1757,11 +1759,11 @@ TPE_Vec3 TPE_rotationFromVecs(TPE_Vec3 forward, TPE_Vec3 right)
   // now do the same for the second axis:
 
   result.x = 
-    _TPE_vec2Angle(forward.z,forward.y);
+    TPE_vec2Angle(forward.z,forward.y);
 
   _TPE_vec2Rotate(&right.z,&right.y,-1 * result.x);
 
-  result.z = _TPE_vec2Angle(right.x,-1 * right.y);
+  result.z = TPE_vec2Angle(right.x,-1 * right.y);
 
   return result;
 }
@@ -1821,18 +1823,23 @@ void TPE_worldDebugDraw(TPE_World *world, TPE_DebugDrawFunction drawFunc,
 
     TPE_Vec3 testPoint;
 
-    TPE_Vec3 center = TPE_vec3(0,TPE_sin(camRot.x),TPE_cos(camRot.x));
+    TPE_Unit gridHalfSize = (envGridSize * envGridRes) / 2;
 
-    _TPE_vec2Rotate(&center.x,&center.z,camRot.y);
+    TPE_Vec3 center;
 
-    TPE_Unit gridHalfSize = (envGridSize * envGridRes) / 2;
+    if (envGridRes != 0)
+    {
+      center = TPE_vec3(0,TPE_sin(camRot.x),TPE_cos(camRot.x));
+
+      _TPE_vec2Rotate(&center.x,&center.z,camRot.y);
 
-    center = TPE_vec3Times(center,gridHalfSize);
-    center = TPE_vec3Plus(camPos,center);
+      center = TPE_vec3Times(center,gridHalfSize);
+      center = TPE_vec3Plus(camPos,center);
 
-    center.x = (center.x / envGridSize) * envGridSize;
-    center.y = (center.y / envGridSize) * envGridSize;
-    center.z = (center.z / envGridSize) * envGridSize;
+      center.x = (center.x / envGridSize) * envGridSize;
+      center.y = (center.y / envGridSize) * envGridSize;
+      center.z = (center.z / envGridSize) * envGridSize;
+    }
 
     testPoint.y = center.y - gridHalfSize;