OK, I'm going to be a wimp and ask the list why this code doesn't 
work.  Basically I'm interested in the logic of making an AI plane fly 
a pattern without hitting others, not in implementing its rendering, 
and needing to render it is stopping me - I just can't get my head 
round this view stuff and hoped I'd never have to!

The attached code is mostly copied from the DCS stuff in main and 
is meant to produce a Cessna at KEMT.  It doesn't.  If anyone has 
any idea why it doesn't I'd be most gratefull, since this is really 
driving me nuts!!  According to the console output ssgLoad loads 
the Cessna model sucessfully, and the Transform() function is 
called each timestep.  Unfortunately all the matrix stuff might as 
well be black magic to me, so there could be glaring errors in how 
I've used it and I'll never find them.  If it turns out to be a silly 
mistake non-matrix/view/something-I-don't-understand-related then 
I'll happily serve my penance by documenting something ;-)

Thanks - Dave

--
[EMAIL PROTECTED]

// FGAILocalTraffic - AIEntity derived class with enough logic to
// fly and interact with the traffic pattern.
//
// Written by David Luff, started March 2002.
//
// Copyright (C) 2002  David C. Luff - [EMAIL PROTECTED]
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License as
// published by the Free Software Foundation; either version 2 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

/*****************************************************************
*
* WARNING - Curt has some ideas about AI traffic so anything in here
* may get rewritten or scrapped.  Contact Curt [EMAIL PROTECTED] 
* before spending any time or effort on this code!!!
*
******************************************************************/

#include <main/globals.hxx>
#include <scenery/scenery.hxx>
//#include <simgear/constants.h>
#include <simgear/math/point3d.hxx>
#include <simgear/math/sg_geodesy.hxx>
#include <simgear/misc/sg_path.hxx>
#include <string>

SG_USING_STD(string);

#include "AILocalTraffic.hxx"

extern ssgRoot* scene;  // The global Flightgear scene graph

FGAILocalTraffic::FGAILocalTraffic() {
}

FGAILocalTraffic::~FGAILocalTraffic() {
}

// In AILocalTraffic.hxx we have:
// ssgEntity* model;
// ssgTransform* position;

void FGAILocalTraffic::Init() {
    // Hack alert - Hardwired path!!
    string planepath = "Aircraft/c172/Models/c172-dpm.ac";
    SGPath path = globals->get_fg_root();
    path.append(planepath);
    ssgTexturePath((char*)path.dir().c_str());
    model = ssgLoad((char*)planepath.c_str());
    if (model == 0) {
        model = ssgLoad((char*)"Models/Geometry/glider.ac");
        if (model == 0)
            cout << "Failed to load an aircraft model in AILocalTraffic\n";
    } else {
        cout << "AILocal Traffic Model loaded successfully\n";
    }
    position = new ssgTransform;
    position->addKid(model);

    // Hardwire to KEMT
    lat = 34.086659;
    lon = -118.031982;
    elev = 296.0 + 10.0;  // Ground is 296 so this should be above it
    heading = 90.0;
    pitch = 0.0;
    roll = 0.0;

    Transform();
}

// Run the internal calculations
void FGAILocalTraffic::Update() {
    // No logic to move the plane yet since we can't even render it :-(
    Transform(); 
}


void FGAILocalTraffic::Transform() {

    // Most of this is copied straight from the ADA DCS stuff

    double bz[3];
    double sl_radius;
    double latgc;

    //Geodetic to Geocentric angles for rotation
    sgGeodToGeoc(lat,elev,&sl_radius,&latgc);

    //moving object gbs-posn in cartesian coords
    Point3D obj_posn = Point3D(lon, lat, elev);
    Point3D obj_pos = sgGeodToCart( obj_posn );

    // Translate moving object w.r.t eye
    Point3D Objtrans = obj_pos - scenery.get_center();
    bz[0] = Objtrans.x();
    bz[1] = Objtrans.y();
    bz[2] = Objtrans.z();

    // rotate dynamic objects for lat,lon & alt and other motion about its axes 
    sgMat4 sgTRANS;
    sgMakeTransMat4( sgTRANS, bz[0],bz[1],bz[2]);

    sgVec3 fwd, rt, up;
    sgSetVec3( fwd, 1.0, 0.0, 0.0);//east,roll
    sgSetVec3( rt, 0.0, 1.0, 0.0);//up,pitch
    sgSetVec3( up, 0.0, 0.0, 1.0); //north,yaw

    sgMat4 sgROT_lon, sgROT_lat, sgROT_hdg, sgROT_pitch, sgROT_roll;
    sgMakeRotMat4( sgROT_lon, lon * SGD_RADIANS_TO_DEGREES, up );
    sgMakeRotMat4( sgROT_lat, 90-latgc*SGD_RADIANS_TO_DEGREES, rt );
    sgMakeRotMat4( sgROT_hdg, 180.0, up );
    sgMakeRotMat4( sgROT_pitch, pitch, rt );
    sgMakeRotMat4( sgROT_roll, roll, fwd );
        
    sgMat4 sgTUX;
    sgCopyMat4( sgTUX, sgROT_hdg );
    sgPostMultMat4( sgTUX, sgROT_pitch );
    sgPostMultMat4( sgTUX, sgROT_roll );
    sgPostMultMat4( sgTUX, sgROT_lat );
    sgPostMultMat4( sgTUX, sgROT_lon );
    sgPostMultMat4( sgTUX, sgTRANS );

    sgCoord shippos;
    sgSetCoord(&shippos, sgTUX );
    position->setTransform( &shippos );
    scene->addKid(position);
    cout << "Transform called\n";
}

Reply via email to