Getting distance between two Geoms.

Topics: User Forum
Mar 1, 2010 at 4:42 PM

Hello,
I'm trying to get the distance between two geoms but I'm having some accuracy problems.

What I do is, I loop all nodes in GEOM A and GEOM B and then get the closest nodes to each other and use the distance between them to get the over all closest distance. The problem with this is, if I have two polygons that are not 100% lined up, like this:

[-----]

     [-----]

The distance is not "0" but its the separation they have by X (not Y).


I need to be able to get the distance for enemy AIs. My enemies have set of weapons that are effective for a specific range  (ie. Arrows = range 10, Beams = range 100, ect.) So the enemy needs to know the distance between itself and its target accurately so it knows what weapon to use. Some weapons like "Beams" do not need the enemy and the target to be 100% or even 50% aligned. The game is pixel based an not square based, which complicates the problem even more.

Any ideas how I can get the distance between two geoms accurately?

Thanks

Mar 3, 2010 at 6:38 AM
Edited Mar 3, 2010 at 6:40 AM

I figured out how :). Here is code if anyone wants it. If it can be improved, let me know how.

[quote]
        /// <summary>
        /// Gets the nearest distance relative to the Geom given.
        /// </summary>
        /// <param name="geom">The geom that should be calculated against.</param>
        /// <returns>The distance</returns>
        public float GetNearestNodeDistance(Geom geom)
        {
            float distance = float.MaxValue;
            float diff;
            Vertices edge1 = new Vertices();
            Vector2 tempPoint = new Vector2();
            for (int i = 0; i < worldVertices.Count; i++)
            {
                // Get Edge
                edge1.Add(worldVertices[i]);
                edge1.Add(worldVertices[worldVertices.NextIndex(i)]);
                // Loop All of given geoms vectors
                for (int j = 0; j < geom.worldVertices.Count; j++)
                {
                    // Compare the two edge distances
                    // x,y |------| x,y
                    if (edge1[0].X <= edge1[1].X)
                    {
                        for (float x = edge1[0].X; x <= edge1[1].X; x++)
                        {
                            if (edge1[0].Y <= edge1[1].Y)
                            {
                                for (float y = edge1[0].Y; y <= edge1[1].Y; y++)
                                {
                                    tempPoint.X = x; tempPoint.Y = y;

                                    diff = (float)EGMGame.ExMath.Range(tempPoint, geom.worldVertices[j]);

                                    if (diff < distance)
                                    {
                                        distance = diff;
                                    }
                                }
                            }
                            else
                            {
                                for (float y = edge1[1].Y; y <= edge1[0].Y; y++)
                                {
                                    tempPoint.X = x; tempPoint.Y = y;

                                    diff = (float)EGMGame.ExMath.Range(tempPoint, geom.worldVertices[j]);

                                    if (diff < distance)
                                    {
                                        distance = diff;
                                    }
                                }
                            }
                        }
                    }
                    else
                    {
                        for (float x = edge1[1].X; x <= edge1[0].X; x++)
                        {
                            if (edge1[0].Y <= edge1[1].Y)
                            {
                                for (float y = edge1[0].Y; y < edge1[1].Y; y++)
                                {
                                    tempPoint.X = x; tempPoint.Y = y;

                                    diff = (float)EGMGame.ExMath.Range(tempPoint, geom.worldVertices[j]);

                                    if (diff < distance)
                                    {
                                        distance = diff;
                                    }
                                }
                            }
                            else
                            {
                                for (float y = edge1[1].Y; y <= edge1[0].Y; y++)
                                {
                                    tempPoint.X = x; tempPoint.Y = y;

                                    diff = (float)EGMGame.ExMath.Range(tempPoint, geom.worldVertices[j]);

                                    if (diff < distance)
                                    {
                                        distance = diff;
                                    }
                                }
                            }
                        }
                    }
                }
                edge1.Clear();
            }
            return distance;

        }

[/quote]