chase.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /*
  2. Copyright (C) 1996-1997 Id Software, Inc.
  3. This program is free software; you can redistribute it and/or
  4. modify it under the terms of the GNU General Public License
  5. as published by the Free Software Foundation; either version 2
  6. of the License, or (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  10. See the GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program; if not, write to the Free Software
  13. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  14. */
  15. // chase.c -- chase camera code
  16. #include "quakedef.h"
  17. cvar_t chase_back = {"chase_back", "100"};
  18. cvar_t chase_up = {"chase_up", "16"};
  19. cvar_t chase_right = {"chase_right", "0"};
  20. cvar_t chase_active = {"chase_active", "0"};
  21. vec3_t chase_pos;
  22. vec3_t chase_angles;
  23. vec3_t chase_dest;
  24. vec3_t chase_dest_angles;
  25. void Chase_Init (void)
  26. {
  27. Cvar_RegisterVariable (&chase_back);
  28. Cvar_RegisterVariable (&chase_up);
  29. Cvar_RegisterVariable (&chase_right);
  30. Cvar_RegisterVariable (&chase_active);
  31. }
  32. void Chase_Reset (void)
  33. {
  34. // for respawning and teleporting
  35. // start position 12 units behind head
  36. }
  37. void TraceLine (vec3_t start, vec3_t end, vec3_t impact)
  38. {
  39. trace_t trace;
  40. memset (&trace, 0, sizeof(trace));
  41. SV_RecursiveHullCheck (cl.worldmodel->hulls, 0, 0, 1, start, end, &trace);
  42. VectorCopy (trace.endpos, impact);
  43. }
  44. void Chase_Update (void)
  45. {
  46. int i;
  47. float dist;
  48. vec3_t forward, up, right;
  49. vec3_t dest, stop;
  50. // if can't see player, reset
  51. AngleVectors (cl.viewangles, forward, right, up);
  52. // calc exact destination
  53. for (i=0 ; i<3 ; i++)
  54. chase_dest[i] = r_refdef.vieworg[i]
  55. - forward[i]*chase_back.value
  56. - right[i]*chase_right.value;
  57. chase_dest[2] = r_refdef.vieworg[2] + chase_up.value;
  58. // find the spot the player is looking at
  59. VectorMA (r_refdef.vieworg, 4096, forward, dest);
  60. TraceLine (r_refdef.vieworg, dest, stop);
  61. // calculate pitch to look at the same spot from camera
  62. VectorSubtract (stop, r_refdef.vieworg, stop);
  63. dist = DotProduct (stop, forward);
  64. if (dist < 1)
  65. dist = 1;
  66. r_refdef.viewangles[PITCH] = -atan(stop[2] / dist) / M_PI * 180;
  67. // move towards destination
  68. VectorCopy (chase_dest, r_refdef.vieworg);
  69. }