diff options
author | taw27 <taw27@84d2e878-0bd5-11dd-ad15-13eda11d74c5> | 2008-06-08 13:20:57 +0000 |
---|---|---|
committer | taw27 <taw27@84d2e878-0bd5-11dd-ad15-13eda11d74c5> | 2008-06-08 13:20:57 +0000 |
commit | 901c64df46251c1610a7d765b41b8343b994540a (patch) | |
tree | 52e88fc6c5640acb3edbec64e47ca884cdf33117 /src | |
parent | cd2e21c6c7d84e8322f4cfe5e5c9f6972c0500d0 (diff) |
Move to the collision position before stopping
git-svn-id: svn://cook.msm.cam.ac.uk:745/thrust3d/thrust3d@85 84d2e878-0bd5-11dd-ad15-13eda11d74c5
Diffstat (limited to 'src')
-rw-r--r-- | src/physics.c | 105 |
1 files changed, 60 insertions, 45 deletions
diff --git a/src/physics.c b/src/physics.c index c9f89f1..d163b01 100644 --- a/src/physics.c +++ b/src/physics.c @@ -88,7 +88,7 @@ int physics_point_is_inside_hull(GLfloat cx, GLfloat cy, GLfloat cz, GLfloat *fv int physics_will_collide_face(GLfloat sx, GLfloat sy, GLfloat sz, GLfloat vx, GLfloat vy, GLfloat vz, GLfloat nx, GLfloat ny, GLfloat nz, GLfloat *fvert, int nfvert, Uint32 dt, - ModelInstance *obj) { + ModelInstance *obj, Uint32 *ttc) { GLfloat px, py, pz; GLfloat pdotn, sdotn, vdotn; @@ -132,6 +132,7 @@ int physics_will_collide_face(GLfloat sx, GLfloat sy, GLfloat sz, GLfloat vx, GL // printf("Collision happens at t + %f\n", t); // exit(0); + *ttc = t; return 1; } @@ -140,6 +141,63 @@ int physics_will_collide_face(GLfloat sx, GLfloat sy, GLfloat sz, GLfloat vx, GL } +static int physics_check_collide_all_faces(ModelInstance *obj, ModelInstance *other, Uint32 dt, int a, + GLfloat sx, GLfloat sy, GLfloat sz, GLfloat vx, GLfloat vy, GLfloat vz) { + + const GLfloat nx = other->model->primitives[a]->normals[0]; + const GLfloat ny = other->model->primitives[a]->normals[1]; + const GLfloat nz = other->model->primitives[a]->normals[2]; + + switch ( other->model->primitives[a]->type ) { + + case PRIMITIVE_QUADS : { + + /* Faces are quads */ + int f; + + for ( f=0; f<other->model->primitives[a]->num_vertices/4; f++ ) { + + GLfloat face[3*4]; + int q; + Uint32 ttc; + + for ( q=0; q<4; q++ ) { + face[3*q + 0] = other->x + other->model->primitives[a]->vertices[3*((4*f)+q) + 0]; + face[3*q + 1] = other->y + other->model->primitives[a]->vertices[3*((4*f)+q) + 1]; + face[3*q + 2] = other->z + other->model->primitives[a]->vertices[3*((4*f)+q) + 2]; + } + + if ( physics_will_collide_face(sx, sy, sz, vx, vy, vz, nx, ny, nz, face, 4, dt, other, &ttc) != 0 ) { + + printf("collides with %s/%p\n", other->model->name, other); + + if ( (nx == 0.0) && (ny == 0.0) && ( nz == 1.0) ) { + obj->x += obj->vx * ttc; + obj->y += obj->vy * ttc; + obj->z += obj->vz * ttc; + obj->vz = 0.0; + obj->landed = 1; + } else { + obj->vx = -obj->vx; + obj->vy = -obj->vy; + obj->vz = -obj->vz; + obj->yawspeed = 0.0; + } + + return 1; /* Don't waste time looking anywhere else */ + + } + } + break; + + } + + } + + return 0; + +} + /* Return non-zero if 'obj' will collide with 'other' with 'dt' milliseconds */ static int physics_check_collide(ModelInstance *obj, ModelInstance *other, Uint32 dt) { @@ -164,50 +222,7 @@ static int physics_check_collide(ModelInstance *obj, ModelInstance *other, Uint3 /* Consider all the primitives in 'other' */ for ( a=0; a<other->model->num_primitives; a++ ) { - - const GLfloat nx = other->model->primitives[a]->normals[0]; - const GLfloat ny = other->model->primitives[a]->normals[1]; - const GLfloat nz = other->model->primitives[a]->normals[2]; - - switch ( other->model->primitives[a]->type ) { - - case PRIMITIVE_QUADS : { - - /* Faces are quads */ - int f; - - for ( f=0; f<other->model->primitives[a]->num_vertices/4; f++ ) { - - GLfloat face[3*4]; - int q; - - for ( q=0; q<4; q++ ) { - face[3*q + 0] = other->x + other->model->primitives[a]->vertices[3*((4*f)+q) + 0]; - face[3*q + 1] = other->y + other->model->primitives[a]->vertices[3*((4*f)+q) + 1]; - face[3*q + 2] = other->z + other->model->primitives[a]->vertices[3*((4*f)+q) + 2]; - } - - if ( physics_will_collide_face(sx, sy, sz, vx, vy, vz, nx, ny, nz, - face, 4, dt, other) != 0 ) { - printf("collides with %s/%p\n", other->model->name, other); - if ( (nx == 0.0) && (ny == 0.0) && ( nz == 1.0) ) { - obj->vz = 0.0; - obj->landed = 1; - } else { - obj->vx = -obj->vx; - obj->vy = -obj->vy; - obj->vz = -obj->vz; - obj->yawspeed = 0.0; - } - return 1; /* Don't waste time looking anywhere else */ - } - } - break; - - } - - } - + if ( physics_check_collide_all_faces(obj, other, dt, a, sx, sy, sz, vx, vy, vz) ) return 1; } } |