void DoPhysicsSim (tObject *objP) { short ignoreObjList [MAX_IGNORE_OBJS], nIgnoreObjs; int iseg; int bRetry; int fate; vmsVector vFrame; //movement in this frame vmsVector vNewPos, ipos; //position after this frame int count=0; short nObject = OBJ_IDX (objP); short nWallHitSeg, nWallHitSide; fvi_info hi; fvi_query fq; vmsVector vSavePos; int nSaveSeg; fix xDrag; fix xSimTime, xOldSimTime, xTimeScale; vmsVector vStartPos; int bObjStopped=0; fix xMovedTime; //how long objected moved before hit something vmsVector vSaveP0, vSaveP1; tPhysicsInfo *pi; short nOrigSegment = objP->nSegment; int bBounced = 0; tSpeedBoostData sbd = gameData.objs.speedBoost [nObject]; int bDoSpeedBoost = sbd.bBoosted; // && (objP == gameData.objs.console); Assert (objP->nType != OBJ_NONE); Assert (objP->movementType == MT_PHYSICS); if (objP->nType == OBJ_PLAYER && Controls.headingTime) objP = objP; #ifdef _DEBUG if (bDontMoveAIObjects) if (objP->controlType == CT_AI) return; #endif pi = &objP->mType.physInfo; DoPhysicsSimRot (objP); #if 1 if (!(pi->velocity.x || pi->velocity.y || pi->velocity.z)) { UnstickObject (objP); if (objP == gameData.objs.console) gameData.objs.speedBoost [nObject].bBoosted = sbd.bBoosted = 0; if (!(pi->thrust.x || pi->thrust.y || pi->thrust.z)) return; } #endif nPhysSegs = 0; bSimpleFVI = (objP->nType != OBJ_PLAYER); xSimTime = gameData.time.xFrame; vStartPos = objP->pos; nIgnoreObjs = 0; Assert (objP->mType.physInfo.brakes==0); //brakes not used anymore? //if uses thrust, cannot have zero xDrag Assert (!(objP->mType.physInfo.flags&PF_USES_THRUST) || objP->mType.physInfo.drag!=0); //do thrust & xDrag if (xDrag = objP->mType.physInfo.drag) { int count; vmsVector accel, *vel = &objP->mType.physInfo.velocity; fix r, k, d, a; d = f1_0 - xDrag; count = xSimTime / FT; r = xSimTime % FT; k = FixDiv (r, FT); if (objP->mType.physInfo.flags & PF_USES_THRUST) { VmVecCopyScale (&accel, &objP->mType.physInfo.thrust, FixDiv (f1_0, objP->mType.physInfo.mass)); a = (accel.x || accel.y || accel.z); if (bDoSpeedBoost && !(a || gameStates.input.bControlsSkipFrame)) *vel = sbd.vVel; else { while (count--) { if (a) VmVecInc (vel, &accel); VmVecScale (vel, d); } //do linear scale on remaining bit of time VmVecScaleInc (vel, &accel, k); VmVecScale (vel, f1_0 - FixMul (k, xDrag)); if (bDoSpeedBoost) { if (vel->x < sbd.vMinVel.x) vel->x = sbd.vMinVel.x; else if (vel->x > sbd.vMaxVel.x) vel->x = sbd.vMaxVel.x; if (vel->y < sbd.vMinVel.y) vel->y = sbd.vMinVel.y; else if (vel->y > sbd.vMaxVel.y) vel->y = sbd.vMaxVel.y; if (vel->z < sbd.vMinVel.z) vel->z = sbd.vMinVel.z; else if (vel->z > sbd.vMaxVel.z) vel->z = sbd.vMaxVel.z; } } } else { fix xTotalDrag = f1_0; while (count--) xTotalDrag = FixMul (xTotalDrag, d); //do linear scale on remaining bit of time xTotalDrag = FixMul (xTotalDrag, f1_0-FixMul (k, xDrag)); VmVecScale (&objP->mType.physInfo.velocity, xTotalDrag); } } if (extraGameInfo [IsMultiGame].bFluidPhysics) { if (gameData.segs.segment2s [objP->nSegment].special == SEGMENT_IS_WATER) xTimeScale = 75; else if (gameData.segs.segment2s [objP->nSegment].special == SEGMENT_IS_LAVA) xTimeScale = 66; else xTimeScale = 100; } else xTimeScale = 100; do { bRetry = 0; //Move the tObject VmVecCopyScale ( &vFrame, &objP->mType.physInfo.velocity, FixMulDiv (xSimTime, xTimeScale, 100)); if ((vFrame.x == 0) && (vFrame.y == 0) && (vFrame.z == 0)) break; retryMove: count++; // If retry count is getting large, then we are trying to do something stupid. if (count > 3) { if (objP->nType == OBJ_PLAYER) { if (count > 8) { if (sbd.bBoosted) sbd.bBoosted = 0; break; } } else break; } VmVecAdd (&vNewPos, &objP->pos, &vFrame); ignoreObjList [nIgnoreObjs] = -1; fq.p0 = &objP->pos; fq.startSeg = objP->nSegment; fq.p1 = &vNewPos; fq.rad = objP->size; fq.thisObjNum = nObject; fq.ignoreObjList = ignoreObjList; fq.flags = FQ_CHECK_OBJS; if (objP->nType == OBJ_WEAPON) fq.flags |= FQ_TRANSPOINT; //if (objP->nType == OBJ_PLAYER) fq.flags |= FQ_GET_SEGLIST; vSaveP0 = *fq.p0; vSaveP1 = *fq.p1; fate = FindVectorIntersection (&fq, &hi); #ifdef _DEBUG if (fate == HIT_WALL) fate = FindVectorIntersection (&fq, &hi); #endif // Matt: Mike's hack. if (fate == HIT_OBJECT) { tObject *objP = gameData.objs.objects + hi.hit.nObject; if ((objP->nType == OBJ_WEAPON) && ((objP->id == PROXIMITY_ID) || (objP->id == SUPERPROX_ID))) count--; } #ifdef _DEBUG if (fate == HIT_BAD_P0) { #if 0 //TRACE con_printf (CON_DEBUG, "Warning: Bad p0 in physics! Object = %i, nType = %i [%s]\n", objP - gameData.objs.objects, objP->nType, ObjectType_names [objP->nType]); #endif Int3 (); } #endif //if ((objP->nType == OBJ_PLAYER) || (objP->nType == OBJ_ROBOT) || (objP->nType == OBJ_MONSTERBALL)) { int i; #ifdef RELEASE int j; #endif if (nPhysSegs && (physSegList [nPhysSegs-1] == hi.segList [0])) nPhysSegs--; #ifdef RELEASE j = MAX_FVI_SEGS - nPhysSegs - 1; if (j > hi.nSegments) j = hi.nSegments; memcpy (physSegList + nPhysSegs, hi.segList, j * sizeof (*physSegList)); nPhysSegs += j; #else for (i = 0; (i < hi.nSegments) && (nPhysSegs < MAX_FVI_SEGS-1); ) { if (hi.segList [i] > gameData.segs.nLastSegment) LogErr ("Invalid segment in segment list #1\n"); physSegList [nPhysSegs++] = hi.segList [i++]; } #endif } ipos = hi.hit.vPoint; iseg = hi.hit.nSegment; nWallHitSide = hi.hit.nSide; nWallHitSeg = hi.hit.nSideSegment; if (iseg==-1) { //some sort of horrible error if (objP->nType == OBJ_WEAPON) objP->flags |= OF_SHOULD_BE_DEAD; break; } Assert (!((fate==HIT_WALL) && ((nWallHitSeg == -1) || (nWallHitSeg > gameData.segs.nLastSegment)))); vSavePos = objP->pos; //save the tObject's position nSaveSeg = objP->nSegment; // update tObject's position and tSegment number objP->pos = ipos; if (iseg != objP->nSegment) RelinkObject (nObject, iseg); //if start point not in tSegment, move tObject to center of tSegment if (GetSegMasks (&objP->pos, objP->nSegment, 0).centerMask) { //tObject stuck vmsVector vCenter; int n = FindObjectSeg (objP); if (n == -1) { n = FindSegByPoint (&objP->last_pos, objP->nSegment); if (n == -1) { objP->flags |= OF_SHOULD_BE_DEAD; return; } } objP->pos = objP->last_pos; RelinkObject (nObject, n); COMPUTE_SEGMENT_CENTER_I (&vCenter, objP->nSegment); VmVecDec (&vCenter, &objP->pos); if (VmVecMag (&vCenter) > F1_0) { VmVecNormalize (&vCenter); VmVecScaleFrac (&vCenter, 1, 10); } VmVecDec (&objP->pos, &vCenter); //return; } //calulate new sim time { //vmsVector vMoved; vmsVector vMoveNormal; fix attemptedDist, actualDist; xOldSimTime = xSimTime; actualDist = VmVecNormalizedDir (&vMoveNormal, &objP->pos, &vSavePos); if ((fate == HIT_WALL) && (VmVecDot (&vMoveNormal, &vFrame) < 0)) { //moved backwards //don't change position or xSimTime objP->pos = vSavePos; //iseg = objP->nSegment; //don't change tSegment if (nSaveSeg != iseg) RelinkObject (nObject, nSaveSeg); if (bDoSpeedBoost) { // int h = FindSegByPoint (&vNewPos, -1); objP->pos = vStartPos; SetSpeedBoostVelocity (nObject, -1, -1, -1, -1, -1, &vStartPos, &sbd.vDest, 0); VmVecCopyScale (&vFrame, &sbd.vVel, xSimTime); goto retryMove; } xMovedTime = 0; } else { //retryMove2: attemptedDist = VmVecMag (&vFrame); xSimTime = FixMulDiv (xSimTime, attemptedDist-actualDist, attemptedDist); xMovedTime = xOldSimTime - xSimTime; if ((xSimTime < 0) || (xSimTime > xOldSimTime)) { xSimTime = xOldSimTime; xMovedTime = 0; } } } switch (fate) { case HIT_WALL: { vmsVector vMoved; fix xHitSpeed, xWallPart, xSideDist, xSideDists [6]; short nSegment; // Find hit speed #if 0//def _DEBUG if (objP->nType == OBJ_PLAYER) HUDMessage (0, "WALL CONTACT"); fate = FindVectorIntersection (&fq, &hi); #endif VmVecSub (&vMoved, &objP->pos, &vSavePos); xWallPart = VmVecDot (&vMoved, &hi.hit.vNormal); if (xWallPart && (xMovedTime > 0) && (xHitSpeed = -FixDiv (xWallPart, xMovedTime)) > 0) { CollideObjectWithWall (objP, xHitSpeed, nWallHitSeg, nWallHitSide, &hi.hit.vPoint); #if 0//def _DEBUG if (objP->nType == OBJ_PLAYER) HUDMessage (0, "BUMP!"); #endif } else { #if 0//def _DEBUG if (objP->nType == OBJ_PLAYER) HUDMessage (0, "SCREEEEEEEEEECH"); #endif ScrapeObjectOnWall (objP, nWallHitSeg, nWallHitSide, &hi.hit.vPoint); } Assert (nWallHitSeg > -1); Assert (nWallHitSide > -1); GetSideDistsAll (&objP->pos, nWallHitSeg, xSideDists); if ((xSideDist = xSideDists [nWallHitSide]) && (xSideDist < objP->size - objP->size / 100)) { #if 1 float r = 0.1f; #else float r; xSideDist = objP->size - xSideDist; r = ((float) xSideDist / (float) objP->size) * f2fl (objP->size); #endif objP->pos.x += (fix) ((float) hi.hit.vNormal.x * r); objP->pos.y += (fix) ((float) hi.hit.vNormal.y * r); objP->pos.z += (fix) ((float) hi.hit.vNormal.z * r); nSegment = FindSegByPoint (&objP->pos, objP->nSegment); if (nSegment != objP->nSegment) RelinkObject (OBJ_IDX (objP), nSegment); #if 0//def _DEBUG if (objP->nType == OBJ_PLAYER) HUDMessage (0, "PENETRATING WALL (%d, %1.4f)", objP->size - xSideDists [nWallHitSide], r); #endif bRetry = 1; } if (!(objP->flags & OF_SHOULD_BE_DEAD)) { int forcefield_bounce; //bounce off a forcefield Assert (gameStates.app.cheats.bBouncingWeapons || !(objP->mType.physInfo.flags & PF_STICK && objP->mType.physInfo.flags & PF_BOUNCE)); //can't be bounce and stick forcefield_bounce = (gameData.pig.tex.pTMapInfo [gameData.segs.segments [nWallHitSeg].sides [nWallHitSide].nBaseTex].flags & TMI_FORCE_FIELD); if (!forcefield_bounce && (objP->mType.physInfo.flags & PF_STICK)) { //stop moving AddStuckObject (objP, nWallHitSeg, nWallHitSide); VmVecZero (&objP->mType.physInfo.velocity); bObjStopped = 1; bRetry = 0; } else { // Slide tObject along wall int bCheckVel = 0; //We're constrained by wall, so subtract wall part from //velocity vector xWallPart = VmVecDot (&hi.hit.vNormal, &objP->mType.physInfo.velocity); if (forcefield_bounce || (objP->mType.physInfo.flags & PF_BOUNCE)) { //bounce off wall xWallPart *= 2; //Subtract out wall part twice to achieve bounce if (forcefield_bounce) { bCheckVel = 1; //check for max velocity if (objP->nType == OBJ_PLAYER) xWallPart *= 2; //player bounce twice as much } if (objP->mType.physInfo.flags & PF_BOUNCES_TWICE) { Assert (objP->mType.physInfo.flags & PF_BOUNCE); if (objP->mType.physInfo.flags & PF_BOUNCED_ONCE) objP->mType.physInfo.flags &= ~ (PF_BOUNCE+PF_BOUNCED_ONCE+PF_BOUNCES_TWICE); else objP->mType.physInfo.flags |= PF_BOUNCED_ONCE; } bBounced = 1; //this tObject bBounced } VmVecScaleInc (&objP->mType.physInfo.velocity, &hi.hit.vNormal, -xWallPart); if (bCheckVel) { fix vel = VmVecMag (&objP->mType.physInfo.velocity); if (vel > MAX_OBJECT_VEL) VmVecScale (&objP->mType.physInfo.velocity, FixDiv (MAX_OBJECT_VEL, vel)); } if (bBounced && (objP->nType == OBJ_WEAPON)) VmVector2Matrix (&objP->orient, &objP->mType.physInfo.velocity, &objP->orient.uVec, NULL); bRetry = 1; } } break; } case HIT_OBJECT: { vmsVector vOldVel; vmsVector *ppos0, *ppos1, vHitPos; fix size0, size1; // Mark the hit tObject so that on a retry the fvi code // ignores this tObject. //if (bSpeedBoost && (objP == gameData.objs.console)) // break; Assert (hi.hit.nObject != -1); ppos0 = &gameData.objs.objects [hi.hit.nObject].pos; ppos1 = &objP->pos; size0 = gameData.objs.objects [hi.hit.nObject].size; size1 = objP->size; // Calculcate the hit point between the two objects. Assert (size0+size1 != 0); // Error, both sizes are 0, so how did they collide, anyway?!? //VmVecScale (VmVecSub (&pos_hit, ppos1, ppos0), FixDiv (size0, size0 + size1); //VmVecInc (&pos_hit, ppos0); VmVecSub (&vHitPos, ppos1, ppos0); VmVecScaleAdd (&vHitPos, ppos0, &vHitPos, FixDiv (size0, size0 + size1)); vOldVel = objP->mType.physInfo.velocity; CollideTwoObjects (objP, gameData.objs.objects + hi.hit.nObject, &vHitPos); if (sbd.bBoosted && (objP == gameData.objs.console)) objP->mType.physInfo.velocity = vOldVel; // Let tObject continue its movement if (!(objP->flags&OF_SHOULD_BE_DEAD) ) { if (objP->mType.physInfo.flags&PF_PERSISTENT || (vOldVel.x == objP->mType.physInfo.velocity.x && vOldVel.y == objP->mType.physInfo.velocity.y && vOldVel.z == objP->mType.physInfo.velocity.z)) { //if (gameData.objs.objects [hi.hit.nObject].nType == OBJ_POWERUP) ignoreObjList [nIgnoreObjs++] = hi.hit.nObject; bRetry = 1; } } break; } case HIT_NONE: #ifdef TACTILE if (TactileStick && objP==gameData.objs.console && !(FrameCount & 15)) Tactile_Xvibrate_clear (); #endif break; #ifdef _DEBUG case HIT_BAD_P0: Int3 (); // Unexpected collision nType: start point not in specified tSegment. #if TRACE con_printf (CON_DEBUG, "Warning: Bad p0 in physics!!!\n"); #endif break; default: // Unknown collision nType returned from FindVectorIntersection!! Int3 (); break; #endif } } while (bRetry); // Pass retry count info to AI. if (objP->controlType == CT_AI) { if (count > 0) { gameData.ai.localInfo [nObject].nRetryCount = count-1; #ifdef _DEBUG Total_retries += count-1; Total_sims++; #endif } } //I'm not sure why we do this. I wish there were a comment that //explained it. I think maybe it only needs to be done if the tObject //is sliding, but I don't know if (!(sbd.bBoosted || bObjStopped || bBounced)) { //Set velocity from actual movement vmsVector vMoved; VmVecSub (&vMoved, &objP->pos, &vStartPos); VmVecCopyScale (&objP->mType.physInfo.velocity, &vMoved, FixMulDiv (FixDiv (f1_0, gameData.time.xFrame), 100, xTimeScale)); #ifdef BUMP_HACK if (objP == gameData.objs.console && objP->mType.physInfo.velocity.x==0 && objP->mType.physInfo.velocity.y==0 && objP->mType.physInfo.velocity.z==0 && (objP->mType.physInfo.thrust.x!=0 || objP->mType.physInfo.thrust.y!=0 || objP->mType.physInfo.thrust.z!=0)) { vmsVector vCenter, vBump; //bump player a little towards vCenter of tSegment to unstick COMPUTE_SEGMENT_CENTER_I (&vCenter, objP->nSegment); //HUDMessage (0, "BUMP! %d %d", d1, d2); //don't bump player toward vCenter of reactor tSegment if (gameData.segs.segment2s [objP->nSegment].special == SEGMENT_IS_CONTROLCEN) VmVecNegate (&vBump); VmVecScaleInc (&objP->pos, &vBump, objP->size/5); //if moving away from seg, might move out of seg, so update if (gameData.segs.segment2s [objP->nSegment].special == SEGMENT_IS_CONTROLCEN) UpdateObjectSeg (objP); } #endif } //Assert (check_point_in_seg (&objP->pos, objP->nSegment, 0).centerMask==0); //if (objP->controlType == CT_FLYING) if (objP->mType.physInfo.flags & PF_LEVELLING) DoPhysicsAlignObject (objP); //hack to keep player from going through closed doors if ((objP->nType == OBJ_PLAYER) && (objP->nSegment != nOrigSegment) && (gameStates.app.cheats.bPhysics != 0xBADA55)) { int nSide; nSide = FindConnectedSide (gameData.segs.segments + objP->nSegment, gameData.segs.segments + nOrigSegment); if (nSide != -1) { if (!(WALL_IS_DOORWAY (gameData.segs.segments + nOrigSegment, nSide, NULL) & WID_FLY_FLAG)) { tSide *sideP; int nVertex, nFaces; fix dist; int vertex_list [6]; //bump tObject back sideP = gameData.segs.segments [nOrigSegment].sides + nSide; if (nOrigSegment==-1) Error ("nOrigSegment == -1 in physics"); CreateAbsVertexLists (&nFaces, vertex_list, nOrigSegment, nSide); //let'sideP pretend this wall is not triangulated nVertex = vertex_list [0]; if (nVertex > vertex_list [1]) nVertex = vertex_list [1]; if (nVertex > vertex_list [2]) nVertex = vertex_list [2]; if (nVertex > vertex_list [3]) nVertex = vertex_list [3]; #ifdef COMPACT_SEGS { vmsVector _vn; get_side_normal (gameData.segs.segments + nOrigSegment, nSide, 0, &_vn); dist = VmDistToPlane (&vStartPos, &_vn, &gameData.segs.vertices [nVertex]); VmVecScaleAdd (&objP->pos, &vStartPos, &_vn, objP->size-dist); } #else dist = VmDistToPlane (&vStartPos, sideP->normals, gameData.segs.vertices + nVertex); VmVecScaleAdd (&objP->pos, &vStartPos, sideP->normals, objP->size-dist); #endif UpdateObjectSeg (objP); } } } //if end point not in tSegment, move tObject to last pos, or tSegment center if (GetSegMasks (&objP->pos, objP->nSegment, 0).centerMask) { if (FindObjectSeg (objP) == -1) { int n; if (objP->nType==OBJ_PLAYER && (n=FindSegByPoint (&objP->last_pos, objP->nSegment))!=-1) { objP->pos = objP->last_pos; RelinkObject (nObject, n); } else { COMPUTE_SEGMENT_CENTER_I (&objP->pos, objP->nSegment); objP->pos.x += nObject; } if (objP->nType == OBJ_WEAPON) objP->flags |= OF_SHOULD_BE_DEAD; } } UnstickObject (objP); }
// ----------------------------------------------------------------------------------------------------------- //Simulate a physics object for this frame void do_physics_sim(object *obj) { int ignore_obj_list[MAX_IGNORE_OBJS],n_ignore_objs; int iseg; int try_again; int fate; vms_vector frame_vec; //movement in this frame vms_vector new_pos,ipos; //position after this frame int count=0; int objnum; int WallHitSeg, WallHitSide; fvi_info hit_info; fvi_query fq; vms_vector save_pos; int save_seg; fix drag; fix sim_time,old_sim_time; vms_vector start_pos; int obj_stopped=0; fix moved_time; //how long objected moved before hit something vms_vector save_p0,save_p1; physics_info *pi; int orig_segnum = obj->segnum; int bounced=0; Assert(obj->type != OBJ_NONE); Assert(obj->movement_type == MT_PHYSICS); #ifndef NDEBUG if (Dont_move_ai_objects) if (obj->control_type == CT_AI) return; #endif pi = &obj->mtype.phys_info; do_physics_sim_rot(obj); if (!(pi->velocity.x || pi->velocity.y || pi->velocity.z || pi->thrust.x || pi->thrust.y || pi->thrust.z)) return; objnum = obj-Objects; n_phys_segs = 0; disable_new_fvi_stuff = (obj->type != OBJ_PLAYER); sim_time = FrameTime; //debug_obj = obj; #ifdef EXTRA_DEBUG if (obj == debug_obj) { printf("object %d:\n start pos = %x %x %x\n",objnum,XYZ(&obj->pos)); printf(" thrust = %x %x %x\n",XYZ(&obj->mtype.phys_info.thrust)); printf(" sim_time = %x\n",sim_time); } //check for correct object segment if(!get_seg_masks(&obj->pos,obj->segnum,0).centermask==0) { #ifndef NDEBUG mprintf((0,"Warning: object %d not in given seg!\n",objnum)); #endif //Int3(); Removed by Rob 10/5/94 if (!update_object_seg(obj)) { #ifndef NDEBUG mprintf((0,"Warning: can't find seg for object %d - moving\n",objnum)); #endif if (!(Game_mode & GM_MULTI)) Int3(); compute_segment_center(&obj->pos,&Segments[obj->segnum]); obj->pos.x += objnum; } } #endif start_pos = obj->pos; n_ignore_objs = 0; Assert(obj->mtype.phys_info.brakes==0); //brakes not used anymore? //if uses thrust, cannot have zero drag Assert(!(obj->mtype.phys_info.flags&PF_USES_THRUST) || obj->mtype.phys_info.drag!=0); //mprintf((0,"thrust=%x speed=%x\n",vm_vec_mag(&obj->mtype.phys_info.thrust),vm_vec_mag(&obj->mtype.phys_info.velocity))); //do thrust & drag if ((drag = obj->mtype.phys_info.drag) != 0) { int count; vms_vector accel; fix r,k; count = sim_time / FT; r = sim_time % FT; k = fixdiv(r,FT); if (obj->mtype.phys_info.flags & PF_USES_THRUST) { vm_vec_copy_scale(&accel,&obj->mtype.phys_info.thrust,fixdiv(f1_0,obj->mtype.phys_info.mass)); while (count--) { vm_vec_add2(&obj->mtype.phys_info.velocity,&accel); vm_vec_scale(&obj->mtype.phys_info.velocity,f1_0-drag); } //do linear scale on remaining bit of time vm_vec_scale_add2(&obj->mtype.phys_info.velocity,&accel,k); vm_vec_scale(&obj->mtype.phys_info.velocity,f1_0-fixmul(k,drag)); } else { fix total_drag=f1_0; while (count--) total_drag = fixmul(total_drag,f1_0-drag); //do linear scale on remaining bit of time total_drag = fixmul(total_drag,f1_0-fixmul(k,drag)); vm_vec_scale(&obj->mtype.phys_info.velocity,total_drag); } } #ifdef EXTRA_DEBUG if (obj == debug_obj) printf(" velocity = %x %x %x\n",XYZ(&obj->mtype.phys_info.velocity)); #endif do { try_again = 0; //Move the object vm_vec_copy_scale(&frame_vec, &obj->mtype.phys_info.velocity, sim_time); #ifdef EXTRA_DEBUG if (obj == debug_obj) printf(" pass %d, frame_vec = %x %x %x\n",count,XYZ(&frame_vec)); #endif if ( (frame_vec.x==0) && (frame_vec.y==0) && (frame_vec.z==0) ) break; count++; // If retry count is getting large, then we are trying to do something stupid. if ( count > 3) { if (obj->type == OBJ_PLAYER) { if (count > 8) break; } else break; } vm_vec_add(&new_pos,&obj->pos,&frame_vec); #ifdef EXTRA_DEBUG if (obj == debug_obj) printf(" desired_pos = %x %x %x\n",XYZ(&new_pos)); #endif ignore_obj_list[n_ignore_objs] = -1; #ifdef EXTRA_DEBUG if (obj == debug_obj) { printf(" FVI parms: p0 = %8x %8x %8x, segnum=%x, size=%x\n",XYZ(&obj->pos),obj->segnum,obj->size); printf(" p1 = %8x %8x %8x\n",XYZ(&new_pos)); } #endif fq.p0 = &obj->pos; fq.startseg = obj->segnum; fq.p1 = &new_pos; fq.rad = obj->size; fq.thisobjnum = objnum; fq.ignore_obj_list = ignore_obj_list; fq.flags = FQ_CHECK_OBJS; if (obj->type == OBJ_WEAPON) fq.flags |= FQ_TRANSPOINT; if (obj->type == OBJ_PLAYER) fq.flags |= FQ_GET_SEGLIST; //@@ if (get_seg_masks(&obj->pos,obj->segnum,0).centermask!=0) //@@ Int3(); save_p0 = *fq.p0; save_p1 = *fq.p1; fate = find_vector_intersection(&fq,&hit_info); // Matt: Mike's hack. if (fate == HIT_OBJECT) { object *objp = &Objects[hit_info.hit_object]; if ((objp->type == OBJ_WEAPON) && ((objp->id == PROXIMITY_ID) || (objp->id == SUPERPROX_ID))) count--; } #ifndef NDEBUG if (fate == HIT_BAD_P0) { mprintf((0,"Warning: Bad p0 in physics! Object = %i, type = %i [%s]\n", obj-Objects, obj->type, Object_type_names[obj->type])); Int3(); } #endif if (obj->type == OBJ_PLAYER) { int i; if (n_phys_segs && phys_seglist[n_phys_segs-1]==hit_info.seglist[0]) n_phys_segs--; for (i=0;(i<hit_info.n_segs) && (n_phys_segs<MAX_FVI_SEGS-1); ) phys_seglist[n_phys_segs++] = hit_info.seglist[i++]; } #ifdef EXTRA_DEBUG if (obj == debug_obj) printf(" fate = %d, hit_pnt = %8x %8x %8x\n",fate,XYZ(&hit_info.hit_pnt));; #endif ipos = hit_info.hit_pnt; iseg = hit_info.hit_seg; WallHitSide = hit_info.hit_side; WallHitSeg = hit_info.hit_side_seg; if (iseg==-1) { //some sort of horrible error #ifndef NDEBUG mprintf((1,"iseg==-1 in physics! Object = %i, type = %i (%s)\n", obj-Objects, obj->type, Object_type_names[obj->type])); #endif //Int3(); //compute_segment_center(&ipos,&Segments[obj->segnum]); //ipos.x += objnum; //iseg = obj->segnum; //fate = HIT_NONE; if (obj->type == OBJ_WEAPON) obj->flags |= OF_SHOULD_BE_DEAD; break; } Assert(!((fate==HIT_WALL) && ((WallHitSeg == -1) || (WallHitSeg > Highest_segment_index)))); //if(!get_seg_masks(&hit_info.hit_pnt,hit_info.hit_seg,0).centermask==0) // Int3(); save_pos = obj->pos; //save the object's position save_seg = obj->segnum; // update object's position and segment number obj->pos = ipos; #ifdef EXTRA_DEBUG if (obj == debug_obj) printf(" new pos = %x %x %x\n",XYZ(&obj->pos)); #endif if ( iseg != obj->segnum ) obj_relink(objnum, iseg ); //if start point not in segment, move object to center of segment if (get_seg_masks(&obj->pos,obj->segnum,0).centermask!=0) { int n; if ((n=find_object_seg(obj))==-1) { //Int3(); if (obj->type==OBJ_PLAYER && (n=find_point_seg(&obj->last_pos,obj->segnum))!=-1) { obj->pos = obj->last_pos; obj_relink(objnum, n ); } else { compute_segment_center(&obj->pos,&Segments[obj->segnum]); obj->pos.x += objnum; } if (obj->type == OBJ_WEAPON) obj->flags |= OF_SHOULD_BE_DEAD; } return; } //calulate new sim time { //vms_vector moved_vec; vms_vector moved_vec_n; fix attempted_dist,actual_dist; old_sim_time = sim_time; actual_dist = vm_vec_normalized_dir(&moved_vec_n,&obj->pos,&save_pos); if (fate==HIT_WALL && vm_vec_dot(&moved_vec_n,&frame_vec) < 0) { //moved backwards //don't change position or sim_time //******* mprintf((0,"Obj %d moved backwards\n",obj-Objects)); #ifdef EXTRA_DEBUG if (obj == debug_obj) printf(" Warning: moved backwards!\n"); #endif obj->pos = save_pos; //iseg = obj->segnum; //don't change segment obj_relink(objnum, save_seg ); moved_time = 0; } else { //if (obj == debug_obj) // printf(" moved_vec = %x %x %x\n",XYZ(&moved_vec)); attempted_dist = vm_vec_mag(&frame_vec); sim_time = fixmuldiv(sim_time,attempted_dist-actual_dist,attempted_dist); moved_time = old_sim_time - sim_time; if (sim_time < 0 || sim_time>old_sim_time) { #ifndef NDEBUG mprintf((0,"Bogus sim_time = %x, old = %x\n",sim_time,old_sim_time)); if (obj == debug_obj) printf(" Bogus sim_time = %x, old = %x, attempted_dist = %x, actual_dist = %x\n",sim_time,old_sim_time,attempted_dist,actual_dist); //Int3(); Removed by Rob #endif sim_time = old_sim_time; //WHY DOES THIS HAPPEN?? moved_time = 0; } } #ifdef EXTRA_DEBUG if (obj == debug_obj) printf(" new sim_time = %x\n",sim_time); #endif } switch( fate ) { case HIT_WALL: { vms_vector moved_v; //@@fix total_d,moved_d; fix hit_speed,wall_part; // Find hit speed vm_vec_sub(&moved_v,&obj->pos,&save_pos); wall_part = vm_vec_dot(&moved_v,&hit_info.hit_wallnorm); if (wall_part != 0 && moved_time>0 && (hit_speed=-fixdiv(wall_part,moved_time))>0) collide_object_with_wall( obj, hit_speed, WallHitSeg, WallHitSide, &hit_info.hit_pnt ); else scrape_object_on_wall(obj, WallHitSeg, WallHitSide, &hit_info.hit_pnt ); Assert( WallHitSeg > -1 ); Assert( WallHitSide > -1 ); if ( !(obj->flags&OF_SHOULD_BE_DEAD) ) { int forcefield_bounce; //bounce off a forcefield Assert(BounceCheat || !(obj->mtype.phys_info.flags & PF_STICK && obj->mtype.phys_info.flags & PF_BOUNCE)); //can't be bounce and stick forcefield_bounce = (TmapInfo[Segments[WallHitSeg].sides[WallHitSide].tmap_num].flags & TMI_FORCE_FIELD); if (!forcefield_bounce && (obj->mtype.phys_info.flags & PF_STICK)) { //stop moving // mprintf((0, "Object %i stuck at %i:%i\n", obj-Objects, WallHitSeg, WallHitSide)); add_stuck_object(obj, WallHitSeg, WallHitSide); vm_vec_zero(&obj->mtype.phys_info.velocity); obj_stopped = 1; try_again = 0; } else { // Slide object along wall int check_vel=0; //We're constrained by wall, so subtract wall part from //velocity vector wall_part = vm_vec_dot(&hit_info.hit_wallnorm,&obj->mtype.phys_info.velocity); // mprintf((0, "%d", f2i(vm_vec_mag(&hit_info.hit_wallnorm)) )); if (forcefield_bounce || (obj->mtype.phys_info.flags & PF_BOUNCE)) { //bounce off wall wall_part *= 2; //Subtract out wall part twice to achieve bounce if (forcefield_bounce) { check_vel = 1; //check for max velocity if (obj->type == OBJ_PLAYER) wall_part *= 2; //player bounce twice as much } if ( obj->mtype.phys_info.flags & PF_BOUNCES_TWICE) { Assert(obj->mtype.phys_info.flags & PF_BOUNCE); if (obj->mtype.phys_info.flags & PF_BOUNCED_ONCE) obj->mtype.phys_info.flags &= ~(PF_BOUNCE+PF_BOUNCED_ONCE+PF_BOUNCES_TWICE); else obj->mtype.phys_info.flags |= PF_BOUNCED_ONCE; } bounced = 1; //this object bounced } vm_vec_scale_add2(&obj->mtype.phys_info.velocity,&hit_info.hit_wallnorm,-wall_part); // mprintf((0, "Velocity at bounce time = %d\n", f2i(vm_vec_mag(&obj->mtype.phys_info.velocity)))); //if (obj==ConsoleObject) // mprintf((0,"player vel = %x\n",vm_vec_mag_quick(&obj->mtype.phys_info.velocity))); if (check_vel) { fix vel = vm_vec_mag_quick(&obj->mtype.phys_info.velocity); if (vel > MAX_OBJECT_VEL) vm_vec_scale(&obj->mtype.phys_info.velocity,fixdiv(MAX_OBJECT_VEL,vel)); } if (bounced && obj->type == OBJ_WEAPON) vm_vector_2_matrix(&obj->orient,&obj->mtype.phys_info.velocity,&obj->orient.uvec,NULL); #ifdef EXTRA_DEBUG if (obj == debug_obj) { printf(" sliding - wall_norm %x %x %x %x\n",wall_part,XYZ(&hit_info.hit_wallnorm)); printf(" wall_part %x, new velocity = %x %x %x\n",wall_part,XYZ(&obj->mtype.phys_info.velocity)); } #endif try_again = 1; } } break; } case HIT_OBJECT: { vms_vector old_vel; // Mark the hit object so that on a retry the fvi code // ignores this object. Assert(hit_info.hit_object != -1); // Calculcate the hit point between the two objects. { vms_vector *ppos0, *ppos1, pos_hit; fix size0, size1; ppos0 = &Objects[hit_info.hit_object].pos; ppos1 = &obj->pos; size0 = Objects[hit_info.hit_object].size; size1 = obj->size; Assert(size0+size1 != 0); // Error, both sizes are 0, so how did they collide, anyway?!? //vm_vec_scale(vm_vec_sub(&pos_hit, ppos1, ppos0), fixdiv(size0, size0 + size1)); //vm_vec_add2(&pos_hit, ppos0); vm_vec_sub(&pos_hit, ppos1, ppos0); vm_vec_scale_add(&pos_hit,ppos0,&pos_hit,fixdiv(size0, size0 + size1)); old_vel = obj->mtype.phys_info.velocity; collide_two_objects( obj, &Objects[hit_info.hit_object], &pos_hit); } // Let object continue its movement if ( !(obj->flags&OF_SHOULD_BE_DEAD) ) { //obj->pos = save_pos; if (obj->mtype.phys_info.flags&PF_PERSISTENT || (old_vel.x == obj->mtype.phys_info.velocity.x && old_vel.y == obj->mtype.phys_info.velocity.y && old_vel.z == obj->mtype.phys_info.velocity.z)) { //if (Objects[hit_info.hit_object].type == OBJ_POWERUP) ignore_obj_list[n_ignore_objs++] = hit_info.hit_object; try_again = 1; } } break; } case HIT_NONE: #ifdef TACTILE if (TactileStick && obj==ConsoleObject && !(FrameCount & 15)) Tactile_Xvibrate_clear (); #endif break; #ifndef NDEBUG case HIT_BAD_P0: Int3(); // Unexpected collision type: start point not in specified segment. mprintf((0,"Warning: Bad p0 in physics!!!\n")); break; default: // Unknown collision type returned from find_vector_intersection!! Int3(); break; #endif } } while ( try_again ); // Pass retry count info to AI. if (obj->control_type == CT_AI) { if (count > 0) { Ai_local_info[objnum].retry_count = count-1; #ifndef NDEBUG Total_retries += count-1; Total_sims++; #endif } } //I'm not sure why we do this. I wish there were a comment that //explained it. I think maybe it only needs to be done if the object //is sliding, but I don't know if (!obj_stopped && !bounced) { //Set velocity from actual movement vms_vector moved_vec; vm_vec_sub(&moved_vec,&obj->pos,&start_pos); vm_vec_copy_scale(&obj->mtype.phys_info.velocity,&moved_vec,fixdiv(f1_0,FrameTime)); #ifdef BUMP_HACK if (obj==ConsoleObject && (obj->mtype.phys_info.velocity.x==0 && obj->mtype.phys_info.velocity.y==0 && obj->mtype.phys_info.velocity.z==0) && !(obj->mtype.phys_info.thrust.x==0 && obj->mtype.phys_info.thrust.y==0 && obj->mtype.phys_info.thrust.z==0)) { vms_vector center,bump_vec; //bump player a little towards center of segment to unstick compute_segment_center(¢er,&Segments[obj->segnum]); vm_vec_normalized_dir_quick(&bump_vec,¢er,&obj->pos); //don't bump player toward center of reactor segment if (Segment2s[obj->segnum].special == SEGMENT_IS_CONTROLCEN) vm_vec_negate(&bump_vec); vm_vec_scale_add2(&obj->pos,&bump_vec,obj->size/5); //if moving away from seg, might move out of seg, so update if (Segment2s[obj->segnum].special == SEGMENT_IS_CONTROLCEN) update_object_seg(obj); } #endif } //Assert(check_point_in_seg(&obj->pos,obj->segnum,0).centermask==0); //if (obj->control_type == CT_FLYING) if (obj->mtype.phys_info.flags & PF_LEVELLING) do_physics_align_object( obj ); //hack to keep player from going through closed doors if (obj->type==OBJ_PLAYER && obj->segnum!=orig_segnum && (Physics_cheat_flag!=0xBADA55) ) { int sidenum; sidenum = find_connect_side(&Segments[obj->segnum],&Segments[orig_segnum]); if (sidenum != -1) { if (! (WALL_IS_DOORWAY(&Segments[orig_segnum],sidenum) & WID_FLY_FLAG)) { side *s; int vertnum,num_faces,i; fix dist; int vertex_list[6]; //bump object back s = &Segments[orig_segnum].sides[sidenum]; if (orig_segnum==-1) Error("orig_segnum == -1 in physics"); create_abs_vertex_lists( &num_faces, vertex_list, orig_segnum, sidenum); //let's pretend this wall is not triangulated vertnum = vertex_list[0]; for (i=1;i<4;i++) if (vertex_list[i] < vertnum) vertnum = vertex_list[i]; #ifdef COMPACT_SEGS { vms_vector _vn; get_side_normal(&Segments[orig_segnum], sidenum, 0, &_vn ); dist = vm_dist_to_plane(&start_pos, &_vn, &Vertices[vertnum]); vm_vec_scale_add(&obj->pos,&start_pos,&_vn,obj->size-dist); } #else dist = vm_dist_to_plane(&start_pos, &s->normals[0], &Vertices[vertnum]); vm_vec_scale_add(&obj->pos,&start_pos,&s->normals[0],obj->size-dist); #endif update_object_seg(obj); } } } //--WE ALWYS WANT THIS IN, MATT AND MIKE DECISION ON 12/10/94, TWO MONTHS AFTER FINAL #ifndef NDEBUG //if end point not in segment, move object to last pos, or segment center if (get_seg_masks(&obj->pos,obj->segnum,0).centermask!=0) { if (find_object_seg(obj)==-1) { int n; //Int3(); if (obj->type==OBJ_PLAYER && (n=find_point_seg(&obj->last_pos,obj->segnum))!=-1) { obj->pos = obj->last_pos; obj_relink(objnum, n ); } else { compute_segment_center(&obj->pos,&Segments[obj->segnum]); obj->pos.x += objnum; } if (obj->type == OBJ_WEAPON) obj->flags |= OF_SHOULD_BE_DEAD; } } //--WE ALWYS WANT THIS IN, MATT AND MIKE DECISION ON 12/10/94, TWO MONTHS AFTER FINAL #endif }