Jump to content

Finding an Object in the Scene


Shard
 Share

Recommended Posts

Is there a way to find objects in a scene?

 

For example, I'm trying to locate all the objects (meshes,models) that are within a certain radius of a different model without keeping track of each thing (which is hard to do with Lua scene loading)

simpleSigPNG.png

 

Programmer/Engineer/Student

www.reikumar.com

 

2.6 GHz Intel Core Duo - nVidia GeForce 8600 GT - Windows 7 64-bit - 4 Gigs RAM

C++ - Visual Studio Express - Dark GDK - Leadwerks SDK

Link to comment
Share on other sites

I do this in my tower defense game to find towers within range of each other so they can link.

 

First create a callback function:

 

void _stdcall ForEachEntityInAABBDoCallback(TEntity entity, BP extra)
{
reinterpret_cast<vector<TEntity>*>(extra)->push_back(entity);
}

 

Note that extra contains a vector of TEntity's and the entity that is passed in the callback function is pushed back into this vector.

 

Then, somewhere in your calling code:

 

       TVec3 p = EntityPosition(node->GetEntity());

TVec6 aabb;

aabb.X0 = p.X - node->GetRadius() / 2;
aabb.Y0 = 0;
aabb.Z0 = p.Z - node->GetRadius() / 2;

aabb.X1 = p.X + node->GetRadius() / 2;
aabb.Y1 = 0;
aabb.Z1 = p.Z + node->GetRadius() / 2;

vector<TEntity> *entities = new vector<TEntity>();
ForEachEntityInAABBDo(aabb, reinterpret_cast<byte*>(ForEachEntityInAABBDoCallback), reinterpret_cast<byte*>(entities));

 

entities should now contain all the entities that are in the AABB.

 

Cheers!

Link to comment
Share on other sites

I do this in my tower defense game to find towers within range of each other so they can link.

 

First create a callback function:

 

void _stdcall ForEachEntityInAABBDoCallback(TEntity entity, BP extra)
{
reinterpret_cast<vector<TEntity>*>(extra)->push_back(entity);
}

 

Note that extra contains a vector of TEntity's and the entity that is passed in the callback function is pushed back into this vector.

 

Then, somewhere in your calling code:

 

       TVec3 p = EntityPosition(node->GetEntity());

TVec6 aabb;

aabb.X0 = p.X - node->GetRadius() / 2;
aabb.Y0 = 0;
aabb.Z0 = p.Z - node->GetRadius() / 2;

aabb.X1 = p.X + node->GetRadius() / 2;
aabb.Y1 = 0;
aabb.Z1 = p.Z + node->GetRadius() / 2;

vector<TEntity> *entities = new vector<TEntity>();
ForEachEntityInAABBDo(aabb, reinterpret_cast<byte*>(ForEachEntityInAABBDoCallback), reinterpret_cast<byte*>(entities));

 

entities should now contain all the entities that are in the AABB.

 

Cheers!

 

Thanks for that.

While that definitely works, it turns out I actually need different functionality.

 

Instead of a radius, it need it to affect every object on the map and the strength of the effect would be dependent on the distance from the object.

So basically what I need is to be able to iterate through all the objects in a map, do a distance formula and then do the effect.

simpleSigPNG.png

 

Programmer/Engineer/Student

www.reikumar.com

 

2.6 GHz Intel Core Duo - nVidia GeForce 8600 GT - Windows 7 64-bit - 4 Gigs RAM

C++ - Visual Studio Express - Dark GDK - Leadwerks SDK

Link to comment
Share on other sites

Well, there is also ForEachEntityDo that iterates over all objects. You can use the same callback (well, maybe change its name to ForEachEntityDoCallback to avoid confusion) and the same calling code except change this line:

 

ForEachEntityInAABBDo(aabb, reinterpret_cast<byte*>(ForEachEntityInAABBDoCallback), reinterpret_cast<byte*>(entities));

 

in this line:

 

ForEachEntityDo(reinterpret_cast<byte*>(ForEachEntityDoCallback), reinterpret_cast<byte*>(entities));

 

and then simply iterate over entities and do EntityDistance(source, destination).

 

Cheers!

Link to comment
Share on other sites

Well, there is also ForEachEntityDo that iterates over all objects. You can use the same callback (well, maybe change its name to ForEachEntityDoCallback to avoid confusion) and the same calling code except change this line:

 

ForEachEntityInAABBDo(aabb, reinterpret_cast<byte*>(ForEachEntityInAABBDoCallback), reinterpret_cast<byte*>(entities));

 

in this line:

 

ForEachEntityDo(reinterpret_cast<byte*>(ForEachEntityDoCallback), reinterpret_cast<byte*>(entities));

 

and then simply iterate over entities and do EntityDistance(source, destination).

 

Cheers!

 

 

Thanks.

Just realized I needed another bit of functionality. I need it to only affect physics bodies >,>

How would I do that?

simpleSigPNG.png

 

Programmer/Engineer/Student

www.reikumar.com

 

2.6 GHz Intel Core Duo - nVidia GeForce 8600 GT - Windows 7 64-bit - 4 Gigs RAM

C++ - Visual Studio Express - Dark GDK - Leadwerks SDK

Link to comment
Share on other sites

There are several ways to go about.

 

You can either set a key and check the key after getting the list of entities or you can specify an entity type in the calling code. I'm not sure how this works but it is on the wiki for the ForEachEntityInAABBDo command. ForEachEntity seems incomplete.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...