]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/common/explosion_equation.qc
new toy: autocvar_g_damagepush_speedfactor - ENJOY
[xonotic/xonotic-data.pk3dir.git] / qcsrc / common / explosion_equation.qc
1 vector explosion_calcpush_nomultiplier(vector explosion_v, vector target_v)
2 {
3         // solution of the equations:
4         //    v'                = v + alpha vp             // central hit
5         //    m*v'   + mp*vp'   = m*v + mp*vp              // conservation of momentum
6         //    m*v'^2 + mp*vp'^2 = m*v^2 + mp*vp^2          // conservation of energy (ELASTIC hit)
7         // -> alpha = 0                                    // case 1: did not hit
8         // -> alpha = 2*mp*(vp^2 - vp.v) / ((m+mp) * vp^2) // case 2: did hit
9         //                                                 // non-elastic hits are somewhere between these two
10
11         float alpha;
12         alpha  = explosion_v * (explosion_v - target_v);
13
14         if(alpha <= 0)
15                 // target is too fast to be hittable by this
16                 return '0 0 0';
17
18         alpha /= (explosion_v * explosion_v);
19                 // now alpha is a multiplier
20                 // we know we can divide by this, or above alpha would be == 0
21         
22         return
23                 explosion_v * alpha;
24 }
25
26 #if 0
27 vector explosion_calcpush(vector explosion_v, float explosion_m, vector target_v, float target_m, float elasticity)
28 {
29         // solution of the equations:
30         //    v'                = v + alpha vp             // central hit
31         //    m*v'   + mp*vp'   = m*v + mp*vp              // conservation of momentum
32         //    m*v'^2 + mp*vp'^2 = m*v^2 + mp*vp^2          // conservation of energy (ELASTIC hit)
33         // -> alpha = 0                                    // case 1: did not hit
34         // -> alpha = 2*mp*(vp^2 - vp.v) / ((m+mp) * vp^2) // case 2: did hit
35         //                                                 // non-elastic hits are somewhere between these two
36
37         // this would be physically correct, but we don't do that
38         return explosion_calcpush_nomultiplier(explosion_v, target_v, 
39                 (1 + elasticity) * (
40                         explosion_m
41                 ) / (
42                         target_m + explosion_m
43                 )
44         );
45 }
46 #endif
47
48 // simplified formula, tuned so that if the target has velocity 0, we get exactly the original force
49 vector damage_explosion_calcpush(vector explosion_f, vector target_v, float speedfactor)
50 {
51         if(speedfactor == 0)
52                 return explosion_f;
53         return explosion_calcpush_nomultiplier(explosion_f * speedfactor, target_v) * (1.0 / speedfactor);
54 }