Dec 15 2010

Calculating Distance from GPS Coordinates

I've been carrying this equation around forever and a day and thought I would share it. With this equation you can calculate the distance between GPS coordinates. I tend to use SI units, but you should be able to easily adjust it for units of your choosing.

using System; 
using System.Device.Location; 
 
namespace J2i.Net.GPS 
    public static class DistanceCalculator 
    { 
 
        public const double EarthRadiusInMiles = 3956.0; 
        public const double EarthRadiusInKilometers = 6367.0; 
        public const double EarthRadiusInMeters = EarthRadiusInKilometers*1000; 
 
        public static double ToRadian(double val) { return val * (Math.PI / 180); } 
        public static double ToDegree(double val) { return val * 180 / Math.PI; } 
        public static double DiffRadian(double val1, double val2) { return ToRadian(val2) - ToRadian(val1); } 
 
 
 
        public static double CalcDistance(GeoCoordinate p1, GeoCoordinate p2) 
        { 
            return CalcDistance(p1.Latitude, p1.Longitude, p2.Latitude, p2.Longitude, EarthRadiusInKilometers); 
        } 
 
        public static Double Bearing(GeoCoordinate p1, GeoCoordinate p2) 
        { 
            return Bearing(p1.Latitude, p1.Longitude, p2.Latitude, p2.Longitude); 
        } 
 
        public static double CalcDistance(double lat1, double lng1, double lat2, double lng2, double radius) 
        { 
 
            return radius * 2 * Math.Asin(Math.Min(1, Math.Sqrt((Math.Pow(Math.Sin((DiffRadian(lat1, lat2)) / 2.0), 2.0) 
                + Math.Cos(ToRadian(lat1)) * Math.Cos(ToRadian(lat2)) * Math.Pow(Math.Sin((DiffRadian(lng1, lng2)) / 2.0), 2.0))))); 
        } 
 
        public static Double Bearing(double lat1, double lng1, double lat2, double lng2) 
        { 
 
            { 
                var dLat = lat2 - lat2; 
                var dLon = lng2 - lng1; 
                var dPhi = Math.Log(Math.Tan(lat2 / 2 + Math.PI / 4) / Math.Tan(lat1 / 2 + Math.PI / 4)); 
                var q = (Math.Abs(dLat) > 0) ? dLat / dPhi : Math.Cos(lat1); 
 
                if (Math.Abs(dLon) > Math.PI) 
                { 
                    dLon = dLon > 0 ? -(2 * Math.PI - dLon) : (2 * Math.PI + dLon); 
                } 
                //var d = Math.Sqrt(dLat * dLat + q * q * dLon * dLon) * R; 
                var brng = ToDegree(Math.Atan2(dLon, dPhi)); 
                return brng; 
            } 
        } 
 
    } 
 

Tags: ,

Jul 27 2009

MIP Mapping and Windows Mobile

Category: Windows Mobile | GraphicsJoel Ivory Johnson @ 16:53

I've heard developers ache over the challenges that targetting multiple form factors and multiple resolutions can bring.  I just went through an excercise this weekend in which I had to make a simple game that would run on 4 different form factors.  320x240, 320x320, 240x400, and 480x640.  It wasn't as challenging as one would think (Of course it helps that it was a simple game).  To make my graphics look their best on each platform I used mip maps for my sprites

 

I didn't specifically target any of those resolutions.  Instead for my sprites I made a high resolution version (first row) and then scaled it down in half, and again, and again.  Then I created a mapping function so that if I requested the red orb of a certain size then a copy of the orb that best fit my requirements would be selected.  The result that I ended up with was the graphics looked absolutly stunning when the program was wun on a WVGA device, but scaled themselves back reasonably when run on a QVGA device.

The technique itself is old.  I remember first being introduced to it in 1994.  And it is very effective.

Tags:

Jul 26 2009

IcoFX - Free Icon Editor

When it comes to developing software the icon for the executable is usually not something handled by me and not something over which I place a great amount of concern.  But earlier today I was working on a Windows Mobile program that I wanted to have a polished look, down to the program icon.  I designed my icon work in Photoshop and was trying to import it into the Visual Studio icon editor.  I had no trouble doing this with the 24-bit true-color version of the icons, but when I was adding the paletted versions I ran into problems.  I evaluated some free icon editors and came across one that I absolutly love, IcoFX.

In short IcoFX is free and very capable.  It will save icons in both Windows and OS X format.  It takes care of resizing the icon images for you. If you are ever in need of a better icon editor you should check this out.

IcoFX Web Page

Tags:

Jul 25 2009

Creating a High Performance Message Loop

I've heard criticisms of the Windows and Windows Mobile environment because of the way that the default message pump is implemented and the performance cap that it puts on applications that require constant refreshing of the UI (such as games).  The problem is that the default message pump is geared towards business applications and minimizing CPU usage.  If you need to make a high performance application or game then you should use a different message pump implementation. You should not be waiting on WM_PAINT message.  You should just go ahead and repaint the screen.   Given that many developers will never alter the default message pump implementation it is preferable to have one that has low CPU usage by default.  If you want to do performance graphics you need a loop similar to the following:

	
bool keepRunning = true;
while(keepRunning)
{
	if(PeekMessage(&msg,NULL,0,0,TRUE))
	{
		if(msg.message==WM_QUIT)
			keepRunning=false;
		if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg)) 
		{
			TranslateMessage(&msg);
			DispatchMessage(&msg);
		}
	}
	else
	{
		//Execute Game Logic Here and rendering here
	}
}

The default message pump handling would be blocked when there are no messages to process and the WM_PAINT message (being low priority) would always be the last thing on the queue. In the message pump above the application will be able to concentrate on the game logic and give attention to messages on the queue when they arrive.  We'll talk about  other things that you would need to do for a game in coming posts.

Tags:

Jul 23 2009

Unsuccessfully Attempting to Identify the DirectDraw Driver

In my experience identifying the DirectDraw driver on the device is difficult to impossible.  According to documentation the IDIRECTDRAW::GetDeviceIdentifier method should be used to retrieve the driver information.  At the time that I am writing this the Windows Mobile 6 documentation shows that this method has one parameter of type  LPDDDEVICEIDENTIFIER.  But when you try to use the method you’ll find there is a second parameter needed of type DWORD.  When I first came across this I was a little confused because there was no mention of it in the documentation.  But I finally came across documentation for Windows CE 3.0 that mentioned the second parameter as being a flag and DDGDI_GETHOSTIDENTIFIER was the only possible value.  I tried to use the flag but then I found that it wasn’t defined in my header file.  After a bit of searching on the Internet I found this flag has a value of one.  I added a conditional #define to my source code.  It is conditional so that if I ever do get a correct header file it won’t break my code with redundant definitions.

#ifndef DDGDI_GETHOSTIDENTIFIER
#define DDGDI_GETHOSTIDENTIFIER 0x00000001L
#endif

I ran my code an error informing me that the argument was invalid.  I considered the possibility that 1 wasn’t the correct value. So I tried calling the function in a for loop testing out all possible values.  For the value of 0 I received  E_NOTIMPL. For all other values I received E_INVALIDARG.  As a final test I ran this against an emulator image with identical results.  So as far as I am concerned this method cannot be called in confidence.

Tags:

Jul 23 2009

Windows Mobile Graphic APIs

I recent had to take into consideration the APIs available in Windows Mobile for something I was working on recreationally. As per usual I added my notes and conclusions to my OneNote notebook and thought I would share some of it for those that may need to look up the same information.

GDI A moderate to slow speed performer with wide compatibility.
GDI+ Successor of GDI. GDI+ adds gradients, complex paths, and uses ARGB to represent color.
Imaging API for manipulating Images and manipulating them, but slow.
GAPI An API now officially dead as of WM6.5. Predates DirectX. Nothing more of it will be said here.
DirectDraw Gives extremely fast access to imaging surface. Targets low level operations. Centered around bit blting.
Direct3D Gives access to 3D display hardware and provides emulation where hardware isn’t available. Potentially high performing, though some OEMs use improper/slow implementation. HTC historically installed an improper version of Direct3D on their devices
DirectShow API for handling video and audio streams
OpenGL ES Platform independent graphic API. May or may not be present on your Windows Mobile device. If you use this be prepared to provide an implementation to the target device.

I think I may have to go through all of these APIs and write a general introduction like what I did for Windows Mobile Power Management.

 

Tags: