Go to content

Main menu:

Detecting Closest Neighbors in Multimedia Fusion 2 and Clickteam Fusion

Last Updated: September 2nd, 2014
Many game mechanics require the detection of the closest neighbor. In Multimedia Fusion 2 and Clickteam Fusion, there are several ways to detect the closest neighbor; some better than others. This tutorial and accompanying example files provide an insight to some methods in detecting a object's closest neighbor in MMFusion.

Quick Links:
Single Instance Method

The flow chart to the right shows the concept to detecting the closest neighbor where you have two objects, one instance of Object A and multiple instances of Object B.

This example uses one object (Object A) and multiple objects (Object B). Since there is only one of Object A, we can loop through all of the Object Bs and calculate the distance between the current Object B and Object A and save the distance in Object B.

After all of the Object Bs have been looped through, we simply find the Object B with the lowest distance. This object is the closest neighbor to Object A.

Flow chart of the Single Instance Method
Click to enlarge image
Flow chart of the Single Instance & Select Object  Method
Click to enlarge image
Single Instance with Select Object Extension Method

The flow chart to the left shows the concept to detecting the closest neighbor where you have two objects, one instance of Object A and multiple instances of Object B, and the Select Object Extension.

This example uses one object (Object A) and multiple objects (Object B). Since there is only one of Object A, we can loop through all of the Object Bs and calculate the distance between the current Object B and Object A and save the distance in Object B.

After all of the Object Bs have been looped through, we use the Select Object Extension to select the Object B with the lowest alterable value. This object is the closest neighbor to Object A.

Overlap Object Method

The flow chart to the right shows the concept to detecting the closest neighbor by using object collisions/overlaps.

This example uses one object (Object A) and multiple objects (Object B) but can be applicable to multiple objects of both types. This method takes a scalable active object (at least 3 x 3 so the hotspot can sits in the center of the object) and scales the object upward until a neighbor is found.

This example uses a small scaling object and scales up until a neighbor is found. The first overlapping object is the closest neighbor to Object A.

An alternative method is to start with a fairly large object (say 150x150) and scale down until only one object is overlapping the scaling object. The last overlapping object is the closest neighbor to Object A.

Flow chart of the Overlap Method
Click to enlarge image
Flow chart of the Multiple Pair Method
Click to enlarge image
Multiple Pair Method

The flow chart to the left shows the concept to detecting the closest neighbor where you have two objects, multiple instances of Object A and multiple instances of Object B.

This example uses multiple objects of both types (Object A and Object B). This method loops Object A. For each loop of Object A, all of the Object Bs are looped through. During this second loop, the distance is calculated between the current Object B and the current Object A and saved into the Object A. If the distance for the currently selected objects is smaller than the currently saved distance, the new distance is saved along with the fixed value/ID of the appropriate object in the current Object A.

After all of the objects are looped through, the Object B whose fixed value/ID matches the appropriate value in Object A is the closest neighbor to said Object A.

Conclusion/Summary

In summary there are several different ways to detecting the closest neighbor from using object overlaps to finding the object with the lowest distance to the stationary object.

There are more possible ways to determine the closest neighbor such as putting the points into a data structure like a kd-tree. It is easily possible to limit the closest neighbor to be within a certain distance of the originating object by adding a one condition, a distance constraint, in each example file.

Hopefully this tutorial brought and explained new concepts on how to detect the closest neighbor in MMFusion. If you want to see examples of these methods, check out the example file located in the "Downloads" section at the top of the page. Finally, remember to rate the tutorial at the bottom of the page!


Rate This Tutorial
Vote: 5.0/5
If you have read this tutorial, rate it from poor (1) to average (3) to great (5)!
Back to content | Back to main menu