2 void SUB_NullThink(entity this) { }
4 void SUB_CalcMoveDone(entity this);
5 void SUB_CalcAngleMoveDone(entity this);
11 Applies some friction to this
15 void SUB_Friction (entity this)
17 this.nextthink = time;
19 this.velocity = this.velocity * (1 - frametime * this.friction);
26 Makes client invisible or removes non-client
29 void SUB_VanishOrRemove (entity ent)
48 void SUB_SetFade_Think (entity this)
52 setthink(this, SUB_SetFade_Think);
53 this.nextthink = time;
54 this.alpha -= frametime * this.fade_rate;
55 if (this.alpha < 0.01)
56 SUB_VanishOrRemove(this);
58 this.nextthink = time;
65 Fade 'ent' out when time >= 'when'
68 void SUB_SetFade (entity ent, float when, float fading_time)
70 ent.fade_rate = 1/fading_time;
71 setthink(ent, SUB_SetFade_Think);
79 calculate this.velocity and this.nextthink to reach dest from
80 this.origin traveling at speed
83 void SUB_CalcMoveDone(entity this)
85 // After moving, set origin to exact final destination
87 setorigin (this, this.finaldest);
88 this.velocity = '0 0 0';
90 if (this.think1 && this.think1 != SUB_CalcMoveDone)
94 .float platmovetype_turn;
95 void SUB_CalcMove_controller_think (entity this)
105 delta = this.destvec;
106 delta2 = this.destvec2;
107 if(time < this.animstate_endtime)
109 nexttick = time + PHYS_INPUT_FRAMETIME;
111 traveltime = this.animstate_endtime - this.animstate_starttime;
112 phasepos = (nexttick - this.animstate_starttime) / traveltime; // range: [0, 1]
113 phasepos = cubic_speedfunc(this.platmovetype_start, this.platmovetype_end, phasepos);
114 nextpos = this.origin + (delta * phasepos) + (delta2 * phasepos * phasepos);
115 // derivative: delta + 2 * delta2 * phasepos (e.g. for angle positioning)
117 if(this.owner.platmovetype_turn)
120 destangle = delta + 2 * delta2 * phasepos;
121 destangle = vectoangles(destangle);
122 destangle_x = -destangle_x; // flip up / down orientation
124 // take the shortest distance for the angles
125 vector v = this.owner.angles;
126 v.x -= 360 * floor((v.x - destangle_x) / 360 + 0.5);
127 v.y -= 360 * floor((v.y - destangle_y) / 360 + 0.5);
128 v.z -= 360 * floor((v.z - destangle_z) / 360 + 0.5);
129 this.owner.angles = v;
130 angloc = destangle - this.owner.angles;
131 angloc = angloc * (1 / PHYS_INPUT_FRAMETIME); // so it arrives for the next frame
132 this.owner.avelocity = angloc;
134 if(nexttick < this.animstate_endtime)
135 veloc = nextpos - this.owner.origin;
137 veloc = this.finaldest - this.owner.origin;
138 veloc = veloc * (1 / PHYS_INPUT_FRAMETIME); // so it arrives for the next frame
140 this.owner.velocity = veloc;
141 this.nextthink = nexttick;
145 // derivative: delta + 2 * delta2 (e.g. for angle positioning)
146 entity own = this.owner;
147 setthink(own, this.think1);
153 void SUB_CalcMove_controller_setbezier (entity controller, vector org, vector control, vector destin)
155 // 0 * (1-t) * (1-t) + 2 * control * t * (1-t) + destin * t * t
156 // 2 * control * t - 2 * control * t * t + destin * t * t
157 // 2 * control * t + (destin - 2 * control) * t * t
159 setorigin(controller, org);
163 controller.destvec = 2 * control; // control point
164 controller.destvec2 = destin - 2 * control; // quadratic part required to reach end point
165 // also: initial d/dphasepos origin = 2 * control, final speed = 2 * (destin - control)
168 void SUB_CalcMove_controller_setlinear (entity controller, vector org, vector destin)
170 // 0 * (1-t) * (1-t) + 2 * control * t * (1-t) + destin * t * t
171 // 2 * control * t - 2 * control * t * t + destin * t * t
172 // 2 * control * t + (destin - 2 * control) * t * t
174 setorigin(controller, org);
177 controller.destvec = destin; // end point
178 controller.destvec2 = '0 0 0';
181 float TSPEED_TIME = -1;
182 float TSPEED_LINEAR = 0;
183 float TSPEED_START = 1;
184 float TSPEED_END = 2;
187 void SUB_CalcMove_Bezier (entity this, vector tcontrol, vector tdest, float tspeedtype, float tspeed, void(entity this) func)
193 objerror (this, "No speed is defined!");
196 this.finaldest = tdest;
197 setthink(this, SUB_CalcMoveDone);
203 traveltime = 2 * vlen(tcontrol - this.origin) / tspeed;
206 traveltime = 2 * vlen(tcontrol - tdest) / tspeed;
209 traveltime = vlen(tdest - this.origin) / tspeed;
216 if (traveltime < 0.1) // useless anim
218 this.velocity = '0 0 0';
219 this.nextthink = this.ltime + 0.1;
223 controller = new(SUB_CalcMove_controller);
224 controller.owner = this;
225 controller.platmovetype = this.platmovetype;
226 controller.platmovetype_start = this.platmovetype_start;
227 controller.platmovetype_end = this.platmovetype_end;
228 SUB_CalcMove_controller_setbezier(controller, this.origin, tcontrol, tdest);
229 controller.finaldest = (tdest + '0 0 0.125'); // where do we want to end? Offset to overshoot a bit.
230 controller.animstate_starttime = time;
231 controller.animstate_endtime = time + traveltime;
232 setthink(controller, SUB_CalcMove_controller_think);
233 controller.think1 = getthink(this);
235 // the thinking is now done by the controller
236 setthink(this, SUB_NullThink); // for PushMove
237 this.nextthink = this.ltime + traveltime;
240 getthink(controller)(controller);
243 void SUB_CalcMove (entity this, vector tdest, float tspeedtype, float tspeed, void(entity this) func)
249 objerror (this, "No speed is defined!");
252 this.finaldest = tdest;
253 setthink(this, SUB_CalcMoveDone);
255 if (tdest == this.origin)
257 this.velocity = '0 0 0';
258 this.nextthink = this.ltime + 0.1;
262 delta = tdest - this.origin;
270 traveltime = vlen (delta) / tspeed;
277 // Very short animations don't really show off the effect
278 // of controlled animation, so let's just use linear movement.
279 // Alternatively entities can choose to specify non-controlled movement.
280 // The only currently implemented alternative movement is linear (value 1)
281 if (traveltime < 0.15 || (this.platmovetype_start == 1 && this.platmovetype_end == 1)) // is this correct?
283 this.velocity = delta * (1/traveltime); // QuakeC doesn't allow vector/float division
284 this.nextthink = this.ltime + traveltime;
288 // now just run like a bezier curve...
289 SUB_CalcMove_Bezier(this, (this.origin + tdest) * 0.5, tdest, tspeedtype, tspeed, func);
292 void SUB_CalcMoveEnt (entity ent, vector tdest, float tspeedtype, float tspeed, void(entity this) func)
294 SUB_CalcMove(ent, tdest, tspeedtype, tspeed, func);
301 calculate this.avelocity and this.nextthink to reach destangle from
304 The calling function should make sure this.setthink is valid
307 void SUB_CalcAngleMoveDone(entity this)
309 // After rotating, set angle to exact final angle
310 this.angles = this.finalangle;
311 this.avelocity = '0 0 0';
313 if (this.think1 && this.think1 != SUB_CalcAngleMoveDone) // avoid endless loops
317 // FIXME: I fixed this function only for rotation around the main axes
318 void SUB_CalcAngleMove (entity this, vector destangle, float tspeedtype, float tspeed, void(entity this) func)
321 objerror (this, "No speed is defined!");
323 // take the shortest distance for the angles
324 this.angles_x -= 360 * floor((this.angles_x - destangle_x) / 360 + 0.5);
325 this.angles_y -= 360 * floor((this.angles_y - destangle_y) / 360 + 0.5);
326 this.angles_z -= 360 * floor((this.angles_z - destangle_z) / 360 + 0.5);
327 vector delta = destangle - this.angles;
336 traveltime = vlen (delta) / tspeed;
344 this.finalangle = destangle;
345 setthink(this, SUB_CalcAngleMoveDone);
347 if (traveltime < 0.1)
349 this.avelocity = '0 0 0';
350 this.nextthink = this.ltime + 0.1;
354 this.avelocity = delta * (1 / traveltime);
355 this.nextthink = this.ltime + traveltime;
358 void SUB_CalcAngleMoveEnt (entity ent, vector destangle, float tspeedtype, float tspeed, void(entity this) func)
360 SUB_CalcAngleMove (ent, destangle, tspeedtype, tspeed, func);