[solved] Ogre::Entity to OgreNewt::Body and vice versa?

Arcanor

06-04-2007 18:55:28

In my code, I'm keeping a pointer to my Player entity in a variable called mPlayerEntity. Using mPlayerEntity I can get to lots of information about the player. Is there any way I can find the associated OgreNewt::Body using function calls based on mPlayerEntity? Or do I need to save the OgreNewt::Body as a variable as well?

As an alternative, if I saved the OgreNewt::Body, could I find the associated entity pointer?

Hopefully I don't have to save both values.

Drakon

06-04-2007 19:42:19

If you have a OgreNewt::Body , you can always get Ogre::Node from it by simple:

Ogre::Node * node = Body->getOgreNode();

but how to get an Entity from this I don't know...

[EDIT]

try this:


Ogre::SceneNode *node = (Ogre::SceneNode*) Body->getOgreNode();
Ogre::Entity * ent = (Ogre::Entity*)node->getAttachedObject(0);
//for test
Ogre::LogManager::getSingleton().logMessage(ent->getName());


now if your name in the log is the same as the name you give to the Player Entity it's working ;)

Ps. In my test it is working ;)

Acid_Gambit

06-04-2007 19:55:46

If you have a OgreNewt::Body , you can always get Ogre::Node from it by simple:

Ogre::Node * node = Body->getOgreNode();

but how to get an Entity from this I don't know...


Once you have the SceneNode like this:


Ogre::SceneNode* node = body->getOgreNode();


You can get the Ogre::Entity like this:


Ogre::SceneManager* sceneMgr = node->getCreator();
Ogre::Entity* ent = sceneMgr->getEntity("my ogre entity");

Arcanor

06-04-2007 20:47:59

If you have a OgreNewt::Body , you can always get Ogre::Node from it by simple:

Ogre::Node * node = Body->getOgreNode();

but how to get an Entity from this I don't know...

[EDIT]

try this:


Ogre::SceneNode *node = (Ogre::SceneNode*) Body->getOgreNode();
Ogre::Entity * ent = (Ogre::Entity*)node->getAttachedObject(0);
//for test
Ogre::LogManager::getSingleton().logMessage(ent->getName());


now if your name in the log is the same as the name you give to the Player Entity it's working ;)

Ps. In my test it is working ;)

Drakon: yes, I was thinking of something like that myself, but I'm concerned that getAttachedObject(0) may not always return the correct attached object, and it would create a dependency when setting up the node.

What I'm thinking now is possibly to use entity->setUserAny() to pass in a pointer to my body, then use entity->getUserAny() to retrieve it. I've not worked with these functions before though.

Arcanor

06-04-2007 21:04:15


You can get the Ogre::Entity like this:


Ogre::SceneManager* sceneMgr = node->getCreator();
Ogre::Entity* ent = sceneMgr->getEntity("my ogre entity");

Acid_Gambit: yes, I can get the entity from the scene manager if I know the entity's name. But how can I get the name if I only know the body pointer?

My objective is to keep one or the other, not both. If I'm going to keep the string for the entity name, I may as well keep the pointer to the entity itself.

Langhole

06-04-2007 23:58:07

well you gave the entity a name when you created it correct?


// create entity
Entity* ent = mSceneMgr->createEntity("MyNameIsAwesome", "my_sweet.mesh");

// then create node and attach
SceneNode* node = mSceneMgr->getRootSceneNode()->createChildSceneNode("AwesomeNode");

// create a temp 'get' entity to store the original entity's data
Entity* getEnt = mSceneMgr->getEntity("MyNameIsAwesome");

// then what happens?
getEnt->doWhateverIWantCuzImAProgrammerAndICan();


[EDIT] You could store the getEnt in a class and have it there for whenever your fingertips need it... but I've not tested the above code anyway, but in theory it might work.

IMO... but i'm a n00b so w/e

Arcanor

07-04-2007 00:28:54

I think I'm chasing a dead end here.

What I was hoping for is a general purpose way to get from the entity to the body, or from the body to the entity, i.e. - something like this:

Ogre::Entity* OgreNewt::Body::getEntity(void)

Unfortunately, I think that the reason why such a function doesn't exist is that each Ogre::Node can actually be attached to any number of Ogre::Entity's.

So that's why a Body can only tell me its attached Node.

Thanks everyone for your ideas though. :)

Arcanor

07-04-2007 00:57:59

Okay, I've got it working after all!

Right after I posted my last message I realized that while each Node may have multiple Entity's, each Entity can only be attached to a single Node. Therefore, all I need is mPlayerEntity, and I can do as follows...

When creating and initializing the Body, seed the Entity with a pointer to the Body:

ent->setUserAny(Any(body));

When I need to use the Body, get the pointer from the Entity like so:

body = Ogre::any_cast<OgreNewt::Body*>(mPlayerEntity->getUserAny());

Voila! :)

Arcanor

13-04-2007 03:30:17

One more update on this topic...

OgreNewt::Body::setUserData() can be used to get from a body directly back to another object, such as an Ogre::Entity, or a user-defined game object, such as a class which contains both an Entity and a related Body.

For example, in my ArcObject game object class, as soon as I am finished creating my Body, I do this:
// set a pointer so we can get back to this ArcObject:
mBody->setUserData(this);

Then when I want to get back to my game object in my custom force callback, I can use this:
// get the associated game object:
ArcObject* obj = (ArcObject*) body->getUserData();

I hope this can help someone else who is just starting out.