[Solved][IrrBullet] Get Rigidbody from Node

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
Post Reply
InstinctCH
Posts: 16
Joined: Fri Feb 25, 2011 12:48 pm
Location: Switzerland

[Solved][IrrBullet] Get Rigidbody from Node

Post by InstinctCH »

Hello everyone !

I'm working on a FPS project using IrrBullet for physics and I have a small problem:

When the player shots, I make a raycast using this function: getSceneNodeAndCollisionPointFromRay() wich returns the object "hit by the bullet". Now, I would like to apply a force to the objet that has been hit, but to perform that I need to get which RigidBody has been hit. So my question is: "How do you get the RigidBody hit from the SceneNode hit ?"
Last edited by InstinctCH on Thu Oct 20, 2011 8:06 am, edited 1 time in total.
smso
Posts: 246
Joined: Fri Jun 04, 2010 3:28 pm
Location: Hong Kong

Re: [IrrBullet] Get Rigidbody from Node

Post by smso »

You may use a map to save the node and the associated body when setting up irrbullet:

Code: Select all

core::map<scene::ISceneNode*, IRigidBody*> NodeBodyMap;
InstinctCH
Posts: 16
Joined: Fri Feb 25, 2011 12:48 pm
Location: Switzerland

Re: [IrrBullet] Get Rigidbody from Node

Post by InstinctCH »

Thanks ! Here's an extract of my code at the moment:

When the body is added to IrrBullet world:

Code: Select all

 
IRigidBody *body;
body = world->addRigidBody(shape);
core::map<scene::ISceneNode* , IRigidBody* > *NodeBodyMap;
NodeBodyMap->set(Node, body);
 return body;
and then when I do the raycast:

Code: Select all

 
scene::ISceneNode * selectedSceneNode =
collMan->getSceneNodeAndCollisionPointFromRay(ray, intersection, hitTriangle, 0, 0); 
 
And now I'd like to do something like this:

Code: Select all

 
scene::ISceneNode * selectedSceneNode =
collMan->getSceneNodeAndCollisionPointFromRay(ray, intersection, hitTriangle, 0, 0); 
 
selectedSceneNodeBody->applyForce(vector3df(x,x,x));
 
How can I get the body associated with my node ?
Radikalizm
Posts: 1215
Joined: Tue Jan 09, 2007 7:03 pm
Location: Leuven, Belgium

Re: [IrrBullet] Get Rigidbody from Node

Post by Radikalizm »

I think you're looking at this the wrong way
When the body is added to IrrBullet world:

Code: Select all

IRigidBody *body;
body = world->addRigidBody(shape);
core::map<scene::ISceneNode* , IRigidBody* > *NodeBodyMap;
NodeBodyMap->set(Node, body);
 return body;
You're making some mistakes here, what you want to do is make a map in your application where you store scene node and rigid body pairs, what you're doing now is creating a reference to a map which you never initialize (huge error right there), and then you try to insert your scene node and rigid body into this uninitialized map (which will result in an error)
Now, even if you were to initialize this map you would still be creating a new map every time you create a rigid body, and you really, really, really don't want to do that

I'm not someone to hand out example code, so I'm going to give you an overview:
1. Create a map in your code which is easily accesible by the function which creates your rigid bodies and scene nodes
2. For every rigid body you want to associate with a scene node you enter this given body and node in your map (the scene node as key, the body as value)
3. Do a ray cast to select a scene node, and use this node to look up your rigid body in the map you created earlier
4. Do whatever you want with the found rigid body
serengeor
Posts: 1712
Joined: Tue Jan 13, 2009 7:34 pm
Location: Lithuania

Re: [IrrBullet] Get Rigidbody from Node

Post by serengeor »

InstinctCH wrote:Hello everyone !

I'm working on a FPS project using IrrBullet for physics and I have a small problem:

When the player shots, I make a raycast using this function: getSceneNodeAndCollisionPointFromRay() wich returns the object "hit by the bullet". Now, I would like to apply a force to the objet that has been hit, but to perform that I need to get which RigidBody has been hit. So my question is: "How do you get the RigidBody hit from the SceneNode hit ?"
Why are you doing ray tests with irrlicht??? If you're using (irr)bullet already why don't you do ray tests with it??
Working on game: Marrbles (Currently stopped).
InstinctCH
Posts: 16
Joined: Fri Feb 25, 2011 12:48 pm
Location: Switzerland

Re: [IrrBullet] Get Rigidbody from Node

Post by InstinctCH »

Thank you Radikalizm, I'll try your method but I know I will have a problem for this :
and use this node to look up your rigid body in the map you created earlier
Wich function can I use to perform this ?

@Serengeor: Humm, yeah I'm doing ray tests with Irrlicht ^^' what's wrong with that ? Is it better to use IrrBullet ? I tried to do the same as in the irrlicht tuts.
Radikalizm
Posts: 1215
Joined: Tue Jan 09, 2007 7:03 pm
Location: Leuven, Belgium

Re: [IrrBullet] Get Rigidbody from Node

Post by Radikalizm »

InstinctCH wrote:Thank you Radikalizm, I'll try your method but I know I will have a problem for this :
and use this node to look up your rigid body in the map you created earlier
Wich function can I use to perform this ?

@Serengeor: Humm, yeah I'm doing ray tests with Irrlicht ^^' what's wrong with that ? Is it better to use IrrBullet ? I tried to do the same as in the irrlicht tuts.
Irrbullet would be preferred, bullet provides more optimized algos for ray test than irrlicht

I would recommend reading up on containers (arrays, maps, queues, lists, etc.) and learn how to work with these, they're crucial elements in any programming project
smso
Posts: 246
Joined: Fri Jun 04, 2010 3:28 pm
Location: Hong Kong

Re: [IrrBullet] Get Rigidbody from Node

Post by smso »

Hope the following could keep you going.

You could pass the map as reference to the rigid body setup function:
=====================================================================
IRigidBody* setupRigidBody
(
core::map<scene::ISceneNode*, IRigidBody*>& nodeBodyMap,
...
)
{
...

nodeBodyMap[node] = body;
...
}


Call the function:
==================
core::map<scene::ISceneNode*, IRigidBody*> NodeBodyMap;
... setupRigidBody(NodeBodyMap, ...);


Find the body:
==============
if (NodeBodyMap.find(selectedSceneNode))
{
IRigidBody* body = NodeBodyMap[selectedSceneNode];
// do what ever you want with body

}
InstinctCH
Posts: 16
Joined: Fri Feb 25, 2011 12:48 pm
Location: Switzerland

Re: [IrrBullet] Get Rigidbody from Node

Post by InstinctCH »

Thanks everybody for your answers !
smso wrote:Hope the following could keep you going.

You could pass the map as reference to the rigid body setup function:
=====================================================================
IRigidBody* setupRigidBody
(
core::map<scene::ISceneNode*, IRigidBody*>& nodeBodyMap,
...
)
{
...

nodeBodyMap[node] = body;
...
}


Call the function:
==================
core::map<scene::ISceneNode*, IRigidBody*> NodeBodyMap;
... setupRigidBody(NodeBodyMap, ...);


Find the body:
==============
if (NodeBodyMap.find(selectedSceneNode))
{
IRigidBody* body = NodeBodyMap[selectedSceneNode];
// do what ever you want with body

}
I tried to apply your code but it seems that every time I try to do something with the body, my game crashes... :? For example, I can't apply a force to it. (body->applyCentralForce(vector3df(0,10,0), ERBTS_WORLD);)
InstinctCH
Posts: 16
Joined: Fri Feb 25, 2011 12:48 pm
Location: Switzerland

Re: [IrrBullet] Get Rigidbody from Node

Post by InstinctCH »

Oops, nevermind, I just rewrited everything and it's working fine, thanks !
Post Reply