ao-tools: Add ao-fakeflight
[fw/altos] / ao-tools / ao-fakeflight / ao-physics.c
1 /*
2  * Copyright © 2014 Keith Packard <keithp@keithp.com>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; version 2 of the License.
7  *
8  * This program is distributed in the hope that it will be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11  * General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License along
14  * with this program; if not, write to the Free Software Foundation, Inc.,
15  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
16  */
17
18 #include "ao-fakeflight.h"
19
20 /* Density of dry air in kg/m³ for a given pressure and temperature. */
21 double
22 density_air(double pressure,            /* Pa */
23             double temperature)         /* °C */
24 {
25         return pressure / (GAS_CONSTANT * (temperature + 273.15));
26 }
27
28 /* Area of a circle */
29 double
30 area_circle(double diameter)
31 {
32         double radius = diameter / 2;
33         return M_PI * radius * radius;
34 }
35
36 /* Force due to drag (N) */
37 double
38 force_drag(double       speed,          /* m/s */
39            double       rho,            /* kg/m³ */
40            double       cd,             /* unitless */
41            double       area)           /* m² */
42 {
43         return 0.5 * rho * (speed * speed) * cd * area;
44 }
45
46 /* Force due to gravity (N) */
47 double
48 force_gravity(double    mass,           /* kg */
49               double    altitude)       /* m */
50 {
51         double  distance = altitude + EARTH_RADIUS;
52         return GRAVITATIONAL_CONSTANT * EARTH_MASS * mass / (distance * distance);
53 }