FindObjects w/ radius
by rwillis · in Torque X 2D · 03/23/2008 (9:11 pm) · 8 replies
It looks to me like the FindObjects method at "sceneObject.SceneGraph.Container.FindObjects()" can only be used to find objects within a rectangle. The method takes in a parameter of type T2DSceneContainerQueryData, but an instance of this type don't seem to allow me to specify a radius.
If I want to search for objects within a radius of a point, how would I do that?
If I want to search for objects within a radius of a point, how would I do that?
About the author
#2
03/24/2008 (7:09 am)
But as far as I know that's only a method of a T2dSceneGraph object. How do I get that object so that I can use that method?
#3
If you don't already have player in a variable, but you know its script name, say "Player", you can get it with
03/24/2008 (7:31 am)
Suppose player is your player object and you want to find objects within a given radius. Then useList <ISceneContainerObject> list = new List<ISceneContainerObject>; player.SceneGraph.Container.FindObjects(pos, radius, findTypes, layerMask,list);
If you don't already have player in a variable, but you know its script name, say "Player", you can get it with
T2DSceneObject player = TorqueObjectDatabase.Instance.FindObject<T2DSceneObject>("Player");
#4
03/24/2008 (10:27 am)
It looks like the "player.SceneGraph.Container.FindObjects" method has no overloads and only takes a parameter of T2DSceneContainerQueryData. You're able to get other parameters?
#5
player.SceneGraph is of type BaseSceneGraph. The FindObjects with radius I was looking at is in T2DSceneGraph. So maybe try this ...
Looks like you don't have to go through Container.
Sorry, I haven't tried this...just guessing by looking at the source code ...
03/24/2008 (1:49 pm)
Ah, my bad...I wasn't looking carefully enough.player.SceneGraph is of type BaseSceneGraph. The FindObjects with radius I was looking at is in T2DSceneGraph. So maybe try this ...
List <ISceneContainerObject> list = new List<ISceneContainerObject>;
T2DSceneObject player = TorqueObjectDatabase.Instance.FindObject<T2DSceneObject>("Player");
T2DSceneGraph g = player.SceneGraph as T2DSceneGraph;
if (null != g) // better make sure the case worked
{
// set up pos, radius, findTypes, layerMask
// ...
g.FindObjects(pos, radius, findTypes, layerMask,list);
}Looks like you don't have to go through Container.
Sorry, I haven't tried this...just guessing by looking at the source code ...
#6
03/24/2008 (6:46 pm)
Thanks! That worked great.
#7
It was picking up objects a bit outside of the radius that I gave it, and after looking at the engine code I saw that it uses that radius yo gave to create a rectangle (not a sphere) for checking.
It's simple to verify the object is in the radius (Vector2.Distance()) but just be aware of that.
04/01/2008 (5:00 am)
Be careful with this if you are doing operations and cannot have it detect objects outside of the radius (say you are using the distance in a calculation). It was picking up objects a bit outside of the radius that I gave it, and after looking at the engine code I saw that it uses that radius yo gave to create a rectangle (not a sphere) for checking.
It's simple to verify the object is in the radius (Vector2.Distance()) but just be aware of that.
#8
04/01/2008 (6:31 am)
Good point. It also matters if what you want is objects whose centers are within a given radius or whose closest point (based on worldclipcollisionrect) is withing the given radius. You may even want to consider distance between the objects collision polygons. If so, you need to write some custom code.
Torque Owner Scott Goodwin
/// <summary> /// Find object in scene container within the given search radius and matching the specified object type and layers. /// </summary> /// <param name="pos">Center of search radius.</param> /// <param name="radius">Radius of search.</param> /// <param name="findTypes">Object types to match.</param> /// <param name="layerMask">Layers to match.</param> /// <param name="list">List which will contain results. Note: list is not cleared.</param> public void FindObjects(Vector2 pos, float radius, TorqueObjectType findTypes, uint layerMask, List<ISceneContainerObject> list)