1 /* $Id: physics.c,v 1.7 2004-08-28 23:17:45 schaffner Exp $ */
3 THE COMPUTER CODE CONTAINED HEREIN IS THE SOLE PROPERTY OF PARALLAX
4 SOFTWARE CORPORATION ("PARALLAX"). PARALLAX, IN DISTRIBUTING THE CODE TO
5 END-USERS, AND SUBJECT TO ALL OF THE TERMS AND CONDITIONS HEREIN, GRANTS A
6 ROYALTY-FREE, PERPETUAL LICENSE TO SUCH END-USERS FOR USE BY SUCH END-USERS
7 IN USING, DISPLAYING, AND CREATING DERIVATIVE WORKS THEREOF, SO LONG AS
8 SUCH USE, DISPLAY OR CREATION IS FOR NON-COMMERCIAL, ROYALTY OR REVENUE
9 FREE PURPOSES. IN NO EVENT SHALL THE END-USER USE THE COMPUTER CODE
10 CONTAINED HEREIN FOR REVENUE-BEARING PURPOSES. THE END-USER UNDERSTANDS
11 AND AGREES TO THE TERMS HEREIN AND ACCEPTS THE SAME BY USE OF THIS FILE.
12 COPYRIGHT 1993-1999 PARALLAX SOFTWARE CORPORATION. ALL RIGHTS RESERVED.
17 * Code for flying through the mines
27 static char rcsid[] = "$Id: physics.c,v 1.7 2004-08-28 23:17:45 schaffner Exp $";
57 //Global variables for physics system
59 #define ROLL_RATE 0x2000
60 #define DAMP_ANG 0x400 //min angle to bank
62 #define TURNROLL_SCALE (0x4ec4/2)
64 #define MAX_OBJECT_VEL i2f(100)
66 #define BUMP_HACK 1 //if defined, bump player when he gets stuck
68 //--unused-- int mike_mode=0;
70 //check point against each side of segment. return bitmask, where bit
71 //set means behind that side
73 int Physics_cheat_flag = 0;
74 extern char BounceCheat;
76 //##//returns the distance of a point (checkp) from a plane (defined by norm & planep)
77 //##fix dist_to_plane(vms_vector *checkp,vms_vector *norm,vms_vector *planep)
79 //## vms_vector deltap;
81 //## vm_vec_sub(&deltap,checkp,planep);
83 //## return vm_vec_dot(&deltap,norm);
86 //--unused-- int dpjm_old_joy_x, dpjm_old_joy_y;
88 int floor_levelling=0;
90 //--unused-- level_with_floor()
92 //--unused-- floor_levelling=1;
95 //make sure matrix is orthogonal
96 void check_and_fix_matrix(vms_matrix *m)
100 vm_vector_2_matrix(&tempm,&m->fvec,&m->uvec,NULL);
105 void do_physics_align_object( object * obj )
107 vms_vector desired_upvec;
108 fixang delta_ang,roll_ang;
109 //vms_vector forvec = {0,0,f1_0};
110 vms_matrix temp_matrix;
111 fix d,largest_d=-f1_0;
115 // bank player according to segment orientation
117 //find side of segment that player is most alligned with
122 get_side_normal( &Segments[obj->segnum], i, 0, &_tv1 );
123 d = vm_vec_dot(&_tv1,&obj->orient.uvec);
125 d = vm_vec_dot(&Segments[obj->segnum].sides[i].normals[0],&obj->orient.uvec);
128 if (d > largest_d) {largest_d = d; best_side=i;}
131 if (floor_levelling) {
133 // old way: used floor's normal as upvec
135 get_side_normal(&Segments[obj->segnum], 3, 0, &desired_upvec );
137 desired_upvec = Segments[obj->segnum].sides[3].normals[0];
141 else // new player leveling code: use normal of side closest to our up vec
142 if (get_num_faces(&Segments[obj->segnum].sides[best_side])==2) {
144 vms_vector normals[2];
145 get_side_normals(&Segments[obj->segnum], best_side, &normals[0], &normals[1] );
147 desired_upvec.x = (normals[0].x + normals[1].x) / 2;
148 desired_upvec.y = (normals[0].y + normals[1].y) / 2;
149 desired_upvec.z = (normals[0].z + normals[1].z) / 2;
151 vm_vec_normalize(&desired_upvec);
153 side *s = &Segments[obj->segnum].sides[best_side];
154 desired_upvec.x = (s->normals[0].x + s->normals[1].x) / 2;
155 desired_upvec.y = (s->normals[0].y + s->normals[1].y) / 2;
156 desired_upvec.z = (s->normals[0].z + s->normals[1].z) / 2;
158 vm_vec_normalize(&desired_upvec);
163 get_side_normal(&Segments[obj->segnum], best_side, 0, &desired_upvec );
165 desired_upvec = Segments[obj->segnum].sides[best_side].normals[0];
168 if (labs(vm_vec_dot(&desired_upvec,&obj->orient.fvec)) < f1_0/2) {
169 fixang save_delta_ang;
172 vm_vector_2_matrix(&temp_matrix,&obj->orient.fvec,&desired_upvec,NULL);
174 save_delta_ang = delta_ang = vm_vec_delta_ang(&obj->orient.uvec,&temp_matrix.uvec,&obj->orient.fvec);
176 delta_ang += obj->mtype.phys_info.turnroll;
178 if (abs(delta_ang) > DAMP_ANG) {
179 vms_matrix rotmat, new_pm;
181 roll_ang = fixmul(FrameTime,ROLL_RATE);
183 if (abs(delta_ang) < roll_ang) roll_ang = delta_ang;
184 else if (delta_ang<0) roll_ang = -roll_ang;
186 tangles.p = tangles.h = 0; tangles.b = roll_ang;
187 vm_angles_2_matrix(&rotmat,&tangles);
189 vm_matrix_x_matrix(&new_pm,&obj->orient,&rotmat);
190 obj->orient = new_pm;
192 else floor_levelling=0;
197 void set_object_turnroll(object *obj)
201 desired_bank = -fixmul(obj->mtype.phys_info.rotvel.y,TURNROLL_SCALE);
203 if (obj->mtype.phys_info.turnroll != desired_bank) {
204 fixang delta_ang,max_roll;
206 max_roll = fixmul(ROLL_RATE,FrameTime);
208 delta_ang = desired_bank - obj->mtype.phys_info.turnroll;
210 if (labs(delta_ang) < max_roll)
211 max_roll = delta_ang;
214 max_roll = -max_roll;
216 obj->mtype.phys_info.turnroll += max_roll;
221 //list of segments went through
222 int phys_seglist[MAX_FVI_SEGS],n_phys_segs;
225 #define MAX_IGNORE_OBJS 100
228 #define EXTRA_DEBUG 1 //no extra debug when NDEBUG is on
232 object *debug_obj=NULL;
235 #define XYZ(v) (v)->x,(v)->y,(v)->z
239 int Total_retries=0, Total_sims=0;
240 int Dont_move_ai_objects=0;
245 extern int disable_new_fvi_stuff;
246 // -----------------------------------------------------------------------------------------------------------
247 // add rotational velocity & acceleration
248 void do_physics_sim_rot(object *obj)
251 vms_matrix rotmat,new_orient;
255 Assert(FrameTime > 0); //Get MATT if hit this!
257 pi = &obj->mtype.phys_info;
259 if (!(pi->rotvel.x || pi->rotvel.y || pi->rotvel.z || pi->rotthrust.x || pi->rotthrust.y || pi->rotthrust.z))
262 if (obj->mtype.phys_info.drag) {
267 count = FrameTime / FT;
271 drag = (obj->mtype.phys_info.drag*5)/2;
273 if (obj->mtype.phys_info.flags & PF_USES_THRUST) {
275 vm_vec_copy_scale(&accel,&obj->mtype.phys_info.rotthrust,fixdiv(f1_0,obj->mtype.phys_info.mass));
279 vm_vec_add2(&obj->mtype.phys_info.rotvel,&accel);
281 vm_vec_scale(&obj->mtype.phys_info.rotvel,f1_0-drag);
284 //do linear scale on remaining bit of time
286 vm_vec_scale_add2(&obj->mtype.phys_info.rotvel,&accel,k);
287 vm_vec_scale(&obj->mtype.phys_info.rotvel,f1_0-fixmul(k,drag));
289 else if (! (obj->mtype.phys_info.flags & PF_FREE_SPINNING)) {
293 total_drag = fixmul(total_drag,f1_0-drag);
295 //do linear scale on remaining bit of time
297 total_drag = fixmul(total_drag,f1_0-fixmul(k,drag));
299 vm_vec_scale(&obj->mtype.phys_info.rotvel,total_drag);
304 //mprintf( (0, "Rot vel = %.3f,%.3f,%.3f\n", f2fl(obj->mtype.phys_info.rotvel.x),f2fl(obj->mtype.phys_info.rotvel.y), f2fl(obj->mtype.phys_info.rotvel.z) ));
308 //unrotate object for bank caused by turn
309 if (obj->mtype.phys_info.turnroll) {
312 tangles.p = tangles.h = 0;
313 tangles.b = -obj->mtype.phys_info.turnroll;
314 vm_angles_2_matrix(&rotmat,&tangles);
315 vm_matrix_x_matrix(&new_pm,&obj->orient,&rotmat);
316 obj->orient = new_pm;
319 tangles.p = fixmul(obj->mtype.phys_info.rotvel.x,FrameTime);
320 tangles.h = fixmul(obj->mtype.phys_info.rotvel.y,FrameTime);
321 tangles.b = fixmul(obj->mtype.phys_info.rotvel.z,FrameTime);
323 vm_angles_2_matrix(&rotmat,&tangles);
324 vm_matrix_x_matrix(&new_orient,&obj->orient,&rotmat);
325 obj->orient = new_orient;
327 if (obj->mtype.phys_info.flags & PF_TURNROLL)
328 set_object_turnroll(obj);
330 //re-rotate object for bank caused by turn
331 if (obj->mtype.phys_info.turnroll) {
334 tangles.p = tangles.h = 0;
335 tangles.b = obj->mtype.phys_info.turnroll;
336 vm_angles_2_matrix(&rotmat,&tangles);
337 vm_matrix_x_matrix(&new_pm,&obj->orient,&rotmat);
338 obj->orient = new_pm;
341 check_and_fix_matrix(&obj->orient);
344 // -----------------------------------------------------------------------------------------------------------
345 //Simulate a physics object for this frame
346 void do_physics_sim(object *obj)
348 int ignore_obj_list[MAX_IGNORE_OBJS],n_ignore_objs;
352 vms_vector frame_vec; //movement in this frame
353 vms_vector new_pos,ipos; //position after this frame
356 int WallHitSeg, WallHitSide;
362 fix sim_time,old_sim_time;
363 vms_vector start_pos;
365 fix moved_time; //how long objected moved before hit something
366 vms_vector save_p0,save_p1;
368 int orig_segnum = obj->segnum;
371 Assert(obj->type != OBJ_NONE);
372 Assert(obj->movement_type == MT_PHYSICS);
375 if (Dont_move_ai_objects)
376 if (obj->control_type == CT_AI)
380 pi = &obj->mtype.phys_info;
382 do_physics_sim_rot(obj);
384 if (!(pi->velocity.x || pi->velocity.y || pi->velocity.z || pi->thrust.x || pi->thrust.y || pi->thrust.z))
387 objnum = obj-Objects;
391 disable_new_fvi_stuff = (obj->type != OBJ_PLAYER);
393 sim_time = FrameTime;
398 if (obj == debug_obj) {
399 printf("object %d:\n start pos = %x %x %x\n",objnum,XYZ(&obj->pos));
400 printf(" thrust = %x %x %x\n",XYZ(&obj->mtype.phys_info.thrust));
401 printf(" sim_time = %x\n",sim_time);
404 //check for correct object segment
405 if(!get_seg_masks(&obj->pos, obj->segnum, 0, __FILE__, __LINE__).centermask == 0)
408 mprintf((0,"Warning: object %d not in given seg!\n",objnum));
410 //Int3(); Removed by Rob 10/5/94
411 if (!update_object_seg(obj)) {
413 mprintf((0,"Warning: can't find seg for object %d - moving\n",objnum));
415 if (!(Game_mode & GM_MULTI))
417 compute_segment_center(&obj->pos,&Segments[obj->segnum]);
418 obj->pos.x += objnum;
423 start_pos = obj->pos;
427 Assert(obj->mtype.phys_info.brakes==0); //brakes not used anymore?
429 //if uses thrust, cannot have zero drag
430 Assert(!(obj->mtype.phys_info.flags&PF_USES_THRUST) || obj->mtype.phys_info.drag!=0);
432 //mprintf((0,"thrust=%x speed=%x\n",vm_vec_mag(&obj->mtype.phys_info.thrust),vm_vec_mag(&obj->mtype.phys_info.velocity)));
436 if ((drag = obj->mtype.phys_info.drag) != 0) {
442 count = sim_time / FT;
446 if (obj->mtype.phys_info.flags & PF_USES_THRUST) {
448 vm_vec_copy_scale(&accel,&obj->mtype.phys_info.thrust,fixdiv(f1_0,obj->mtype.phys_info.mass));
452 vm_vec_add2(&obj->mtype.phys_info.velocity,&accel);
454 vm_vec_scale(&obj->mtype.phys_info.velocity,f1_0-drag);
457 //do linear scale on remaining bit of time
459 vm_vec_scale_add2(&obj->mtype.phys_info.velocity,&accel,k);
461 vm_vec_scale(&obj->mtype.phys_info.velocity,f1_0-fixmul(k,drag));
467 total_drag = fixmul(total_drag,f1_0-drag);
469 //do linear scale on remaining bit of time
471 total_drag = fixmul(total_drag,f1_0-fixmul(k,drag));
473 vm_vec_scale(&obj->mtype.phys_info.velocity,total_drag);
478 if (obj == debug_obj)
479 printf(" velocity = %x %x %x\n",XYZ(&obj->mtype.phys_info.velocity));
486 vm_vec_copy_scale(&frame_vec, &obj->mtype.phys_info.velocity, sim_time);
489 if (obj == debug_obj)
490 printf(" pass %d, frame_vec = %x %x %x\n",count,XYZ(&frame_vec));
493 if ( (frame_vec.x==0) && (frame_vec.y==0) && (frame_vec.z==0) )
498 // If retry count is getting large, then we are trying to do something stupid.
500 if (obj->type == OBJ_PLAYER) {
507 vm_vec_add(&new_pos,&obj->pos,&frame_vec);
510 if (obj == debug_obj)
511 printf(" desired_pos = %x %x %x\n",XYZ(&new_pos));
514 ignore_obj_list[n_ignore_objs] = -1;
517 if (obj == debug_obj) {
518 printf(" FVI parms: p0 = %8x %8x %8x, segnum=%x, size=%x\n",XYZ(&obj->pos),obj->segnum,obj->size);
519 printf(" p1 = %8x %8x %8x\n",XYZ(&new_pos));
524 fq.startseg = obj->segnum;
527 fq.thisobjnum = objnum;
528 fq.ignore_obj_list = ignore_obj_list;
529 fq.flags = FQ_CHECK_OBJS;
531 if (obj->type == OBJ_WEAPON)
532 fq.flags |= FQ_TRANSPOINT;
534 if (obj->type == OBJ_PLAYER)
535 fq.flags |= FQ_GET_SEGLIST;
537 //@@ if (get_seg_masks(&obj->pos, obj->segnum, 0, __FILE__, __LINE__).centermask != 0)
544 fate = find_vector_intersection(&fq,&hit_info);
545 // Matt: Mike's hack.
546 if (fate == HIT_OBJECT) {
547 object *objp = &Objects[hit_info.hit_object];
549 if ((objp->type == OBJ_WEAPON) && ((objp->id == PROXIMITY_ID) || (objp->id == SUPERPROX_ID)))
554 if (fate == HIT_BAD_P0) {
555 mprintf((0,"Warning: Bad p0 in physics! Object = %i, type = %i [%s]\n", obj-Objects, obj->type, Object_type_names[obj->type]));
560 if (obj->type == OBJ_PLAYER) {
563 if (n_phys_segs && phys_seglist[n_phys_segs-1]==hit_info.seglist[0])
566 for (i=0;(i<hit_info.n_segs) && (n_phys_segs<MAX_FVI_SEGS-1); )
567 phys_seglist[n_phys_segs++] = hit_info.seglist[i++];
571 if (obj == debug_obj)
572 printf(" fate = %d, hit_pnt = %8x %8x %8x\n",fate,XYZ(&hit_info.hit_pnt));;
575 ipos = hit_info.hit_pnt;
576 iseg = hit_info.hit_seg;
577 WallHitSide = hit_info.hit_side;
578 WallHitSeg = hit_info.hit_side_seg;
580 if (iseg==-1) { //some sort of horrible error
582 mprintf((1,"iseg==-1 in physics! Object = %i, type = %i (%s)\n", obj-Objects, obj->type, Object_type_names[obj->type]));
585 //compute_segment_center(&ipos,&Segments[obj->segnum]);
587 //iseg = obj->segnum;
589 if (obj->type == OBJ_WEAPON)
590 obj->flags |= OF_SHOULD_BE_DEAD;
594 Assert(!((fate==HIT_WALL) && ((WallHitSeg == -1) || (WallHitSeg > Highest_segment_index))));
596 //if(!get_seg_masks(&hit_info.hit_pnt,hit_info.hit_seg,0).centermask==0)
599 save_pos = obj->pos; //save the object's position
600 save_seg = obj->segnum;
602 // update object's position and segment number
606 if (obj == debug_obj)
607 printf(" new pos = %x %x %x\n",XYZ(&obj->pos));
610 if ( iseg != obj->segnum )
611 obj_relink(objnum, iseg );
613 //if start point not in segment, move object to center of segment
614 if (get_seg_masks(&obj->pos, obj->segnum, 0, __FILE__, __LINE__).centermask !=0 )
618 if ((n=find_object_seg(obj))==-1) {
620 if (obj->type==OBJ_PLAYER && (n=find_point_seg(&obj->last_pos,obj->segnum))!=-1) {
621 obj->pos = obj->last_pos;
622 obj_relink(objnum, n );
625 compute_segment_center(&obj->pos,&Segments[obj->segnum]);
626 obj->pos.x += objnum;
628 if (obj->type == OBJ_WEAPON)
629 obj->flags |= OF_SHOULD_BE_DEAD;
634 //calulate new sim time
636 //vms_vector moved_vec;
637 vms_vector moved_vec_n;
638 fix attempted_dist,actual_dist;
640 old_sim_time = sim_time;
642 actual_dist = vm_vec_normalized_dir(&moved_vec_n,&obj->pos,&save_pos);
644 if (fate==HIT_WALL && vm_vec_dot(&moved_vec_n,&frame_vec) < 0) { //moved backwards
646 //don't change position or sim_time
648 //******* mprintf((0,"Obj %d moved backwards\n",obj-Objects));
651 if (obj == debug_obj)
652 printf(" Warning: moved backwards!\n");
657 //iseg = obj->segnum; //don't change segment
659 obj_relink(objnum, save_seg );
665 //if (obj == debug_obj)
666 // printf(" moved_vec = %x %x %x\n",XYZ(&moved_vec));
668 attempted_dist = vm_vec_mag(&frame_vec);
670 sim_time = fixmuldiv(sim_time,attempted_dist-actual_dist,attempted_dist);
672 moved_time = old_sim_time - sim_time;
674 if (sim_time < 0 || sim_time>old_sim_time) {
676 mprintf((0,"Bogus sim_time = %x, old = %x\n",sim_time,old_sim_time));
677 if (obj == debug_obj)
678 printf(" Bogus sim_time = %x, old = %x, attempted_dist = %x, actual_dist = %x\n",sim_time,old_sim_time,attempted_dist,actual_dist);
679 //Int3(); Removed by Rob
681 sim_time = old_sim_time;
682 //WHY DOES THIS HAPPEN??
689 if (obj == debug_obj)
690 printf(" new sim_time = %x\n",sim_time);
700 //@@fix total_d,moved_d;
701 fix hit_speed,wall_part;
705 vm_vec_sub(&moved_v,&obj->pos,&save_pos);
707 wall_part = vm_vec_dot(&moved_v,&hit_info.hit_wallnorm);
709 if (wall_part != 0 && moved_time>0 && (hit_speed=-fixdiv(wall_part,moved_time))>0)
710 collide_object_with_wall( obj, hit_speed, WallHitSeg, WallHitSide, &hit_info.hit_pnt );
712 scrape_object_on_wall(obj, WallHitSeg, WallHitSide, &hit_info.hit_pnt );
714 Assert( WallHitSeg > -1 );
715 Assert( WallHitSide > -1 );
717 if ( !(obj->flags&OF_SHOULD_BE_DEAD) ) {
718 int forcefield_bounce; //bounce off a forcefield
720 Assert(BounceCheat || !(obj->mtype.phys_info.flags & PF_STICK && obj->mtype.phys_info.flags & PF_BOUNCE)); //can't be bounce and stick
722 forcefield_bounce = (TmapInfo[Segments[WallHitSeg].sides[WallHitSide].tmap_num].flags & TMI_FORCE_FIELD);
724 if (!forcefield_bounce && (obj->mtype.phys_info.flags & PF_STICK)) { //stop moving
726 // mprintf((0, "Object %i stuck at %i:%i\n", obj-Objects, WallHitSeg, WallHitSide));
727 add_stuck_object(obj, WallHitSeg, WallHitSide);
729 vm_vec_zero(&obj->mtype.phys_info.velocity);
733 else { // Slide object along wall
736 //We're constrained by wall, so subtract wall part from
739 wall_part = vm_vec_dot(&hit_info.hit_wallnorm,&obj->mtype.phys_info.velocity);
741 // mprintf((0, "%d", f2i(vm_vec_mag(&hit_info.hit_wallnorm)) ));
743 if (forcefield_bounce || (obj->mtype.phys_info.flags & PF_BOUNCE)) { //bounce off wall
744 wall_part *= 2; //Subtract out wall part twice to achieve bounce
746 if (forcefield_bounce) {
747 check_vel = 1; //check for max velocity
748 if (obj->type == OBJ_PLAYER)
749 wall_part *= 2; //player bounce twice as much
752 if ( obj->mtype.phys_info.flags & PF_BOUNCES_TWICE) {
753 Assert(obj->mtype.phys_info.flags & PF_BOUNCE);
754 if (obj->mtype.phys_info.flags & PF_BOUNCED_ONCE)
755 obj->mtype.phys_info.flags &= ~(PF_BOUNCE+PF_BOUNCED_ONCE+PF_BOUNCES_TWICE);
757 obj->mtype.phys_info.flags |= PF_BOUNCED_ONCE;
760 bounced = 1; //this object bounced
763 vm_vec_scale_add2(&obj->mtype.phys_info.velocity,&hit_info.hit_wallnorm,-wall_part);
765 // mprintf((0, "Velocity at bounce time = %d\n", f2i(vm_vec_mag(&obj->mtype.phys_info.velocity))));
767 //if (obj==ConsoleObject)
768 // mprintf((0,"player vel = %x\n",vm_vec_mag_quick(&obj->mtype.phys_info.velocity)));
771 fix vel = vm_vec_mag_quick(&obj->mtype.phys_info.velocity);
773 if (vel > MAX_OBJECT_VEL)
774 vm_vec_scale(&obj->mtype.phys_info.velocity,fixdiv(MAX_OBJECT_VEL,vel));
777 if (bounced && obj->type == OBJ_WEAPON)
778 vm_vector_2_matrix(&obj->orient,&obj->mtype.phys_info.velocity,&obj->orient.uvec,NULL);
781 if (obj == debug_obj) {
782 printf(" sliding - wall_norm %x %x %x %x\n",wall_part,XYZ(&hit_info.hit_wallnorm));
783 printf(" wall_part %x, new velocity = %x %x %x\n",wall_part,XYZ(&obj->mtype.phys_info.velocity));
796 // Mark the hit object so that on a retry the fvi code
797 // ignores this object.
799 Assert(hit_info.hit_object != -1);
801 // Calculcate the hit point between the two objects.
802 { vms_vector *ppos0, *ppos1, pos_hit;
804 ppos0 = &Objects[hit_info.hit_object].pos;
806 size0 = Objects[hit_info.hit_object].size;
808 Assert(size0+size1 != 0); // Error, both sizes are 0, so how did they collide, anyway?!?
809 //vm_vec_scale(vm_vec_sub(&pos_hit, ppos1, ppos0), fixdiv(size0, size0 + size1));
810 //vm_vec_add2(&pos_hit, ppos0);
811 vm_vec_sub(&pos_hit, ppos1, ppos0);
812 vm_vec_scale_add(&pos_hit,ppos0,&pos_hit,fixdiv(size0, size0 + size1));
814 old_vel = obj->mtype.phys_info.velocity;
816 collide_two_objects( obj, &Objects[hit_info.hit_object], &pos_hit);
820 // Let object continue its movement
821 if ( !(obj->flags&OF_SHOULD_BE_DEAD) ) {
822 //obj->pos = save_pos;
824 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)) {
825 //if (Objects[hit_info.hit_object].type == OBJ_POWERUP)
826 ignore_obj_list[n_ignore_objs++] = hit_info.hit_object;
835 if (TactileStick && obj==ConsoleObject && !(FrameCount & 15))
836 Tactile_Xvibrate_clear ();
842 Int3(); // Unexpected collision type: start point not in specified segment.
843 mprintf((0,"Warning: Bad p0 in physics!!!\n"));
846 // Unknown collision type returned from find_vector_intersection!!
852 } while ( try_again );
854 // Pass retry count info to AI.
855 if (obj->control_type == CT_AI) {
857 Ai_local_info[objnum].retry_count = count-1;
859 Total_retries += count-1;
865 //I'm not sure why we do this. I wish there were a comment that
866 //explained it. I think maybe it only needs to be done if the object
867 //is sliding, but I don't know
868 if (!obj_stopped && !bounced) { //Set velocity from actual movement
869 vms_vector moved_vec;
871 vm_vec_sub(&moved_vec,&obj->pos,&start_pos);
872 vm_vec_copy_scale(&obj->mtype.phys_info.velocity,&moved_vec,fixdiv(f1_0,FrameTime));
875 if (obj==ConsoleObject && (obj->mtype.phys_info.velocity.x==0 && obj->mtype.phys_info.velocity.y==0 && obj->mtype.phys_info.velocity.z==0) &&
876 !(obj->mtype.phys_info.thrust.x==0 && obj->mtype.phys_info.thrust.y==0 && obj->mtype.phys_info.thrust.z==0)) {
877 vms_vector center,bump_vec;
879 //bump player a little towards center of segment to unstick
881 compute_segment_center(¢er,&Segments[obj->segnum]);
882 vm_vec_normalized_dir_quick(&bump_vec,¢er,&obj->pos);
884 //don't bump player toward center of reactor segment
885 if (Segment2s[obj->segnum].special == SEGMENT_IS_CONTROLCEN)
886 vm_vec_negate(&bump_vec);
888 vm_vec_scale_add2(&obj->pos,&bump_vec,obj->size/5);
890 //if moving away from seg, might move out of seg, so update
891 if (Segment2s[obj->segnum].special == SEGMENT_IS_CONTROLCEN)
892 update_object_seg(obj);
897 //Assert(check_point_in_seg(&obj->pos,obj->segnum,0).centermask==0);
899 //if (obj->control_type == CT_FLYING)
900 if (obj->mtype.phys_info.flags & PF_LEVELLING)
901 do_physics_align_object( obj );
904 //hack to keep player from going through closed doors
905 if (obj->type==OBJ_PLAYER && obj->segnum!=orig_segnum && (Physics_cheat_flag!=0xBADA55) ) {
908 sidenum = find_connect_side(&Segments[obj->segnum],&Segments[orig_segnum]);
912 if (! (WALL_IS_DOORWAY(&Segments[orig_segnum],sidenum) & WID_FLY_FLAG)) {
914 int vertnum,num_faces,i;
920 s = &Segments[orig_segnum].sides[sidenum];
923 Error("orig_segnum == -1 in physics");
925 create_abs_vertex_lists(&num_faces, vertex_list, orig_segnum, sidenum, __FILE__, __LINE__);
927 //let's pretend this wall is not triangulated
928 vertnum = vertex_list[0];
930 if (vertex_list[i] < vertnum)
931 vertnum = vertex_list[i];
936 get_side_normal(&Segments[orig_segnum], sidenum, 0, &_vn );
937 dist = vm_dist_to_plane(&start_pos, &_vn, &Vertices[vertnum]);
938 vm_vec_scale_add(&obj->pos,&start_pos,&_vn,obj->size-dist);
941 dist = vm_dist_to_plane(&start_pos, &s->normals[0], &Vertices[vertnum]);
942 vm_vec_scale_add(&obj->pos,&start_pos,&s->normals[0],obj->size-dist);
944 update_object_seg(obj);
950 //--WE ALWYS WANT THIS IN, MATT AND MIKE DECISION ON 12/10/94, TWO MONTHS AFTER FINAL #ifndef NDEBUG
951 //if end point not in segment, move object to last pos, or segment center
952 if (get_seg_masks(&obj->pos, obj->segnum, 0, __FILE__, __LINE__).centermask != 0)
954 if (find_object_seg(obj)==-1) {
958 if (obj->type==OBJ_PLAYER && (n=find_point_seg(&obj->last_pos,obj->segnum))!=-1) {
959 obj->pos = obj->last_pos;
960 obj_relink(objnum, n );
963 compute_segment_center(&obj->pos,&Segments[obj->segnum]);
964 obj->pos.x += objnum;
966 if (obj->type == OBJ_WEAPON)
967 obj->flags |= OF_SHOULD_BE_DEAD;
970 //--WE ALWYS WANT THIS IN, MATT AND MIKE DECISION ON 12/10/94, TWO MONTHS AFTER FINAL #endif
975 //--unused-- //tell us what the given object will do (as far as hiting walls) in
976 //--unused-- //the given time (in seconds) t. Igores acceleration (sorry)
977 //--unused-- //if check_objects is set, check with objects, else just with walls
978 //--unused-- //returns fate, fills in hit time. If fate==HIT_NONE, hit_time undefined
979 //--unused-- int physics_lookahead(object *obj,fix t,int fvi_flags,fix *hit_time, fvi_info *hit_info)
981 //--unused-- vms_vector new_pos;
982 //--unused-- int objnum,fate;
983 //--unused-- fvi_query fq;
985 //--unused-- Assert(obj->movement_type == MT_PHYSICS);
987 //--unused-- objnum = obj-Objects;
989 //--unused-- vm_vec_scale_add(&new_pos, &obj->pos, &obj->mtype.phys_info.velocity, t);
991 //--unused-- fq.p0 = &obj->pos;
992 //--unused-- fq.startseg = obj->segnum;
993 //--unused-- fq.p1 = &new_pos;
994 //--unused-- fq.rad = obj->size;
995 //--unused-- fq.thisobjnum = objnum;
996 //--unused-- fq.ignore_obj_list = NULL;
997 //--unused-- fq.flags = fvi_flags;
999 //--unused-- fate = find_vector_intersection(&fq,hit_info);
1001 //--unused-- if (fate != HIT_NONE) {
1002 //--unused-- fix dist,speed;
1004 //--unused-- dist = vm_vec_dist(&obj->pos, &hit_info->hit_pnt);
1006 //--unused-- speed = vm_vec_mag(&obj->mtype.phys_info.velocity);
1008 //--unused-- *hit_time = fixdiv(dist,speed);
1012 //--unused-- return fate;
1016 //Applies an instantaneous force on an object, resulting in an instantaneous
1017 //change in velocity.
1018 void phys_apply_force(object *obj,vms_vector *force_vec)
1021 // Put in by MK on 2/13/96 for force getting applied to Omega blobs, which have 0 mass,
1022 // in collision with crazy reactor robot thing on d2levf-s.
1023 if (obj->mtype.phys_info.mass == 0)
1026 if (obj->movement_type != MT_PHYSICS)
1030 if (TactileStick && obj==&Objects[Players[Player_num].objnum])
1031 Tactile_apply_force (force_vec,&obj->orient);
1034 //Add in acceleration due to force
1035 vm_vec_scale_add2(&obj->mtype.phys_info.velocity,force_vec,fixdiv(f1_0,obj->mtype.phys_info.mass));
1040 // ----------------------------------------------------------------
1041 // Do *dest = *delta unless:
1042 // *delta is pretty small
1043 // and they are of different signs.
1044 void physics_set_rotvel_and_saturate(fix *dest, fix delta)
1046 if ((delta ^ *dest) < 0) {
1047 if (abs(delta) < F1_0/8) {
1048 // mprintf((0, "D"));
1051 // mprintf((0, "d"));
1054 // mprintf((0, "!"));
1059 // ------------------------------------------------------------------------------------------------------
1060 // Note: This is the old ai_turn_towards_vector code.
1061 // phys_apply_rot used to call ai_turn_towards_vector until I fixed it, which broke phys_apply_rot.
1062 void physics_turn_towards_vector(vms_vector *goal_vector, object *obj, fix rate)
1064 vms_angvec dest_angles, cur_angles;
1065 fix delta_p, delta_h;
1066 vms_vector *rotvel_ptr = &obj->mtype.phys_info.rotvel;
1068 // Make this object turn towards the goal_vector. Changes orientation, doesn't change direction of movement.
1069 // If no one moves, will be facing goal_vector in 1 second.
1071 // Detect null vector.
1072 if ((goal_vector->x == 0) && (goal_vector->y == 0) && (goal_vector->z == 0))
1075 // Make morph objects turn more slowly.
1076 if (obj->control_type == CT_MORPH)
1079 vm_extract_angles_vector(&dest_angles, goal_vector);
1080 vm_extract_angles_vector(&cur_angles, &obj->orient.fvec);
1082 delta_p = (dest_angles.p - cur_angles.p);
1083 delta_h = (dest_angles.h - cur_angles.h);
1085 if (delta_p > F1_0/2) delta_p = dest_angles.p - cur_angles.p - F1_0;
1086 if (delta_p < -F1_0/2) delta_p = dest_angles.p - cur_angles.p + F1_0;
1087 if (delta_h > F1_0/2) delta_h = dest_angles.h - cur_angles.h - F1_0;
1088 if (delta_h < -F1_0/2) delta_h = dest_angles.h - cur_angles.h + F1_0;
1090 delta_p = fixdiv(delta_p, rate);
1091 delta_h = fixdiv(delta_h, rate);
1093 if (abs(delta_p) < F1_0/16) delta_p *= 4;
1094 if (abs(delta_h) < F1_0/16) delta_h *= 4;
1096 physics_set_rotvel_and_saturate(&rotvel_ptr->x, delta_p);
1097 physics_set_rotvel_and_saturate(&rotvel_ptr->y, delta_h);
1101 // -----------------------------------------------------------------------------
1102 // Applies an instantaneous whack on an object, resulting in an instantaneous
1103 // change in orientation.
1104 void phys_apply_rot(object *obj,vms_vector *force_vec)
1108 if (obj->movement_type != MT_PHYSICS)
1111 vecmag = vm_vec_mag(force_vec)/8;
1112 if (vecmag < F1_0/256)
1114 else if (vecmag < obj->mtype.phys_info.mass >> 14)
1117 rate = fixdiv(obj->mtype.phys_info.mass, vecmag);
1118 if (obj->type == OBJ_ROBOT) {
1121 // Changed by mk, 10/24/95, claw guys should not slow down when attacking!
1122 if (!Robot_info[obj->id].thief && !Robot_info[obj->id].attack_type) {
1123 if (obj->ctype.ai_info.SKIP_AI_COUNT * FrameTime < 3*F1_0/4) {
1124 fix tval = fixdiv(F1_0, 8*FrameTime);
1129 if ( (d_rand() * 2) < (tval & 0xffff))
1131 obj->ctype.ai_info.SKIP_AI_COUNT += addval;
1132 // -- mk: too much stuff making hard to see my debug messages...mprintf((0, "FrameTime = %7.3f, addval = %i\n", f2fl(FrameTime), addval));
1141 // Turn amount inversely proportional to mass. Third parameter is seconds to do 360 turn.
1142 physics_turn_towards_vector(force_vec, obj, rate);
1148 //this routine will set the thrust for an object to a value that will
1149 //(hopefully) maintain the object's current velocity
1150 void set_thrust_from_velocity(object *obj)
1154 Assert(obj->movement_type == MT_PHYSICS);
1156 k = fixmuldiv(obj->mtype.phys_info.mass,obj->mtype.phys_info.drag,(f1_0-obj->mtype.phys_info.drag));
1158 vm_vec_copy_scale(&obj->mtype.phys_info.thrust,&obj->mtype.phys_info.velocity,k);