shithub: tinygl

Download patch

ref: e07fbb3904e8194fb620988ee47c32b443884c3a
parent: 02c64fd2b32c350c156e74a15656e4951165b872
author: MHS <gek@katherine>
date: Tue Mar 16 13:46:16 EDT 2021

Automatic commit.

--- a/include-demo/chadphys.h
+++ b/include-demo/chadphys.h
@@ -30,7 +30,7 @@
 	};
 	body->mass = 0;
 	body->bounciness = 0;
-	body->friction = 0.1;
+	body->friction = 0.99; //The amount of coplanar velocity preserved in collisions.
 	body->airfriction = 1.0;
 	body->a = (vec3){.d[0] = 0,.d[1] = 0,.d[2] = 0};
 	body->localt = identitymat4();
@@ -74,6 +74,7 @@
 #endif
 	}
 	if(penvec.d[3] <= 0) return; //No penetration detected, or invalid configuration.
+	vec3 penvecnormalized = scalev3(1.0/penvec.d[3], downv4(penvec)); //the penetration vector points into B...
 	//We now have the penetration vector. There is a penetration.
 	//determine how much each should be displaced by.
 	//The penvec points INTO A and is of length penvec.d[3]
@@ -88,20 +89,32 @@
 	}
 	if(a->mass > 0){
 		vec4 displacea = scalev4(-adisplacefactor, penvec);
+		vec3 a_relvel = subv3(a->v, comvel);
+		vec3 a_planarvel = subv3(a_relvel,
+								 scalev3(
+								 	dotv3(a_relvel, penvecnormalized),
+								 	penvecnormalized
+								 )
+								);
 		a->shape.c.d[0] += displacea.d[0];
 		a->shape.c.d[1] += displacea.d[1];
 		a->shape.c.d[2] += displacea.d[2];
-		//a->v = addv3(scalev3(1.0-a->friction, arelvel),bvel); //Apply friction!
-		a->v = addv3( scalev3(a->friction, comvel), scalev3(1.0-a->friction, a->v)  );
+		a->v = addv3( comvel, scalev3(a->friction, a_planarvel) ); //The center of mass velocity, plus a portion of coplanar velocity.
 		a->v = addv3(a->v, scalev3( a->bounciness, downv4(displacea) ) );
 	}
 	if(b->mass > 0){
 		vec4 displaceb = scalev4(bdisplacefactor, penvec);
+		vec3 b_relvel = subv3(b->v, comvel);
+		vec3 b_planarvel = subv3(b_relvel,  //brelvel - portion of brelvel in the direction of penvecnormalized
+									scalev3(
+										dotv3(b_relvel, penvecnormalized), //the component in that direction
+										penvecnormalized //that direction
+									)
+								);
 		b->shape.c.d[0] += displaceb.d[0];
 		b->shape.c.d[1] += displaceb.d[1];
 		b->shape.c.d[2] += displaceb.d[2];
-		//b->v = addv3(scalev3(1.0 - b->friction, brelvel),avel);
-		b->v = addv3( scalev3(b->friction, comvel), scalev3(1.0-b->friction, b->v)  );
+		b->v = addv3(comvel, scalev3(b->friction, b_planarvel) ); //The center of mass velocity, plus a portion of coplanar velocity.
 		b->v = addv3(b->v, scalev3( b->bounciness, downv4(displaceb) ) );
 	}
 }