Tuesday, March 28, 2017

Orbital Aero Model: Host Application vs Aero Library

I want to keep the logic of the aerodynamic model separate from that of the application running it. All of the gravity/friction/aerodynamic calculations is in a self contained aero library. The application itself is a separate project that ingests the aero library.

Some use cases for keeping them separate:
 - The application could be a game that has a lot of processing overhead rendering the objects to the screen.
 - The application could be research experiment where it is running one or many simultaneous orbital calculations and wants minimal overhead (no graphics rendering).
 - The application could be from some third party that does its own visualization / whatnot and just needs a way to have objects move in space.

Rather than maintaining two tables of objects (one in the aero library and one in the host application), the orbital aero library has the one and only list. I'm using a pointer in the Body object in the orbital aero library to point to an Entity object in my host application.


When a Body is created, two objects are created and added to heap memory: the Body object and an Entity object. The aero library creates the Body object; the host application creates the Entity object. To tie the two together, the Body object has a hostData pointer to the Entity object.

Since my application is doing graphics rendering, the Entity object has a pointer to the visual model that will be drawn on the screen. Eventually the Entity object will contain lots of information that should be tied to the Body but has no purpose within the aero library.

(Side note: In previous posts the bodies map table contained the Body object itself rather than a pointer to the memory location of the Body object. That has been changed.)

OrbitalAeroModel.h

class OrbitalAeroModel
{
public:
   .
   .
   // Table to hold all the bodies.
   map <uint64, BodyModel*> tableBodies;
   .
   .
}

OrbitalAeroModel.cpp

uint64 OrbitalAeroModel::createBody()
{
indexCounter++;
BodyModel *thisBody = new BodyModel(this, indexCounter);
thisBody->isActive = true;

updateBodyInBodiesTable(thisBody->index, thisBody);

return thisBody->index;
}

BodyModel.h

class BodyModel
{
public:
.
.
void *hostData; // Pointer to data structure populated by host software.
.
.
}

Entity.h

#pragma once

class BodyModel;
class VisualModel;

class Entity
{
public:
Entity(void);
Entity(BodyModel *newBody);
~Entity(void);

BodyModel *bodyObject;

VisualModel *visualModelObject;

bool isVisible;

private:
};

Entity.cpp

#include "Entity.h"

Entity::Entity()
{
this->isVisible = false;

return;
}

Entity::Entity(BodyModel *newBody)
{
this->bodyObject = newBody;
this->isVisible = true;

return;
}

Entity::~Entity(void)
{
return;
}

Function for Creating a Body in the Orbital Aero App

void addBodyEarth(OrbitalAeroModel &orbitalAero, VisualModel *newVisualModel)
{
uint64 newBodyIndex = orbitalAero.createBody();
BodyModel *newBody = orbitalAero.getBody(newBodyIndex);
newBody->location_meters = 0.0;
newBody->massBase_kilograms = earthMass_kilograms;
newBody->radius_meters = earthRadius_meters;
newBody->linearVelocity_metersPerSecond = 0.0;
newBody->angularVelocity_radiansPerSecond.psi = 0.0000727220509259;

Entity *newHostBody = new Entity(newBody);
newBody->hostData = newHostBody;
newHostBody->visualModelObject = newVisualModel;

return;
}


Copyright (c) 2017 Clinton Kam
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

No comments:

Post a Comment