]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/lib/p2mathlib.qc
Merge branch 'amade/small-fixes' into 'master'
[xonotic/xonotic-data.pk3dir.git] / qcsrc / lib / p2mathlib.qc
1 /*
2  Copyright (C) 2015 Micah Talkiewicz.
3
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.
8
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.
12
13  See the GNU General Public License for more details.
14
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.
18 */
19
20 vector vec_bias(vector v, float f)
21 {
22         vector c;
23         c.x = v.x + f;
24         c.y = v.y + f;
25         c.z = v.z + f;
26         return c;
27 }
28 vector vec_to_min(vector a, vector b)
29 {
30         vector c;
31         c.x = min(a.x, b.x);
32         c.y = min(a.y, b.y);
33         c.z = min(a.z, b.z);
34         return c;
35 }
36
37 vector vec_to_max(vector a, vector b)
38 {
39         vector c;
40         c.x = max(a.x, b.x);
41         c.y = max(a.y, b.y);
42         c.z = max(a.z, b.z);
43         return c;
44 }
45
46 // there may already be a function for bounding a vector in this manner, however my very quick search did not reveal one -- Player_2
47 vector vec_bounds_in(vector point, vector a, vector b)
48 {
49         vector d = vec_to_min(a, b);
50         vector e = vec_to_max(a, b);
51
52         vector c = vec_to_min(vec_to_max(point, d), e);
53
54         return c;
55 }
56
57 vector vec_bounds_out(vector point, vector a, vector b)
58 {
59         vector d = vec_to_max(a, b);
60         vector e = vec_to_min(a, b);
61
62         vector c = vec_to_min(vec_to_max(point, d), e);
63
64         return c;
65 }
66
67 float angle_snap_f(float f, float increment)
68 {
69         for (int j = 0; j <= 360; )
70         {
71                 if (f <= j - increment) return j - increment;
72                 j = j + increment;
73         }
74
75         return 0;
76 }
77
78 vector angle_snap_vec(vector v, float increment)
79 {
80         vector c;
81         c.x = angle_snap_f(v.x, increment);
82         c.y = angle_snap_f(v.y, increment);
83         c.z = angle_snap_f(v.z, increment);
84         return c;
85 }
86
87 vector aim_vec(vector org, vector targ)
88 {
89         vector v;
90         // we float around x and y, but rotate around z
91         v.x = targ.x - org.x;
92         v.y = targ.y - org.y;
93         v.z = org.z - targ.z;
94         // get the angles actual
95         return vectoangles(normalize(v));
96 }