[Solved][IrrBullet] Get Rigidbody from Node
-
InstinctCH
- Posts: 16
- Joined: Fri Feb 25, 2011 12:48 pm
- Location: Switzerland
[Solved][IrrBullet] Get Rigidbody from Node
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 ?"
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.
Re: [IrrBullet] Get Rigidbody from Node
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
Thanks ! Here's an extract of my code at the moment:
When the body is added to IrrBullet world:
and then when I do the raycast:
And now I'd like to do something like this:
How can I get the body associated with my node ?
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;Code: Select all
scene::ISceneNode * selectedSceneNode =
collMan->getSceneNodeAndCollisionPointFromRay(ray, intersection, hitTriangle, 0, 0);
Code: Select all
scene::ISceneNode * selectedSceneNode =
collMan->getSceneNodeAndCollisionPointFromRay(ray, intersection, hitTriangle, 0, 0);
selectedSceneNodeBody->applyForce(vector3df(x,x,x));
-
Radikalizm
- Posts: 1215
- Joined: Tue Jan 09, 2007 7:03 pm
- Location: Leuven, Belgium
Re: [IrrBullet] Get Rigidbody from Node
I think you're looking at this the wrong way
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
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)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;
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
Re: [IrrBullet] Get Rigidbody from Node
Why are you doing ray tests with irrlicht??? If you're using (irr)bullet already why don't you do ray tests with it??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 ?"
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
Thank you Radikalizm, I'll try your method but I know I will have a problem for 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.
Wich function can I use to perform this ?and use this node to look up your rigid body in the map you created earlier
@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
Irrbullet would be preferred, bullet provides more optimized algos for ray test than irrlichtInstinctCH wrote:Thank you Radikalizm, I'll try your method but I know I will have a problem for this :
Wich function can I use to perform this ?and use this node to look up your rigid body in the map you created earlier
@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.
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
Re: [IrrBullet] Get Rigidbody from Node
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
}
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
Thanks everybody for your answers !
For example, I can't apply a force to it. (body->applyCentralForce(vector3df(0,10,0), ERBTS_WORLD);)
I tried to apply your code but it seems that every time I try to do something with the body, my game crashes...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
}
-
InstinctCH
- Posts: 16
- Joined: Fri Feb 25, 2011 12:48 pm
- Location: Switzerland
Re: [IrrBullet] Get Rigidbody from Node
Oops, nevermind, I just rewrited everything and it's working fine, thanks !
