OBJPHYS.C 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /*
  2. THE COMPUTER CODE CONTAINED HEREIN IS THE SOLE PROPERTY OF PARALLAX
  3. SOFTWARE CORPORATION ("PARALLAX"). PARALLAX, IN DISTRIBUTING THE CODE TO
  4. END-USERS, AND SUBJECT TO ALL OF THE TERMS AND CONDITIONS HEREIN, GRANTS A
  5. ROYALTY-FREE, PERPETUAL LICENSE TO SUCH END-USERS FOR USE BY SUCH END-USERS
  6. IN USING, DISPLAYING, AND CREATING DERIVATIVE WORKS THEREOF, SO LONG AS
  7. SUCH USE, DISPLAY OR CREATION IS FOR NON-COMMERCIAL, ROYALTY OR REVENUE
  8. FREE PURPOSES. IN NO EVENT SHALL THE END-USER USE THE COMPUTER CODE
  9. CONTAINED HEREIN FOR REVENUE-BEARING PURPOSES. THE END-USER UNDERSTANDS
  10. AND AGREES TO THE TERMS HEREIN AND ACCEPTS THE SAME BY USE OF THIS FILE.
  11. COPYRIGHT 1993-1998 PARALLAX SOFTWARE CORPORATION. ALL RIGHTS RESERVED.
  12. */
  13. void do_object_physics( object * obj )
  14. {
  15. vms_angvec rotang;
  16. vms_vector frame_vec; //movement in this frame
  17. vms_vector new_pos,ipos; //position after this frame
  18. int iseg;
  19. int hit;
  20. vms_matrix rotmat,new_pm;
  21. int count=0;
  22. short joy_x,joy_y,btns;
  23. int joyx_moved,joyy_moved;
  24. fix speed;
  25. vms_vector *desired_upvec;
  26. fixang delta_ang,roll_ang;
  27. vms_vector forvec = {0,0,f1_0};
  28. vms_matrix temp_matrix;
  29. //check keys
  30. rotang.pitch = ROT_SPEED * (key_down_time(KEY_UP) - key_down_time(KEY_DOWN));
  31. rotang.head = ROT_SPEED * (key_down_time(KEY_RIGHT) - key_down_time(KEY_LEFT));
  32. rotang.bank = 0;
  33. //check for joystick movement
  34. joy_get_pos(&joy_x,&joy_y);
  35. btns=joy_get_btns();
  36. joyx_moved = (abs(joy_x - _old_joy_x)>JOY_NULL);
  37. joyy_moved = (abs(joy_y - _old_joy_y)>JOY_NULL);
  38. if (abs(joy_x) < JOY_NULL) joy_x = 0;
  39. if (abs(joy_y) < JOY_NULL) joy_y = 0;
  40. if (!rotang.pitch) rotang.pitch = fixmul(-joy_y * 128,FrameTime);
  41. if (!rotang.head) rotang.head = fixmul(joy_x * 128,FrameTime);
  42. if (joyx_moved) _old_joy_x = joy_x;
  43. if (joyy_moved) _old_joy_y = joy_y;
  44. speed = ((btns&2) || keyd_pressed[KEY_A])?SLOW_SPEED*3:(keyd_pressed[KEY_Z]?SLOW_SPEED/2:SLOW_SPEED);
  45. //now build matrices, do rotations, etc., etc.
  46. vm_angles_2_matrix(&rotmat,&rotang);
  47. vm_matrix_x_matrix(&new_pm,&obj->orient,&rotmat);
  48. obj->orient = new_pm;
  49. //move player
  50. vm_vec_copy_scale(&obj->velocity,&obj->orient.fvec,speed);
  51. vm_vec_copy_scale(&frame_vec,&obj->velocity,FrameTime);
  52. do {
  53. fix wall_part;
  54. vms_vector tvec;
  55. count++;
  56. vm_vec_add(&new_pos,&obj->pos,&frame_vec);
  57. hit = find_vector_intersection(&ipos,&iseg,&obj->pos,obj->seg_id,&new_pos,obj->size,-1);
  58. obj->seg_id = iseg;
  59. obj->pos = ipos;
  60. //-FIXJOHN-if (hit==HIT_OBJECT) ExplodeObject(hit_objnum);
  61. if (hit==HIT_WALL) {
  62. vm_vec_sub(&frame_vec,&new_pos,&obj->pos); //part through wall
  63. wall_part = vm_vec_dot(wall_norm,&frame_vec);
  64. vm_vec_copy_scale(&tvec,wall_norm,wall_part);
  65. if ((wall_part == 0) || (vm_vec_mag(&tvec) < 5)) Int3();
  66. vm_vec_sub2(&frame_vec,&tvec);
  67. }
  68. } while (hit == HIT_WALL);
  69. Assert(check_point_in_seg(&obj->pos,obj->seg_id,0).centermask==0);
  70. //now bank player according to segment orientation
  71. desired_upvec = &Segments[obj->seg_id].sides[3].faces[0].normal;
  72. if (labs(vm_vec_dot(desired_upvec,&obj->orient.fvec)) < f1_0/2) {
  73. vm_vector_2_matrix(&temp_matrix,&obj->orient.fvec,desired_upvec,NULL);
  74. delta_ang = vm_vec_delta_ang(&obj->orient.uvec,&temp_matrix.uvec,&obj->orient.fvec);
  75. if (rotang.head) delta_ang += (rotang.head<0)?TURNROLL_ANG:-TURNROLL_ANG;
  76. if (abs(delta_ang) > DAMP_ANG) {
  77. roll_ang = fixmul(FrameTime,ROLL_RATE);
  78. if (abs(delta_ang) < roll_ang) roll_ang = delta_ang;
  79. else if (delta_ang<0) roll_ang = -roll_ang;
  80. vm_vec_ang_2_matrix(&rotmat,&forvec,roll_ang);
  81. vm_matrix_x_matrix(&new_pm,&obj->orient,&rotmat);
  82. obj->orient = new_pm;
  83. }
  84. }
  85. }
  86.