git-svn-id: http://bladebattles.com/kurok/SVN@11 20cd92bb-ff49-0410-b73e-96a06e42c3b9
[kurok.git] / chase.c
blobd581b5f20492f006c4c48c17b31f26977f4e4622
1 /*
2 Copyright (C) 1996-1997 Id Software, Inc.
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License
6 as published by the Free Software Foundation; either version 2
7 of the License, or (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13 See the GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 // chase.c -- chase camera code
22 #include "quakedef.h"
24 cvar_t chase_back = {"chase_back", "100", true};
25 cvar_t chase_up = {"chase_up", "16", true};
26 cvar_t chase_right = {"chase_right", "0", true};
27 cvar_t chase_active = {"chase_active", "0", false};
29 vec3_t chase_pos;
30 vec3_t chase_angles;
32 vec3_t chase_dest;
33 vec3_t chase_dest_angles;
36 void Chase_Init (void)
38 Cvar_RegisterVariable (&chase_back);
39 Cvar_RegisterVariable (&chase_up);
40 Cvar_RegisterVariable (&chase_right);
41 Cvar_RegisterVariable (&chase_active);
44 void Chase_Reset (void)
46 // for respawning and teleporting
47 // start position 12 units behind head
50 void TraceLine (vec3_t start, vec3_t end, vec3_t impact)
52 trace_t trace;
54 memset (&trace, 0, sizeof(trace));
55 SV_RecursiveHullCheck (cl.worldmodel->hulls, 0, 0, 1, start, end, &trace);
57 VectorCopy (trace.endpos, impact);
60 void Chase_Update (void)
62 int i;
63 float dist;
64 vec3_t forward, up, right;
65 vec3_t dest, stop;
68 // if can't see player, reset
69 AngleVectors (cl.viewangles, forward, right, up);
71 // calc exact destination
72 for (i=0 ; i<3 ; i++)
73 chase_dest[i] = r_refdef.vieworg[i]
74 - forward[i]*chase_back.value
75 - right[i]*chase_right.value;
76 chase_dest[2] = r_refdef.vieworg[2] + chase_up.value;
78 // find the spot the player is looking at
79 VectorMA (r_refdef.vieworg, 4096, forward, dest);
80 TraceLine (r_refdef.vieworg, dest, stop);
82 // calculate pitch to look at the same spot from camera
83 VectorSubtract (stop, r_refdef.vieworg, stop);
84 dist = DotProduct (stop, forward);
85 if (dist < 1)
86 dist = 1;
87 r_refdef.viewangles[PITCH] = -atanf(stop[2] / dist) / M_PI * 180;
89 TraceLine(r_refdef.vieworg, chase_dest, stop);
91 if (Length(stop) != 0)
93 VectorCopy(stop, chase_dest);
96 // move towards destination
97 VectorCopy (chase_dest, r_refdef.vieworg);