Miloslav Ciz 3 anni fa
parent
commit
79e35e2bc7
7 ha cambiato i file con 50 aggiunte e 29 eliminazioni
  1. 1 1
      programs/2d.c
  2. 1 1
      programs/car.c
  3. 1 1
      programs/make.sh
  4. 3 3
      programs/player.c
  5. 2 0
      programs/test.c
  6. 8 8
      programs/testGeneral.c
  7. 34 15
      tinyphysicsengine.h

+ 1 - 1
programs/2d.c

@@ -66,7 +66,7 @@ else
  
 tpe_world.bodies[i].elasticity = 100;
 
-    TPE_bodyMove(&tpe_world.bodies[i],TPE_vec3(-1000 + i * 800,0,0));
+    TPE_bodyMoveBy(&tpe_world.bodies[i],TPE_vec3(-1000 + i * 800,0,0));
   }
 
 

+ 1 - 1
programs/car.c

@@ -120,7 +120,7 @@ tpe_world.bodies[0].connectionCount,
 
 wheelSize = TPE_JOINT_SIZE(tpe_world.bodies[0].joints[0]) + 30;
   
-TPE_bodyMove(carBody,TPE_vec3(3000,1000,0));
+TPE_bodyMoveBy(carBody,TPE_vec3(3000,1000,0));
 
 tpe_world.bodies[0].elasticity = 64; 
 tpe_world.bodies[0].friction = 64; 

+ 1 - 1
programs/make.sh

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

+ 3 - 3
programs/player.c

@@ -48,7 +48,7 @@ int main(void)
 
   playerBody = &(tpe_world.bodies[0]);
 
-  TPE_bodyMove(&tpe_world.bodies[0],TPE_vec3(0,1000,0));
+  TPE_bodyMoveBy(&tpe_world.bodies[0],TPE_vec3(0,1000,0));
   TPE_bodyRotateByAxis(&tpe_world.bodies[0],TPE_vec3(0,0,TPE_FRACTIONS_PER_UNIT / 4));
   playerBody->elasticity = 0;
   playerBody->friction = 0;   
@@ -56,14 +56,14 @@ int main(void)
   groundDist = TPE_JOINT_SIZE(playerBody->joints[0]) + 30;
 
   helper_addBall(1000,100);
-  TPE_bodyMove(&tpe_world.bodies[1],TPE_vec3(-1000,1000,0));
+  TPE_bodyMoveBy(&tpe_world.bodies[1],TPE_vec3(-1000,1000,0));
   tpe_world.bodies[1].elasticity = 400;
   tpe_world.bodies[1].friction = 100;
 
   ballPreviousPos = tpe_world.bodies[1].joints[0].position;
 
   helper_addCenterRect(600,600,400,50);
-  TPE_bodyMove(&tpe_world.bodies[2],TPE_vec3(-3000,1000,2000));
+  TPE_bodyMoveBy(&tpe_world.bodies[2],TPE_vec3(-3000,1000,2000));
   tpe_world.bodies[2].elasticity = 100;
   tpe_world.bodies[2].friction = 50;
  

+ 2 - 0
programs/test.c

@@ -10,6 +10,8 @@ int main(void)
   puts("== testing tinyphysicsengine ==");
 
   {
+    ass(TPE_vec2Angle(-100,0) == TPE_FRACTIONS_PER_UNIT / 2,"vec2 angle")
+
     TPE_Unit l;
 
     l = TPE_LENGTH(TPE_vec3Normalized(TPE_vec3(100,0,0)));

+ 8 - 8
programs/testGeneral.c

@@ -39,7 +39,7 @@ int main(void)
 
 #define addBody(x,y,z,f) \
   helper_addBox(700,700,700,300,500); \
-  TPE_bodyMove(&helper_lastBody,TPE_vec3(x,y,z)); \
+  TPE_bodyMoveBy(&helper_lastBody,TPE_vec3(x,y,z)); \
   helper_lastBody.elasticity = 255; \
   helper_lastBody.friction = f;
 
@@ -57,7 +57,7 @@ int main(void)
 
 #define addBody(x,y,z,e) \
   helper_addRect(700,700,300,500); \
-  TPE_bodyMove(&helper_lastBody,TPE_vec3(x,y,z)); \
+  TPE_bodyMoveBy(&helper_lastBody,TPE_vec3(x,y,z)); \
   helper_lastBody.elasticity = e;
 
   // falling bodies
@@ -67,38 +67,38 @@ int main(void)
   addBody(10600,5000,0,0)
 
   helper_addBox(5000,5000,5000,2000,2000); // big box
-  TPE_bodyMove(&helper_lastBody,TPE_vec3(-20000,10000,8000));
+  TPE_bodyMoveBy(&helper_lastBody,TPE_vec3(-20000,10000,8000));
   TPE_bodySpin(&helper_lastBody,TPE_vec3(100,200,-20));
 
   // two colliding spheres:
 
   helper_addBall(800,2000);
-  TPE_bodyMove(&helper_lastBody,TPE_vec3(200,4000,-4800));
+  TPE_bodyMoveBy(&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));
+  TPE_bodyMoveBy(&helper_lastBody,TPE_vec3(3200,3800,-4800));
   helper_lastBody.elasticity = 255;
   helper_lastBody.friction = 255;
   helper_lastBody.joints[0].velocity[0] = -300;
 
   helper_addBall(1000,1000);
   controlledBody = &helper_lastBody;
-  TPE_bodyMove(&helper_lastBody,TPE_vec3(200,2000,-3000));
+  TPE_bodyMoveBy(&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));
+  TPE_bodyMoveBy(&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));
+  TPE_bodyMoveBy(&helper_lastBody,TPE_vec3(-8000,5000,-3000));
   helper_lastBody.elasticity = 255;
   helper_lastBody.friction = 255;
   helper_lastBody.flags |= TPE_BODY_FLAG_SOFT;

+ 34 - 15
tinyphysicsengine.h

@@ -254,10 +254,16 @@ TPE_Vec3 TPE_bodyGetRotation(const TPE_Body *body, uint16_t joint1,
 
 void TPE_vec3Normalize(TPE_Vec3 *v);
 
+/** Rotates a 3D point by given Euler angle rotation (see rotation
+  conventions). */
 TPE_Vec3 TPE_pointRotate(TPE_Vec3 point, TPE_Vec3 rotation);
 
+/** Returns an inverse rotation to given rotation, in Euler angles (see rotation
+  conventions). */
 TPE_Vec3 TPE_rotationInverse(TPE_Vec3 rotation);
 
+/** Returns a connection tension, i.e. a signed percentage difference against
+  desired length (TPE_FRACTIONS_PER_UNIT means 100%). */
 static inline TPE_Unit TPE_connectionTension(TPE_Unit length,
   TPE_Unit desiredLength);
 
@@ -265,12 +271,14 @@ static inline TPE_Unit TPE_connectionTension(TPE_Unit length,
   rotation conventions). Returns a rotation in Eurler angles. */
 TPE_Vec3 TPE_rotationRotateByAxis(TPE_Vec3 rotation, TPE_Vec3 rotationByAxis);
 
-/** Computes the formula of a 1 dimensional collision of rigid bodies. */
+/** Computes the formula of a 1D collision of rigid bodies. */
 void TPE_getVelocitiesAfterCollision(TPE_Unit *v1, TPE_Unit *v2, TPE_Unit m1,
   TPE_Unit m2, TPE_Unit elasticity);
 
 TPE_Unit TPE_sqrt(TPE_Unit value);
 
+/** Returns an angle in TPE_Units (see angle conventions) of a 2D vector with
+  the X axis, CCW. */
 TPE_Unit TPE_vec2Angle(TPE_Unit x, TPE_Unit y);
 
 TPE_Vec3 TPE_vec3(TPE_Unit x, TPE_Unit y, TPE_Unit z);
@@ -280,7 +288,7 @@ TPE_Vec3 TPE_vec3Cross(TPE_Vec3 v1, TPE_Vec3 v2);
 TPE_Vec3 TPE_vec3Project(TPE_Vec3 v, TPE_Vec3 base);
 TPE_Vec3 TPE_vec3ProjectNormalized(TPE_Vec3 v, TPE_Vec3 baseNormalized);
 TPE_Vec3 TPE_vec3Times(TPE_Vec3 v, TPE_Unit units);
-TPE_Vec3 TPE_vec3TimesNonNormalized(TPE_Vec3 v, TPE_Unit q);
+TPE_Vec3 TPE_vec3TimesPlain(TPE_Vec3 v, TPE_Unit q);
 TPE_Vec3 TPE_vec3Normalized(TPE_Vec3 v);
 
 TPE_Unit TPE_vec3Dot(TPE_Vec3 v1, TPE_Vec3 v2);
@@ -477,20 +485,23 @@ void TPE_bodyReshape(TPE_Body *body, TPE_ClosestPointFunction
 
 void TPE_bodyCancelOutVelocities(TPE_Body *body, uint8_t strong);
 
-/** Move a body by certain offset. */
-void TPE_bodyMove(TPE_Body *body, TPE_Vec3 offset);
+/** Moves a body by certain offset. */
+void TPE_bodyMoveBy(TPE_Body *body, TPE_Vec3 offset);
 
-/** Zero velocities of all soft body joints. */
+/** Moves a body (its center of mass) to given position). */
+void TPE_bodyMoveTo(TPE_Body *body, TPE_Vec3 position);
+
+/** Zeros velocities of all soft body joints. */
 void TPE_bodyStop(TPE_Body *body);
 
 void TPE_bodyActivate(TPE_Body *body);
 
-/** Add velocity to a soft body. */
+/** Adds velocity to a soft body. */
 void TPE_bodyAccelerate(TPE_Body *body, TPE_Vec3 velocity);
 
 void TPE_bodyApplyGravity(TPE_Body *body, TPE_Unit downwardsAccel);
 
-/** Add angular velocity to a soft body. The rotation vector specifies the axis
+/** Adds angular velocity to a soft body. The rotation vector specifies the axis
   of rotation by its direction and angular velocity by its magnitude (magnitude
   of TPE_FRACTIONS_PER_UNIT will add linear velocity of TPE_FRACTIONS_PER_UNIT
   per tick to a point in the distance of TPE_FRACTIONS_PER_UNIT from the
@@ -499,14 +510,14 @@ void TPE_bodySpin(TPE_Body *body, TPE_Vec3 rotation);
 
 void TPE_bodySpinWithCenter(TPE_Body *body, TPE_Vec3 rotation, TPE_Vec3 center);
 
-/** Instantly rotate soft body about an axis (see library conventions for
+/** Instantly rotates a body about an axis (see library conventions for
   the rotation format). */
 void TPE_bodyRotateByAxis(TPE_Body *body, TPE_Vec3 rotation);
 
-/** Compute the center of mass of a soft body. This averages the position of
-  all joints; note that if you need, you may estimate the center of the body
-  faster, e.g. by taking a position of a single "center joint", or averaging
-  just 2 extreme points. */ 
+/** Computes the center of mass of a body. This averages the position of all
+  joints; note that if you need, you may estimate the center of the body faster,
+  e.g. by taking a position of a single "center joint", or averaging just 2
+  extreme points. */ 
 TPE_Vec3 TPE_bodyGetCenterOfMass(const TPE_Body *body);
 
 /** Compute sine, TPE_FRACTIONS_PER_UNIT as argument corresponds to 2 * PI
@@ -882,7 +893,7 @@ void TPE_worldStep(TPE_World *world)
 
       if (collided &&
         TPE_bodyEnvironmentCollide(body,world->environmentFunction))
-        TPE_bodyMove(body,TPE_vec3Minus(origPos,body->joints[0].position));
+        TPE_bodyMoveBy(body,TPE_vec3Minus(origPos,body->joints[0].position));
     }
     else // normal, rotating bodies
     {
@@ -1332,7 +1343,7 @@ TPE_Vec3 TPE_vec3Project(TPE_Vec3 v, TPE_Vec3 base)
   return TPE_vec3ProjectNormalized(v,base);
 }
 
-void TPE_bodyMove(TPE_Body *body, TPE_Vec3 offset)
+void TPE_bodyMoveBy(TPE_Body *body, TPE_Vec3 offset)
 {
   for (uint16_t i = 0; i < body->jointCount; ++i)
     body->joints[i].position = TPE_vec3Plus(body->joints[i].position,
@@ -1581,7 +1592,7 @@ TPE_Vec3 TPE_vec3Times(TPE_Vec3 v, TPE_Unit units)
   return v;
 }
 
-TPE_Vec3 TPE_vec3TimesNonNormalized(TPE_Vec3 v, TPE_Unit q)
+TPE_Vec3 TPE_vec3TimesPlain(TPE_Vec3 v, TPE_Unit q)
 {
   v.x *= q;
   v.y *= q;
@@ -2849,4 +2860,12 @@ uint32_t TPE_worldHash(const TPE_World *world)
   return r;
 }
 
+void TPE_bodyMoveTo(TPE_Body *body, TPE_Vec3 position)
+{
+  position = TPE_vec3Minus(position,TPE_bodyGetCenterOfMass(body));
+
+  for (uint8_t i = 0; i < body->jointCount; ++i)
+    body->joints[i].position = TPE_vec3Plus(body->joints[i].position,position);
+}
+
 #endif // guard