aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authortaw27 <taw27@84d2e878-0bd5-11dd-ad15-13eda11d74c5>2008-06-11 23:43:19 +0000
committertaw27 <taw27@84d2e878-0bd5-11dd-ad15-13eda11d74c5>2008-06-11 23:43:19 +0000
commit355a35acb687510f8e82e1a1f65cd396073575b9 (patch)
treef7e181c0530f47fc27df5cccb0c05260c8b454df /src
parent0453db5eb3133b2e378587d26860b9ec8beadab5 (diff)
Break (while actually fixing) collision detection...
git-svn-id: svn://cook.msm.cam.ac.uk:745/thrust3d/thrust3d@90 84d2e878-0bd5-11dd-ad15-13eda11d74c5
Diffstat (limited to 'src')
-rw-r--r--src/physics.c47
1 files changed, 30 insertions, 17 deletions
diff --git a/src/physics.c b/src/physics.c
index ce0b9bc..cfbdf7e 100644
--- a/src/physics.c
+++ b/src/physics.c
@@ -140,7 +140,8 @@ int physics_will_collide_face(double sx, double sy, double sz, double vx, double
/* Check for collision with all faces in a primitive */
static int physics_check_collide_all_faces(ModelInstance *obj, ModelInstance *other, Uint32 dt, int a,
- double sx, double sy, double sz, double vx, double vy, double vz) {
+ double sx, double sy, double sz, double vx, double vy, double vz,
+ double *ttc_lowest, double *nxc, double *nyc, double *nzc) {
switch ( other->model->primitives[a]->type ) {
@@ -170,23 +171,14 @@ static int physics_check_collide_all_faces(ModelInstance *obj, ModelInstance *ot
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 in %5.2f ms\n", other->model->name, other, ttc);
-
- 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;
+ if ( ttc < *ttc_lowest ) {
+ printf("collides with %s/%p in %5.2f ms\n", other->model->name, other, ttc);
+ *ttc_lowest = ttc;
+ *nxc = nx;
+ *nyc = ny;
+ *nzc = nz;
}
- return 1; /* Don't waste time looking anywhere else */
-
}
}
break;
@@ -208,6 +200,11 @@ static int physics_check_collide(ModelInstance *obj, ModelInstance *other, Uint3
const double vy = obj->vy;
const double vz = obj->vz;
+ double ttc_lowest = 100000.0;
+ double nxc = 0.0;
+ double nyc = 0.0;
+ double nzc = 0.0;
+
/* Consider all the primitives in 'obj' */
for ( i=0; i<obj->model->num_primitives; i++ ) {
@@ -223,11 +220,27 @@ 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++ ) {
- if ( physics_check_collide_all_faces(obj, other, dt, a, sx, sy, sz, vx, vy, vz) ) return 1;
+ physics_check_collide_all_faces(obj, other, dt, a, sx, sy, sz, vx, vy, vz, &ttc_lowest, &nxc, &nyc, &nzc);
}
}
}
+
+ if ( ttc_lowest < dt ) return 0;
+
+ if ( (nxc == 0.0) && (nyc == 0.0) && (nzc == 1.0) ) {
+ obj->x += obj->vx * ttc_lowest;
+ obj->y += obj->vy * ttc_lowest;
+ obj->z += obj->vz * ttc_lowest;
+ 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 0;