Virtual Earth 3D Flight Simulator – part 2

Your class should look like this:

using System;
using System.IO;
using System.Drawing;
using System.Threading;
using System.Reflection;

using System.Runtime.InteropServices;
using Microsoft.MapPoint.Rendering3D.GraphicsProxy;
using Microsoft.MapPoint.PlugIns;
using Microsoft.MapPoint.Rendering3D;

using Microsoft.MapPoint.Rendering3D.Steps.Actors;
using Microsoft.MapPoint.Rendering3D.Atmospherics;
using Microsoft.MapPoint.Binding;

namespace SimpleFlightSimulator
{
    [Guid("67D5CFD7-9975-492d-B7AE-1B4DE757B0BD")]
    public class SimpleFlightSimulatorPlugin : PlugIn
    {             
     }
}

There are three methods, Name, Activate and the Deactivate methods, that are a part of the PlugIn class which you will want to override.

        public override string Name
        {
            get { return "Simple Flight Simulator"; }
        } 

        public SimpleFlightSimulatorPlugin(Host host)
            : base(host)
        {
            // it is encouraged that most startup logic occur in the Activate function.
        }

         public override void Activate(object activationObject)
        {
            base.Activate(activationObject);
        }

        public override void Deactivate()
        {
            base.Deactivate();

        }

You will now create an Airplane class. This class will have three methods, Load, UpdateCockpit, and UpdateGauges. The Load method will create a ScreenImageActor to overlay the airplane cockpit imagery over the map and a ScreenTextActor to create gauge readings. The gauges will display the coordinates of the Airplane (camera), altitude, pitch (horizon) and heading. The UpdateCockpit method will allow you to change the airplane cockpit image. The UpdateGauges method will retrieve all the data required for the gauges and update the ScreenTextActor. The Airplane class should look like this:

using System;
using System.IO;
using System.Drawing;
using System.Reflection;
using Microsoft.MapPoint;
using Microsoft.MapPoint.Rendering3D;
using Microsoft.MapPoint.Rendering3D.Cameras;
using Microsoft.MapPoint.Rendering3D.Steps.Actors;
using Microsoft.MapPoint.Geometry.VectorMath;

namespace SimpleFlightSimulator
{
    public class Aiplane
    {
        private ScreenTextActor gauges;
        private ScreenImageActor imageActor;
        private Host _host;

        public Aiplane(Host host)
        {
            this._host = host;
        }

        public void Load(string imageSource, Point imagePosition, Size imageSize, Color gaugeColor, Point gaugePosition)
        {
            gauges = new ScreenTextActor("GaugeReadings", "", new Font(FontFamily.GenericMonospace, 11.0f), gaugeColor, gaugePosition, null);
            this._host.Actors.Add(gauges);

            Stream data = Assembly.GetExecutingAssembly().GetManifestResourceStream(imageSource);
            Bitmap cockpit = (Bitmap)Bitmap.FromStream(data);
            data.Close();

            imageActor = new ScreenImageActor(cockpit, imagePosition, imageSize, null);
            this._host.Actors.Add(imageActor);
        }

        public void UpdateCockpit(Host host, string imageSource, Point imagePosition, Size imageSize, Color gaugeColor, Point gaugePosition)
        {
            gauges.Color = gaugeColor;
            gauges.Position = gaugePosition;

            imageActor.Size = new Size(0, 0);

            Stream data = Assembly.GetExecutingAssembly().GetManifestResourceStream(imageSource);
            Bitmap cockpit = (Bitmap)Bitmap.FromStream(data);
            data.Close();

            ScreenImageActor newImageActor = new ScreenImageActor(cockpit, imagePosition, imageSize, null);
            host.Actors.Add(newImageActor);

            imageActor = newImageActor;
        }

        public void UpdateGauges(object state)
        {
            RollPitchYaw rpy = this._host.Navigation.CameraLocalOrientation;
            GeodeticCameraSnapshot cameraSnapshot = this._host.Navigation.CameraSnapshot;
            double heading = cameraSnapshot.LocalOrientation.RollPitchYaw.Yaw * Constants.DegreesPerRadian;

            if (heading <= 0)
                heading *= -1;
            else
                heading = 360 – heading;

            GeodeticPositionSnapshot position = this._host.Navigation.CameraPosition;
            double pitch = rpy.Pitch * Constants.DegreesPerRadian;

            //Altitude in meters
            double altitude = position.Location.Altitude;

            this.gauges.Text = String.Format("Heading: {0:N2}º\r\nPitch: {1:N2}º\r\nAltitude: {2:N2} m\r\nLatitude: {3:N5}º\r\nLongitude: {4:N5}º",
                heading, pitch, altitude, position.Location.LatitudeDegrees, position.Location.LongitudeDegrees);
        }
    }
}


Part 1 – http://rbrundritt.spaces.live.com/blog/cns!E7DBA9A4BFD458C5!756.entry
Part 3 – http://rbrundritt.spaces.live.com/blog/cns!E7DBA9A4BFD458C5!751.entry
Part 4 – http://rbrundritt.spaces.live.com/blog/cns!E7DBA9A4BFD458C5!750.entry

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s