Rigs of Rods 2023.09
Soft-body Physics Simulation
Loading...
Searching...
No Matches
Airfoil.cpp
Go to the documentation of this file.
1/*
2 This source file is part of Rigs of Rods
3 Copyright 2005-2012 Pierre-Michel Ricordel
4 Copyright 2007-2012 Thomas Fischer
5
6 For more information, see http://www.rigsofrods.org/
7
8 Rigs of Rods is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License version 3, as
10 published by the Free Software Foundation.
11
12 Rigs of Rods is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with Rigs of Rods. If not, see <http://www.gnu.org/licenses/>.
19*/
20
21#include "Airfoil.h"
22
23#include "Application.h"
24
25#include <Ogre.h>
26
27using namespace Ogre;
28using namespace RoR;
29
30Airfoil::Airfoil(Ogre::String const& fname)
31{
32 for (int i = 0; i < 3601; i++) //init in case of bad things
33 {
34 cl[i] = 0;
35 cd[i] = 0;
36 cm[i] = 0;
37 }
38 char line[1024];
39 //we load directly X-Plane AFL file format!!!
40 bool process = false;
41 bool neg = true;
42 int lastia = -1;
43 ResourceGroupManager& rgm = ResourceGroupManager::getSingleton();
44
45 String group = "";
46 try
47 {
48 group = rgm.findGroupContainingResource(fname);
49 }
50 catch (...)
51 {
52 }
53 if (group == "")
54 {
55 LOG(String("Airfoil error: could not load airfoil ")+fname);
56 return;
57 }
58
59 DataStreamPtr ds = rgm.openResource(fname, group);
60 while (!ds->eof())
61 {
62 size_t ll = ds->readLine(line, 1023);
63 if (ll == 0)
64 continue;
65 // fscanf(fd," %[^\n\r]",line);
66 if (!strncmp("alpha", line, 5))
67 {
68 process = true;
69 continue;
70 };
71 if (process)
72 {
73 float l, d, m;
74 int a, b;
75 sscanf(line, "%i.%i %f %f %f", &a, &b, &l, &d, &m);
76 if (neg)
77 b = -b;
78 if (a == 0 && b == 0)
79 neg = false;
80 int ia = (a * 10 + b) + 1800;
81 if (ia == 3600) { process = false; };
82 cl[ia] = l;
83 cd[ia] = d;
84 cm[ia] = m;
85 if (lastia != -1 && ia - lastia > 1)
86 {
87 //we have to interpolate previous elements (linear interpolation)
88 int i;
89 for (i = 0; i < ia - lastia - 1; i++)
90 {
91 cl[lastia + 1 + i] = cl[lastia] + (float)(i + 1) * (cl[ia] - cl[lastia]) / (float)(ia - lastia);
92 cd[lastia + 1 + i] = cd[lastia] + (float)(i + 1) * (cd[ia] - cd[lastia]) / (float)(ia - lastia);
93 cm[lastia + 1 + i] = cm[lastia] + (float)(i + 1) * (cm[ia] - cm[lastia]) / (float)(ia - lastia);
94 }
95 }
96 lastia = ia;
97 }
98 }
99}
100
104
105void Airfoil::getparams(float a, float cratio, float cdef, float* ocl, float* ocd, float* ocm)
106{
107 int ta = (int)(a / 360.0);
108 // float va=360.0f*fmod(a, 360.0f); FMOD IS TOTALLY UNRELIABLE HERE : fmod(-180.0f, 360.0f)=-180.0f!!!!!
109 float va = a - (float)(ta * 360);
110 if (va > 180.0f)
111 va -= 360.0f;
112 if (va < -180.0f)
113 va += 360.0f;
114 int ia = (int)((va + 180.0f) * 10.0f);
115 //drag shift
116 float dva = va + 1.15 * (1.0 - cratio) * cdef;
117 if (dva > 180.0f)
118 dva -= 360.0f;
119 if (dva < -180.0f)
120 dva += 360.0f;
121 int dia = (int)((dva + 180.0f) * 10.0f);
122 float sign = 1.0;
123 if (cdef < 0)
124 sign = -1.0;
125 *ocl = cl[ia] - 0.66 * sign * (1.0 - cratio) * sqrt(fabs(cdef));
126 *ocd = cd[dia] + 0.00015 * (1.0 - cratio) * cdef * cdef;
127 *ocm = cm[ia] + 0.20 * sign * (1.0 - cratio) * sqrt(fabs(cdef));
128}
Central state/object manager and communications hub.
void LOG(const char *msg)
Legacy alias - formerly a macro.
float sign(const float x)
Definition ApproxMath.h:140
void getparams(float a, float cratio, float cdef, float *ocl, float *ocd, float *ocm)
Definition Airfoil.cpp:105
float cd[3601]
Definition Airfoil.h:45
Airfoil(Ogre::String const &fname)
Parses the airfoil from file.
Definition Airfoil.cpp:30
float cl[3601]
Definition Airfoil.h:44
float cm[3601]
Definition Airfoil.h:46