Skip to content

Commit a4a6c7a

Browse files
committed
JSBSim: fix initial heading, floating point accuracy, add start button, never cull back faces of aircraft
1 parent cd13aa5 commit a4a6c7a

2 files changed

Lines changed: 39 additions & 28 deletions

File tree

src/OpenCOVER/plugins/hlrs/JSBSim/JSBSim.cpp

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ JSBSimPlugin::JSBSimPlugin()
6969
plugin = this;
7070

7171
geometryTrans = new osg::MatrixTransform();
72+
geometryTrans->getOrCreateStateSet()->setMode(GL_CULL_FACE, osg::StateAttribute::OFF | osg::StateAttribute::OVERRIDE);
7273

7374
loadAvailableAircraft();
7475
readJoystickConfiguration();
@@ -148,6 +149,7 @@ JSBSimPlugin::~JSBSimPlugin()
148149
{
149150
delete FDMExec;
150151
delete printCatalog;
152+
delete startAction;
151153
delete debugButton;
152154
delete resetButton;
153155
delete upButton;
@@ -175,7 +177,8 @@ void JSBSimPlugin::reset(double dz)
175177
// Compute the eyepoint transformation from the aircraft. The GetXYZep method returns
176178
// a dimension in inches, so we divide by
177179
osg::Vec3 aircraftEyePoint(Aircraft->GetXYZep(1), Aircraft->GetXYZep(2), Aircraft->GetXYZep(3));
178-
eyePoint = osg::Matrix::translate(aircraftEyePoint / 0.00254);
180+
eyePoint = osg::Matrix::translate(aircraftEyePoint * 25.4);
181+
std::cout << "EYE POINT " << aircraftEyePoint * 25.4 << std::endl;
179182

180183
std::shared_ptr<JSBSim::FGInitialCondition> IC = FDMExec->GetIC();
181184
if (!IC->Load(SGPath("reset00.xml")))
@@ -184,7 +187,7 @@ void JSBSimPlugin::reset(double dz)
184187
}
185188

186189
// Transform the viewer coordinates to lat/lng
187-
osg::Vec3 pos = GeoData::instance()->getGlobalPosition();
190+
osg::Vec3d pos = GeoData::instance()->getGlobalPosition();
188191

189192
float altitude = pos.z();
190193
float altitudeFt = altitude / METERS_PER_FOOT - Aircraft->GetXYZep(3) * FEET_PER_INCH;
@@ -196,9 +199,9 @@ void JSBSimPlugin::reset(double dz)
196199
// there is also IC->SetAltitudeAGLFtIC(...), maybe we want that?
197200

198201
// Align heading to viewer, reset roll (wings level), keep pitch according to aircraft reset file
199-
osg::Vec3 dir(viewer(1, 0), viewer(1, 1), viewer(1, 2));
202+
osg::Vec3d dir(viewer(1, 0), viewer(1, 1), viewer(1, 2));
200203
dir.normalize();
201-
IC->SetPsiRadIC(atan2(dir[1], dir[0])); // heading
204+
IC->SetPsiRadIC(atan2(dir[1], dir[0]) - M_PI_2); // heading
202205
IC->SetPhiRadIC(0.0); // roll
203206

204207
// IC->SetPsiRadIC(0.0); // heading
@@ -343,10 +346,10 @@ void JSBSimPlugin::loadAvailableAircraft()
343346
.systemsDir = entry.value<std::string>("", "systemsDir", "systems")->value(),
344347
.enginesDir = entry.value<std::string>("", "enginesDir", "engine")->value(),
345348
.geometryTransform = osg::Matrix::scale(s, s, s)
346-
* osg::Matrix::rotate(
347-
osg::DegreesToRadians(h), osg::Vec3(0, 0, 1),
348-
osg::DegreesToRadians(p), osg::Vec3(1, 0, 0),
349-
osg::DegreesToRadians(r), osg::Vec3(0, 1, 0))
349+
* osg::Matrixd::rotate(
350+
osg::DegreesToRadians(h), osg::Vec3d(0, 0, 1),
351+
osg::DegreesToRadians(p), osg::Vec3d(1, 0, 0),
352+
osg::DegreesToRadians(r), osg::Vec3d(0, 1, 0))
350353
* osg::Matrix::translate(x, y, z),
351354
};
352355

@@ -483,6 +486,10 @@ bool JSBSimPlugin::init()
483486
if (FDMExec)
484487
FDMExec->PrintPropertyCatalog(); });
485488

489+
startAction = new ui::Action(JSBMenu, "start");
490+
startAction->setCallback([this]()
491+
{ coVRNavigationManager::instance()->setNavMode("JSBSim"); });
492+
486493
pauseButton = new ui::Button(JSBMenu, "pause");
487494
pauseButton->setState(false);
488495
pauseButton->setCallback([this](bool state)
@@ -735,25 +742,25 @@ bool JSBSimPlugin::update()
735742
auto vehicleState = Propagate->GetVState();
736743
auto location = vehicleState.vLocation;
737744

738-
osg::Vec3 pos(location.GetLongitudeDeg(), location.GetLatitudeDeg(), location.GetGeodAltitude() * METERS_PER_FOOT);
745+
osg::Vec3d pos(location.GetLongitudeDeg(), location.GetLatitudeDeg(), location.GetGeodAltitude() * METERS_PER_FOOT);
739746

740-
float scale = cover->getScale();
747+
double scale = cover->getScale();
741748
auto position = GeoData::instance()->globalToProject(pos);
742-
auto trans = osg::Matrix::translate(position * scale);
749+
auto trans = osg::Matrixd::translate(position * scale);
743750

744751
float heading = Propagate->GetEuler(JSBSim::FGJSBBase::ePsi);
745752
float roll = Propagate->GetEuler(JSBSim::FGJSBBase::ePhi);
746753
float pitch = Propagate->GetEuler(JSBSim::FGJSBBase::eTht);
747754

748755
// std::cout << " Heading " << heading << " Roll " << roll << " Pitch " << pitch << std::endl;
749756
// auto rot = osg::Matrix::rotate(
750-
// -pitch, osg::Vec3(1, 0, 0),
751-
// roll, osg::Vec3(0, 1, 0),
752-
// heading, osg::Vec3(0, 0, 1));
757+
// -pitch, osg::Vec3d(1, 0, 0),
758+
// roll, osg::Vec3d(0, 1, 0),
759+
// heading, osg::Vec3d(0, 0, 1));
753760

754-
auto rot = osg::Matrix::rotate(roll, osg::Vec3(0, 1, 0)) * osg::Matrix::rotate(pitch, osg::Vec3(1, 0, 0)) * osg::Matrix::rotate(-heading, osg::Vec3(0, 0, 1));
761+
auto rot = osg::Matrixd::rotate(roll, osg::Vec3d(0, 1, 0)) * osg::Matrix::rotate(pitch, osg::Vec3d(1, 0, 0)) * osg::Matrix::rotate(-heading, osg::Vec3d(0, 0, 1));
755762

756-
osg::Matrix newPos = osg::Matrix::inverse(/*osg::Matrix::rotate(-M_PI_2, 0, 0, 1.0) * eyePoint */ rot * trans);
763+
osg::Matrixd newPos = osg::Matrix::inverse(/*osg::Matrix::rotate(-M_PI_2, 0, 0, 1.0) * eyePoint */ rot * trans);
757764

758765
if (newPos.isNaN())
759766
{
@@ -846,6 +853,7 @@ void JSBSimPlugin::setEnabled(bool flag)
846853
{
847854
cover->getScene()->removeChild(geometryTrans.get());
848855
}
856+
849857
if (coVRMSController::instance()->isMaster())
850858
{
851859
if (flag)
@@ -943,7 +951,7 @@ void JSBSimPlugin::updateUdp()
943951
}
944952
}
945953

946-
void JSBSimPlugin::addThermal(const osg::Vec3 &velocity, float turbulence)
954+
void JSBSimPlugin::addThermal(const osg::Vec3d &velocity, float turbulence)
947955
{
948956
m_windVelocityTarget += velocity;
949957
m_windTurbulenceTarget += turbulence;

src/OpenCOVER/plugins/hlrs/JSBSim/JSBSim.h

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include <cover/coVRPlugin.h>
1212
#include <cover/input/dev/Joystick/Joystick.h>
1313

14+
#include <cover/input/input.h>
1415
#include <string>
1516
#include <util/common.h>
1617

@@ -58,7 +59,7 @@ struct AircraftInfo
5859
std::string geometryFile;
5960
std::string systemsDir;
6061
std::string enginesDir;
61-
osg::Matrix geometryTransform;
62+
osg::Matrixd geometryTransform;
6263
};
6364

6465
enum JoystickActionType
@@ -69,6 +70,7 @@ enum JoystickActionType
6970
THROTTLE,
7071
MIXTURE
7172
};
73+
7274
class JoystickAction
7375
{
7476
public:
@@ -80,7 +82,7 @@ class JoystickAction
8082
int engine = -1;
8183
bool invert = false;
8284

83-
void update(Joystick *device)
85+
virtual void update(Joystick *device)
8486
{
8587
m_oldValue = m_value;
8688

@@ -116,13 +118,13 @@ class JSBSimPlugin : public coVRPlugin, public ui::Owner, public opencover::coVR
116118

117119
static JSBSimPlugin *instance() { return plugin; };
118120

119-
bool init();
120-
bool update();
121+
bool init() override;
122+
bool update() override;
121123

122124
// from coVRNavigationProvider
123125
virtual void setEnabled(bool) override;
124126

125-
void addThermal(const osg::Vec3 &velocity, float turbulence);
127+
void addThermal(const osg::Vec3d &velocity, float turbulence);
126128

127129
private:
128130
void readJoystickConfiguration();
@@ -136,6 +138,7 @@ class JSBSimPlugin : public coVRPlugin, public ui::Owner, public opencover::coVR
136138
static JSBSimPlugin *plugin;
137139
ui::Menu *JSBMenu;
138140
ui::Action *printCatalog;
141+
ui::Action *startAction;
139142
ui::Button *pauseButton;
140143
ui::Button *debugButton;
141144
ui::Action *resetButton;
@@ -170,7 +173,7 @@ class JSBSimPlugin : public coVRPlugin, public ui::Owner, public opencover::coVR
170173
vector<SGPath> LogDirectiveName;
171174
vector<string> CommandLineProperties;
172175
vector<double> CommandLinePropertyValues;
173-
osg::Matrix eyePoint;
176+
osg::Matrixd eyePoint;
174177

175178
double current_seconds = 0.0;
176179
double SimStartTime = 0.0;
@@ -236,16 +239,16 @@ class JSBSimPlugin : public coVRPlugin, public ui::Owner, public opencover::coVR
236239
void reset(double dz = 0.0);
237240

238241
//! this functions is called when a key is pressed or released
239-
virtual void key(int type, int keySym, int mod);
242+
virtual void key(int type, int keySym, int mod) override;
240243

241244
OpenThreads::Mutex mutex;
242245

243-
osg::Matrix lastPos;
246+
osg::Matrixd lastPos;
244247

245248
// For wind/turbulence
246-
osg::Vec3 m_windVelocitySetting;
247-
osg::Vec3 m_windVelocityCurrent;
248-
osg::Vec3 m_windVelocityTarget;
249+
osg::Vec3d m_windVelocitySetting;
250+
osg::Vec3d m_windVelocityCurrent;
251+
osg::Vec3d m_windVelocityTarget;
249252
float m_windTurbulenceCurrent;
250253
float m_windTurbulenceTarget;
251254

0 commit comments

Comments
 (0)