shithub: tinygl

Download patch

ref: e6ead58e38cac4293da8643822e6386b3ab61a03
parent: 55c02ff080dfe0abd113a2d930c39f2da6e9316b
author: MHS <gek@katherine>
date: Thu Apr 29 20:54:33 EDT 2021

Automatic commit.

--- a/include-demo/chade.h
+++ b/include-demo/chade.h
@@ -78,7 +78,7 @@
 }
 /*invoked every single time that an object is added or removed*/
 static inline void prepChadWorld(ChadWorld* world){
-GLuint i;
+unsigned long i;
 	if(world->world.bodies) free(world->world.bodies);
 	world->world.bodies = calloc(1,sizeof(phys_body*) * world->n_ents);
 	/*Bodies is an array of pointers.*/
@@ -98,7 +98,7 @@
 	ChadEntity* old_ents_phys = world->ents_phys;
 	if(world->n_ents <= index) return; /*Bad index*/
 	world->ents_phys = calloc(1,(--world->n_ents) * sizeof(ChadEntity));
-	for(unsigned int i = 0; i < world->n_ents+1; i++) /**/
+	for(unsigned long i = 0; i < world->n_ents+1; i++) /**/
 	{
 		
 		if(i < index)
@@ -113,7 +113,7 @@
 
 
 static inline void renderChadWorld(ChadWorld* world){
-	GLuint i; 
+	unsigned long i; 
 	/*The user will already have set up the viewport, the camera, and the lights.*/
 	for(i = 0; i < world->n_ents; i++){
 		glPushMatrix();
--- a/include-demo/chadphys.h
+++ b/include-demo/chadphys.h
@@ -16,7 +16,7 @@
 	vec3 g; //gravity
 	phys_body** bodies;
 	f_ ms; //max speed
-	int nbodies; //number of bodies
+	long nbodies; //number of bodies
 } phys_world;
 
 
@@ -42,11 +42,12 @@
 
 //Check for and, if necessary, resolve colliding bodies.
 static inline void resolveBodies(phys_body* a, phys_body* b){
+	vec4 penvec; vec3 penvecnormalized, comvel; f_ friction, bdisplacefactor, adisplacefactor;
 	if(a->mass > 0 || b->mass > 0){ //Perform a preliminary check. Do we even have to do anything?
 		/*We must do shit*/
 	} else {return;}
 	/*Optimized for branch prediction.*/
-	vec4 penvec = (vec4){
+	penvec = (vec4){
 		.d[0]=0,
 		.d[1]=0,
 		.d[2]=0,
@@ -74,14 +75,13 @@
 	}
 #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...
-	f_ friction = a->friction * b->friction;
+	penvecnormalized = scalev3(1.0/penvec.d[3], downv4(penvec)); //the penetration vector points into B...
+	friction = a->friction * b->friction;
 	//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]
-	f_ bdisplacefactor = a->mass / (a->mass + b->mass);
-	f_ adisplacefactor = b->mass / (a->mass + b->mass);
-	vec3 comvel;
+	bdisplacefactor = a->mass / (a->mass + b->mass);
+	adisplacefactor = b->mass / (a->mass + b->mass);
 	if(!(a->mass > 0)) {
 		adisplacefactor = 0; bdisplacefactor = 1;
 	}else if(!(b->mass > 0)) {
@@ -88,10 +88,11 @@
 		bdisplacefactor = 0; adisplacefactor = 1;
 	}
 	comvel = addv3( scalev3(bdisplacefactor, a->v), scalev3(adisplacefactor, b->v));
-	if(a->mass > 0){
-		vec4 displacea = scalev4(-adisplacefactor, penvec);
-		vec3 a_relvel = subv3(a->v, comvel);
-		vec3 a_planarvel = subv3(a_relvel,
+	if(a->mass > 0){ 
+		vec4 displacea; vec3 a_relvel, a_planarvel; 
+		displacea = scalev4(-adisplacefactor, penvec);
+		a_relvel = subv3(a->v, comvel);
+		a_planarvel = subv3(a_relvel,
 								 scalev3(
 								 	dotv3(a_relvel, penvecnormalized),
 								 	penvecnormalized
@@ -103,25 +104,26 @@
 		a->v = addv3( comvel, scalev3(1-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
+	if(b->mass > 0){ 
+		vec4 displaceb; vec3 b_relvel, b_planarvel;
+		displaceb = scalev4(bdisplacefactor, penvec);
+		b_relvel = subv3(b->v, comvel);
+		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
 									)
 								);
-#pragma omp simd
-		for(int i = 0; i < 3; i++)
-			b->shape.c.d[i] += displaceb.d[i];
+		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(comvel, scalev3(1-friction, b_planarvel) ); //The center of mass velocity, plus a portion of coplanar velocity.
 		b->v = addv3(b->v, scalev3( b->bounciness, downv4(displaceb) ) );
 	}
 }
 
-static inline void stepPhysWorld(phys_world* world, const int collisioniter){
-	for(int i = 0; i < world->nbodies; i++)
+static inline void stepPhysWorld(phys_world* world, const long collisioniter){
+	for(long i = 0; i < world->nbodies; i++)
 		if(world->bodies[i] && world->bodies[i]->mass > 0){
 			phys_body* body = world->bodies[i];
 			vec3 bodypos = addv3(downv4(body->shape.c),body->v);
@@ -135,11 +137,11 @@
 			}
 		}
 	//Resolve collisions (if any)
-	for(int iter = 0; iter < collisioniter; iter++)
-	for(int i = 0; i < (int)(world->nbodies-1); i++)
-	if(world->bodies[i])
-	for(int j = i+1; j < (int)world->nbodies; j++)
-	if(world->bodies[j])
-		resolveBodies(world->bodies[i], world->bodies[j]);
+	for(long iter = 0; iter < collisioniter; iter++)
+	for(long i = 0; i < (long)(world->nbodies-1); i++)
+		if(world->bodies[i])
+		for(long j = i+1; j < (long)world->nbodies; j++)
+		if(world->bodies[j])
+			resolveBodies(world->bodies[i], world->bodies[j]);
 }
 #endif