This commit is contained in:
ABelliqueux 2021-02-11 16:02:18 +01:00
commit 1a362b9b37
2 changed files with 190 additions and 145 deletions

View File

@ -411,8 +411,6 @@ int main() {
applyAcceleration(meshes[k]->body); applyAcceleration(meshes[k]->body);
//~ VECTOR col_lvl, col_sphere = {0}; //~ VECTOR col_lvl, col_sphere = {0};
// Get col with level ( modelgnd_body ) // Get col with level ( modelgnd_body )
@ -432,32 +430,66 @@ int main() {
if ( col_lvl.vx ) { meshes[k]->body->gForce.vx *= -1; } if ( col_lvl.vx ) { meshes[k]->body->gForce.vx *= -1; }
if ( col_lvl.vy ) { meshes[k]->body->gForce.vy *= -1; } if ( col_lvl.vy ) {
//~ meshes[k]->body->gForce.vy *= -1;
}
if ( col_lvl.vz ) { meshes[k]->body->gForce.vz *= -1; } if ( col_lvl.vz ) { meshes[k]->body->gForce.vz *= -1; }
// If col, reset velocity // If col, reset velocity
if ( col_lvl.vx || //~ if ( col_lvl.vx ||
col_lvl.vy || //~ col_lvl.vy ||
col_lvl.vz //~ col_lvl.vz
) { //~ ) {
//~ meshes[k]->body->velocity.vy = meshes[k]->body->velocity.vx = meshes[k]->body->velocity.vz = 0;
//~ meshes[k]->body->velocity.vy = meshes[k]->body->velocity.vz = 0; //~ meshes[k]->body->velocity.vy = meshes[k]->body->velocity.vz = 0;
//~ }
ResolveCollision( &modelobject_body, &modelSphere_body);
//~ FntPrint("Vel: %d\n", modelSphere_body.velocity.vx);
if (col_sphere.vx){
int w = (ONE / (( modelSphere_body.velocity.vx * ONE ) / ( (modelSphere_body.max.vx - modelSphere_body.min.vx) / 2 ))) ;
if (modelSphere_body.velocity.vx){
//~ int w = (ONE / (( modelSphere_body.velocity.vx * ONE ) / ( (modelSphere_body.max.vx - modelSphere_body.min.vx) / 2 ))) * modelSphere_body.velocity.vx ;
//~ FntPrint("W %d\n",w);
FntPrint("Vel %d\n",modelSphere_body.velocity.vx);
modelSphere_rot.vz += w;
//~ if ( col_sphere.vx ) {
//~ meshes[k]->body->gForce.vx *= -1;
//modelSphere_body.gForce.vx = -meshes[k]->body->gForce.vx/4; //~ ResolveCollision(&modelobject_body, &modelSphere_body);
//~ }
}
} }
if (!col_sphere.vx){
modelSphere_body.velocity.vx = 0;
}
//~ if (w && !modelSphere_body.velocity.vx)
//~ {
//~ FntPrint("W %d\n",w);
//~ w --;
//~ }
if ( col_sphere.vx ) { meshes[k]->body->gForce.vx *= -1; modelSphere_body.gForce.vx = -meshes[k]->body->gForce.vx/4; //~ ResolveCollision(&modelobject_body, &modelSphere_body);
} //~ if ( col_sphere.vz ) { meshes[k]->body->gForce.vz *= -1; }
if ( col_sphere.vz ) { meshes[k]->body->gForce.vz *= -1; }
//~ if ( col_sphere.vy ) { meshes[k]->body->gForce.vy *= -1; } //~ if ( col_sphere.vy ) { meshes[k]->body->gForce.vy *= -1; }
//~ if (modelSphere_body.gForce.vx){modelSphere_body.gForce.vx -= 5;} //~ if (modelSphere_body.gForce.vx){modelSphere_body.gForce.vx -= 5;}
meshes[k]->body->position.vx = meshes[k]->pos->vx; meshes[k]->pos->vx = meshes[k]->body->position.vx;
meshes[k]->body->position.vy = meshes[k]->pos->vy; //~ meshes[k]->pos->vy = meshes[k]->body->position.vy ;
meshes[k]->body->position.vz = meshes[k]->pos->vz; meshes[k]->pos->vz = meshes[k]->body->position.vz;
} }
meshes[k]->body->velocity.vy = meshes[k]->body->velocity.vx = meshes[k]->body->velocity.vz = 0;
} }
} }
} }
@ -963,11 +995,11 @@ void applyAcceleration(BODY * actor){
short dt = 1; short dt = 1;
VECTOR acceleration = {actor->gForce.vx / actor->mass, actor->gForce.vy / actor->mass, actor->gForce.vz / actor->mass}; VECTOR acceleration = {actor->invMass * actor->gForce.vx , actor->invMass * actor->gForce.vy, actor->invMass * actor->gForce.vz};
actor->velocity.vx += (acceleration.vx * dt); actor->velocity.vx += (acceleration.vx * dt) / 4096;
actor->velocity.vy += (acceleration.vy * dt); actor->velocity.vy += (acceleration.vy * dt) / 4096;
actor->velocity.vz += (acceleration.vz * dt); actor->velocity.vz += (acceleration.vz * dt) / 4096;
actor->position.vx += (actor->velocity.vx * dt); actor->position.vx += (actor->velocity.vx * dt);
actor->position.vy += (actor->velocity.vy * dt); actor->position.vy += (actor->velocity.vy * dt);
@ -979,13 +1011,18 @@ void applyAcceleration(BODY * actor){
void ResolveCollision( BODY * one, BODY * two ){ void ResolveCollision( BODY * one, BODY * two ){
// Calculate relative velocity // Calculate relative velocity
VECTOR rv = { subVector(two->velocity, one->velocity) }; VECTOR rv = { subVector( one->velocity, two->velocity) };
//~ FntPrint("rv: %d, %d, %d\n", rv.vx,rv.vy,rv.vz); //~ FntPrint("rv: %d, %d, %d\n", rv.vx,rv.vy,rv.vz);
// Collision normal // Collision normal
VECTOR normal = { subVector( two->position, one->position ) }; VECTOR normal = { subVector( two->position, one->position ) };
// Normalize collision normal
normal.vx = normal.vx > 32 ? 1 : normal.vx < -32 ? -1 : 0 ;
normal.vy = normal.vy > 256 ? 1 : normal.vy < -256 ? -1 : 0 ;
normal.vz = normal.vz > 32 ? 1 : normal.vz < -32 ? -1 : 0 ;
//~ FntPrint("norm: %d, %d, %d\n", normal.vx,normal.vy,normal.vz); //~ FntPrint("norm: %d, %d, %d\n", normal.vx,normal.vy,normal.vz);
// Calculate relative velocity in terms of the normal direction // Calculate relative velocity in terms of the normal direction
@ -1003,30 +1040,33 @@ void ResolveCollision( BODY * one, BODY * two ){
//~ FntPrint("e: %d\n", e); //~ FntPrint("e: %d\n", e);
//~ // Calculate impulse scalar //~ // Calculate impulse scalar
long j = -(1 + e) * velAlongNormal; long j = -(1 + e) * velAlongNormal * ONE;
long k = ONE / one->mass; j /= one->invMass + two->invMass;
long l = ONE / two->mass; //~ j /= ONE;
j /= k + l;
j /= ONE;
//~ FntPrint("j: %d\n", j); //~ FntPrint("j: %d\n", j);
// Apply impulse // Apply impulse
applyVector(&normal, j, j, j, *=); applyVector(&normal, j, j, j, *=);
//~ FntPrint("Cnormal %d %d %d\n",normal.vx,normal.vy,normal.vz);
VECTOR velOne = normal; VECTOR velOne = normal;
VECTOR velTwo = normal; VECTOR velTwo = normal;
FntPrint("vel1: %d, %d, %d\n", velOne.vx,velOne.vy,velOne.vz); applyVector(&velOne,one->invMass,one->invMass,one->invMass, *=);
FntPrint("vel2: %d, %d, %d\n", velTwo.vx,velTwo.vy,velTwo.vz); applyVector(&velTwo,two->invMass,two->invMass,two->invMass, *=);
applyVector(&velOne,k/ONE,k/ONE,k/ONE, *=); //~ FntPrint("V1 %d %d %d\n", velOne.vx/4096/4096,velOne.vy/4096/4096,velOne.vz/4096/4096);
applyVector(&velTwo,l/ONE,l/ONE,l/ONE, *=); //~ FntPrint("V2 %d %d %d\n", velTwo.vx/4096/4096,velTwo.vy/4096/4096,velTwo.vz/4096/4096);
applyVector(&one->velocity, velOne.vx, velOne.vy, velOne.vz, -=); applyVector(&one->velocity, velOne.vx/4096/4096, velOne.vy/4096/4096, velOne.vz/4096/4096, -=);
applyVector(&two->velocity, velTwo.vx, velTwo.vy, velTwo.vz, +=); applyVector(&two->velocity, velTwo.vx/4096/4096, velTwo.vy/4096/4096, velTwo.vz/4096/4096, +=);
}
//~ FntPrint("V1 %d %d %d\n", velOne.vx/4096/4096,velOne.vy/4096/4096,velOne.vz/4096/4096);
//~ FntPrint("V2 %d %d %d\n", velTwo.vx/4096/4096,velTwo.vy/4096/4096,velTwo.vz/4096/4096);
}
// A few notes on the following code : // A few notes on the following code :

233
coridor.c
View File

@ -1,10 +1,11 @@
typedef struct { typedef struct {
VECTOR gForce; VECTOR gForce;
VECTOR position; VECTOR position;
SVECTOR velocity; SVECTOR velocity;
int mass; int invMass;
VECTOR min; VECTOR min;
VECTOR max; VECTOR max;
int restitution;
} BODY; } BODY;
typedef struct { typedef struct {
@ -42,8 +43,8 @@ typedef struct {
} CAMPATH; } CAMPATH;
CAMPOS camStartPos = { CAMPOS camStartPos = {
{-62,176,-15}, {-177,90,121},
{537,459,0}, {78,459,0},
}; };
CAMPATH camPath = { CAMPATH camPath = {
@ -868,12 +869,13 @@ short modelCylindre_isPrism =0;
short modelCylindre_isAnim =1; short modelCylindre_isAnim =1;
long modelCylindre_p = 0; long modelCylindre_p = 0;
BODY modelCylindre_body = { BODY modelCylindre_body = {
{0, 981, 0, 0}, {0, 981, 0, 0},
-53,-108,18, 0, -53,-108,18, 0,
0,0,0, 0, 0,0,0, 0,
1000, ONE/1000,
13,-3,14, 0, 13,-3,14, 0,
90,62,91, 0, 90,62,91, 0,
4096,
}; };
TMESH modelCylindre = { TMESH modelCylindre = {
@ -2103,12 +2105,13 @@ short modelgnd_isPrism =0;
short modelgnd_isAnim =0; short modelgnd_isAnim =0;
long modelgnd_p = 0; long modelgnd_p = 0;
BODY modelgnd_body = { BODY modelgnd_body = {
{0, 981, 0, 0}, {0, 981, 0, 0},
0,0,0, 0, 0,0,0, 0,
0,0,0, 0, 0,0,0, 0,
1000, ONE/1000,
-580,-195,-98, 0, -580,-195,-98, 0,
200,0,682, 0, 200,0,682, 0,
4096,
}; };
TMESH modelgnd = { TMESH modelgnd = {
@ -2256,19 +2259,20 @@ int modelobject_index[] = {
}; };
MATRIX modelobject_matrix = {0}; MATRIX modelobject_matrix = {0};
VECTOR modelobject_pos = {-160,-141,58, 0}; VECTOR modelobject_pos = {-200,-179,57, 0};
SVECTOR modelobject_rot = {0,0,0}; SVECTOR modelobject_rot = {0,0,0};
short modelobject_isRigidBody =1; short modelobject_isRigidBody =1;
short modelobject_isPrism =0; short modelobject_isPrism =0;
short modelobject_isAnim =0; short modelobject_isAnim =0;
long modelobject_p = 0; long modelobject_p = 0;
BODY modelobject_body = { BODY modelobject_body = {
{0, 981, 100, 0}, {100, 981, 0, 0},
-160,-141,58, 0, -200,-179,57, 0,
0,0,0, 0, 0,0,0, 0,
50, ONE/64,
-20,1,-22, 0, -20,1,-22, 0,
19,53,18, 0, 19,53,18, 0,
2048
}; };
TMESH modelobject = { TMESH modelobject = {
@ -2301,102 +2305,102 @@ MESH meshobject = {
}; };
SVECTOR modelSphere_mesh[] = { SVECTOR modelSphere_mesh[] = {
{0,-50,42}, {0,-19,15},
{0,-30,58}, {0,-11,21},
{0,-6,65}, {0,-2,24},
{0,19,62}, {0,7,23},
{15,13,63}, {6,5,23},
{13,-50,40}, {5,-19,15},
{17,-26,58}, {6,-10,22},
{14,42,49}, {5,15,18},
{23,53,32}, {8,20,12},
{47,-6,46}, {17,-2,17},
{36,7,55}, {13,3,20},
{38,-33,42}, {14,-12,16},
{49,-21,38}, {18,-8,14},
{48,21,39}, {18,8,14},
{41,42,30}, {15,15,11},
{32,-51,26}, {12,-19,10},
{30,-58,6}, {11,-22,2},
{55,-30,17}, {20,-11,6},
{63,-7,18}, {23,-3,7},
{61,13,20}, {23,5,7},
{55,31,17}, {20,11,6},
{12,-63,14}, {4,-23,5},
{39,52,5}, {15,19,2},
{64,-15,-6}, {24,-5,-2},
{58,31,-5}, {21,11,-2},
{24,60,5}, {9,22,2},
{9,-64,-9}, {3,-24,-3},
{50,-43,-8}, {18,-16,-3},
{64,7,-13}, {24,2,-5},
{28,-58,-11}, {10,-21,-4},
{46,42,-21}, {17,16,-8},
{40,-42,-31}, {15,-15,-12},
{53,17,-35}, {20,6,-13},
{46,-26,-40}, {17,-10,-15},
{37,31,-45}, {14,11,-17},
{22,55,-28}, {8,21,-10},
{0,65,0}, {0,24,0},
{30,-6,-58}, {11,-2,-21},
{23,18,-59}, {8,7,-22},
{15,-58,-27}, {5,-21,-10},
{20,-43,-46}, {7,-16,-17},
{17,-30,-56}, {6,-11,-21},
{13,-7,-64}, {5,-3,-23},
{23,47,-40}, {8,17,-15},
{13,63,-13}, {5,23,-5},
{0,-50,-41}, {0,-19,-15},
{0,-31,-57}, {0,-12,-21},
{0,-6,-65}, {0,-2,-24},
{0,19,-62}, {0,7,-23},
{11,47,-46}, {4,17,-17},
{-17,-30,-56}, {-6,-11,-21},
{-13,-7,-64}, {-5,-3,-23},
{-17,36,-52}, {-6,13,-19},
{-20,-43,-46}, {-7,-16,-17},
{-30,-6,-58}, {-11,-2,-21},
{-23,18,-59}, {-8,7,-22},
{0,-65,11}, {0,-24,4},
{-15,-58,-27}, {-5,-21,-10},
{-40,-42,-31}, {-15,-15,-12},
{-37,31,-45}, {-14,11,-17},
{-23,47,-40}, {-8,17,-15},
{-46,-26,-40}, {-17,-10,-15},
{-54,-8,-37}, {-20,-3,-14},
{-53,17,-35}, {-20,6,-13},
{-46,42,-21}, {-17,16,-8},
{-22,55,-28}, {-8,21,-10},
{-9,-64,-9}, {-3,-24,-3},
{-64,7,-13}, {-24,2,-5},
{-28,-58,-11}, {-10,-21,-4},
{-64,-15,-6}, {-24,-5,-2},
{-58,31,-5}, {-21,11,-2},
{-50,-43,-8}, {-18,-16,-3},
{-61,13,20}, {-23,5,7},
{-13,63,-13}, {-5,23,-5},
{-55,-30,17}, {-20,-11,6},
{-63,-7,18}, {-23,-3,7},
{-55,31,17}, {-20,11,6},
{-39,52,5}, {-15,19,2},
{-30,-58,6}, {-11,-22,2},
{-24,60,5}, {-9,22,2},
{-49,-21,38}, {-18,-8,14},
{-48,21,39}, {-18,8,14},
{-41,42,30}, {-15,15,11},
{-32,-51,26}, {-12,-19,10},
{-38,-33,42}, {-14,-12,16},
{-47,-6,46}, {-17,-2,17},
{-29,29,51}, {-11,11,19},
{-36,7,55}, {-13,3,20},
{-10,63,16}, {-4,23,6},
{-12,-63,14}, {-4,-23,5},
{-13,-50,40}, {-5,-19,15},
{-17,-26,58}, {-6,-10,22},
{-15,13,63}, {-6,5,23},
{-14,42,49}, {-5,15,18},
{-23,53,32}, {-8,20,12},
{0,47,46} {0,17,17}
}; };
SVECTOR modelSphere_normal[] = { SVECTOR modelSphere_normal[] = {
@ -3824,19 +3828,20 @@ int modelSphere_index[] = {
}; };
MATRIX modelSphere_matrix = {0}; MATRIX modelSphere_matrix = {0};
VECTOR modelSphere_pos = {-24,-41,238, 0}; VECTOR modelSphere_pos = {-103,-22,54, 0};
SVECTOR modelSphere_rot = {0,0,0}; SVECTOR modelSphere_rot = {0,0,0};
short modelSphere_isRigidBody =1; short modelSphere_isRigidBody =1;
short modelSphere_isPrism =0; short modelSphere_isPrism =0;
short modelSphere_isAnim =0; short modelSphere_isAnim =0;
long modelSphere_p = 0; long modelSphere_p = 0;
BODY modelSphere_body = { BODY modelSphere_body = {
{0, 981, 0, 0}, {0, 981, 0, 0},
-24,-41,238, 0, -103,-22,54, 0,
0,0,0, 0, 0,0,0, 0,
1000, ONE/128,
-64,-65,-65, 0, -24,-24,-24, 0,
64,65,65, 0, 24,24,24, 0,
1024
}; };
TMESH modelSphere = { TMESH modelSphere = {