Problems on CollisionCallBack

ogre_newbie

24-03-2009 14:39:02

Hi all

I have made a class to implement collision callback. When 2 ball objects collide, I want them to stick to together by a hinge joint. I tested the collision callback. It works, but when I add a join to link 2 collided objects, the joint doesn't work. Could anyone here tell me what the problem is? Or Is there any suggestion you could give me to join two balls when collision occurs ?

Thanks a lot!
int ballColCallback::userBegin()
{

Ogre::SceneNode* ballNode = m_body0->getOgreNode();
Ogre::Entity* ball = static_cast<Ogre::Entity*> (ballNode->getAttachedObject(0));

Ogre::SceneNode* ballNode2 = m_body1->getOgreNode();
Ogre::Entity* ball2 = static_cast<Ogre::Entity*> (ballNode2->getAttachedObject(0));

OgreNewt::Collision* col = new OgreNewt::CollisionPrimitives::Ellipsoid( mWorld, Ogre::Vector3(60,60,60) );

OgreNewt::Body* ball_Newtbod = new OgreNewt::Body( mWorld, col );
ball_Newtbod->attachToNode(ballNode);

OgreNewt::Body* ball_Newtbod2 = new OgreNewt::Body( mWorld, col );
ball_Newtbod2->attachToNode(ballNode2);

// ball_Newtbod->addForce(Ogre::Vector3(0,100,0));

OgreNewt::Body* parent = NULL;
OgreNewt::Body* child = NULL;
OgreNewt::Joint* joint;

parent = ball_Newtbod;
child = ball_Newtbod2;

//Ogre::LogManager::getSingletonPtr()->logMessage(ball->getName());
//Ogre::LogManager::getSingletonPtr()->logMessage(ball2->getName());

if(!strncmp(ball->getName().c_str(),"Ball",4) && !strncmp(ball2->getName().c_str(),"Ball",4))
{
ball->setMaterialName("myMaterial/beachstone");
ball2->setMaterialName("myMaterial/ground");

joint = new OgreNewt::BasicJoints::BallAndSocket( mWorld, child, parent, Ogre::Vector3(30,0,0) );
joint = new OgreNewt::BasicJoints::Hinge( mWorld, child, parent, Ogre::Vector3(30,0,0), Ogre::Vector3(0,0,1) );

}

return 1;
}
ogre_newbie
Newcomer

Posts: 20
Joined: Tue Sep 16, 2008 11:31 am

melven

24-03-2009 23:19:59

Why are you creating new bodies? And what's about the old ones?
I don't think you should attach several nodes to one body (the name of the attachToNode-function is misleading, that's why it is changed to attachNode in the OgreNewt for newton 2.0 beta... in fact you "attach a node to a body", so if the body is moved, it'll set the position of the node to its new position each frame (and orientation of course)).

ogre_newbie

25-03-2009 00:55:52

Hi melven

Thanks for your reply.

I think I know the problem now. The reason why I created new bodies is that I want to create a joint to link the balls when they collided each other. In the function - ballColCallback::userBegin(), I couldn't get the OgreNewt bodies back to add them to a joint.

Do you have any suggestion to make the balls join together when they collide each other?

Thanks a lot!!!

melven

25-03-2009 08:21:54

I'm not shure if it works, but have you tried:
joint = new OgreNewt::BasicJoints::BallAndSocket( mWorld, m_body0, m_body1, Ogre::Vector3(30,0,0) );
joint = new OgreNewt::BasicJoints::Hinge( mWorld, m_body1, m_body1, Ogre::Vector3(30,0,0), Ogre::Vector3(0,0,1) );

If you care which of the bodies 'll get the "parent" and which the "child" you need to check this before by some criteria.

ogre_newbie

25-03-2009 14:54:51

Hi melven

Thanks much for your suggestion!

I tried the joint functions, directly using m_body0 & m_body1. It seems that the joint was implemented, but the joint and the balls are made to lose control. They do not move in a controlled and expected manner.

Is it necessary to shut down the physics simulation at the moment that the balls join? I used the joint in createScene(), it worked properly, but when I put it into a callback function, it seems that the joint doesn't work properly. What do you think?

int ballColCallback::userBegin()
{
Ogre::SceneNode* ballNode = m_body0->getOgreNode();
Ogre::Entity* ball = static_cast<Ogre::Entity*> (ballNode->getAttachedObject(0));
Ogre::SceneNode* ballNode2 = m_body1->getOgreNode();
Ogre::Entity* ball2 = static_cast<Ogre::Entity*> (ballNode2->getAttachedObject(0));

if(!strncmp(ball->getName().c_str(),"Entity",6) && !strncmp(ball->getName().c_str(),"Entity",6))
{

joint = new OgreNewt::BasicJoints::BallAndSocket( mWorld, m_body0, m_body1, Ogre::Vector3(30,0,0) );
joint = new OgreNewt::BasicJoints::Hinge( mWorld, m_body1, m_body1, Ogre::Vector3(30,0,0), Ogre::Vector3(0,0,1) );


}
}

melven

25-03-2009 16:28:33

Perhaps it helps if you reset the bodies velocity and rotation (to a reasonable value)... but it's not really a good solution even if it works fine, because the functions for setting velocity, position etc directly are only intended to be used for the initial setup... Have also a look at the newton-forum, you could possibly find there different approaches.

ogre_newbie

26-03-2009 16:06:07

Hi melven

i tried to reset the bodies' velocity to the range from 0 to 10000. The problem still exists.

I think I might need to find another approach to implement the function.

Thanks for your help!