]> icculus.org git repositories - divverent/nexuiz.git/blob - data/qcsrc/server/movelib.qc
pathlib changed to use with AStar.
[divverent/nexuiz.git] / data / qcsrc / server / movelib.qc
1 /**
2     Simulate drag
3     self.velocity = movelib_vdrag(self.velocity,0.02,0.5);
4 **/
5 vector movelib_dragvec(float drag, float exp)
6 {
7     float lspeed,ldrag;
8
9     lspeed = vlen(self.velocity);
10     ldrag = lspeed * drag;
11     ldrag = ldrag * (drag * exp);
12     ldrag = 1 - (ldrag / lspeed);
13
14     return self.velocity * ldrag;
15 }
16
17 /**
18     Simulate drag
19     self.velocity = movelib_vdrag(somespeed,0.01,0.7);
20 **/
21 float movelib_dragflt(float fspeed,float drag,float exp)
22 {
23     float ldrag;
24
25     ldrag = fspeed * drag;
26     ldrag = ldrag * ldrag * exp;
27     ldrag = 1 - (ldrag / fspeed);
28
29     return ldrag;
30 }
31
32 /**
33     Do a inertia simulation based on velocity.
34     Basicaly, this allows you to simulate loss of steering with higher speed.
35     self.velocity = movelib_inertia_fromspeed(self.velocity,newvel,1000,0.1,0.9);
36 **/
37 vector movelib_inertmove_byspeed(vector vel_new, float vel_max,float newmin,float oldmax)
38 {
39     float influense;
40
41     influense = vlen(self.velocity) * (1 / vel_max);
42
43     influense = bound(newmin,influense,oldmax);
44
45     return (vel_new * (1 - influense)) + (self.velocity * influense);
46 }
47
48 vector movelib_inertmove(vector new_vel,float new_bias)
49 {
50     return new_vel * new_bias + self.velocity * (1-new_bias);
51 }
52
53 .float  movelib_lastupdate;
54 void movelib_move(vector force,float max_velocity,float drag,float mass,float breakforce)
55 {
56     float deltatime;
57     float acceleration;
58     float mspeed;\r
59     vector breakvec;
60
61     deltatime = time - self.movelib_lastupdate;
62     if (deltatime > 0.15) deltatime = 0;
63     self.movelib_lastupdate = time;
64     if(!deltatime) return;
65
66     mspeed = vlen(self.velocity);
67
68     if(mass)
69         acceleration = vlen(force) / mass;
70     else
71         acceleration = vlen(force);
72
73     if(self.flags & FL_ONGROUND)
74     {
75         if(breakforce)
76         {\r
77             breakvec = (normalize(self.velocity) * (breakforce / mass) * deltatime);\r
78             self.velocity = self.velocity - breakvec;
79         }\r
80
81         self.velocity = self.velocity + force * (acceleration * deltatime);
82     }\r
83
84     if(drag)\r
85         self.velocity = movelib_dragvec(drag, 1);\r
86 \r
87     if(self.waterlevel > 1)\r
88     {\r
89         self.velocity = self.velocity + force * (acceleration * deltatime);\r
90         self.velocity = self.velocity + '0 0 0.05' * sv_gravity * deltatime;\r
91     }\r
92     else
93         self.velocity = self.velocity + '0 0 -1' * sv_gravity * deltatime;
94 \r
95     mspeed = vlen(self.velocity);
96
97     if(max_velocity)
98         if(mspeed > max_velocity)
99             self.velocity = normalize(self.velocity) * (mspeed - 50);//* max_velocity;
100 }\r
101 \r
102 /*\r
103 .float mass;\r
104 .float side_friction;\r
105 .float ground_friction;\r
106 .float air_friction;\r
107 .float water_friction;\r
108 .float buoyancy;\r
109 float movelib_deltatime;\r
110 \r
111 void movelib_startupdate()\r
112 {\r
113     movelib_deltatime = time - self.movelib_lastupdate;\r
114 \r
115     if (movelib_deltatime > 0.5)\r
116         movelib_deltatime = 0;\r
117 \r
118     self.movelib_lastupdate = time;\r
119 }\r
120
121 void movelib_update(vector dir,float force)\r
122 {\r
123     vector acceleration;\r
124     float old_speed;\r
125     float ffriction,v_z;\r
126 \r
127     vector breakvec;\r
128     vector old_dir;\r
129     vector ggravity;\r
130     vector old;\r
131 \r
132     if(!movelib_deltatime)\r
133         return;\r
134     v_z = self.velocity_z;\r
135     old_speed    = vlen(self.velocity);\r
136     old_dir      = normalize(self.velocity);\r
137 \r
138     //ggravity      =  (sv_gravity / self.mass) * '0 0 100';\r
139     acceleration =  (force / self.mass) * dir;\r
140     //acceleration -= old_dir * (old_speed / self.mass);\r
141     acceleration -= ggravity;\r
142 \r
143     if(self.waterlevel > 1)\r
144     {\r
145         ffriction = self.water_friction;\r
146         acceleration += self.buoyancy * '0 0 1';\r
147     }\r
148     else\r
149         if(self.flags & FL_ONGROUND)\r
150             ffriction = self.ground_friction;\r
151         else\r
152             ffriction = self.air_friction;\r
153 \r
154     acceleration *= ffriction;\r
155     //self.velocity = self.velocity * (ffriction * movelib_deltatime);\r
156     self.velocity += acceleration * movelib_deltatime;\r
157     self.velocity_z = v_z;\r
158 \r
159 }\r
160 */\r
161
162 void movelib_move_simple(vector newdir,float velo,float blendrate)
163 {
164     self.velocity = self.velocity * (1 - blendrate) + (newdir * blendrate) * velo;
165 }
166 void movelib_beak_simple(float force)\r
167 {\r
168     float mspeed;\r
169     vector mdir;\r
170 \r
171     mspeed = max(0,vlen(self.velocity) - force);\r
172     mdir   = normalize(self.velocity);\r
173     self.velocity = mdir * mspeed;\r
174 }\r
175 \r
176 \r
177 void movelib_groundalign4point(float spring_length,float spring_up)\r
178 {\r
179     vector a,b,c,d,e,r,push_angle;\r
180     push_angle_y = 0;\r
181     r = (self.absmax + self.absmin) * 0.5 + (v_up * spring_up);\r
182     e = v_up * spring_length;\r
183 \r
184     a = r + (v_forward * self.maxs_x) + (v_right * self.maxs_y);\r
185     b = r + (v_forward * self.maxs_x) - (v_right * self.maxs_y);\r
186     c = r - (v_forward * self.maxs_x) + (v_right * self.maxs_y);\r
187     d = r - (v_forward * self.maxs_x) - (v_right * self.maxs_y);\r
188 \r
189     traceline(a, a - e,MOVE_NORMAL,self);\r
190     a_z =  (1 - trace_fraction);\r
191     r = trace_endpos;\r
192     traceline(b, b - e,MOVE_NORMAL,self);\r
193     b_z =  (1 - trace_fraction);\r
194     r += trace_endpos;\r
195     traceline(c, c - e,MOVE_NORMAL,self);\r
196     c_z =  (1 - trace_fraction);\r
197     r += trace_endpos;\r
198     traceline(d, d - e,MOVE_NORMAL,self);\r
199     d_z =  (1 - trace_fraction);\r
200     r += trace_endpos;\r
201     a_x = r_z;\r
202     r = self.origin;\r
203     r_z = r_z;\r
204 \r
205     push_angle_x = (a_z - c_z) * 45;\r
206     push_angle_x += (b_z - d_z) * 45;\r
207 \r
208     push_angle_z = (b_z - a_z) * 45;\r
209     push_angle_z += (d_z - c_z) * 45;\r
210 \r
211     self.angles_x += push_angle_x * 0.95;\r
212     self.angles_z += push_angle_z * 0.95;\r
213 \r
214     setorigin(self,r);\r
215 }\r
216 \r