babyken
07-03-2010 05:13:27
I am new to ogre newton.
And I am working dominos for learning ogre newton.
however, it seems to be no force can act on the node in the program. I am trying to click on a domino so that affect it orientation and it will fall down and hit other domino.
But it looks like no gravity. I am not quite sure. Because the dominos looks like have force properties. However they won't fall down(sometime they do) evening i rotate the ground!!
I hope someone can give me some hints on how to implement it.
Thanks a lot
Ken
And I am working dominos for learning ogre newton.
however, it seems to be no force can act on the node in the program. I am trying to click on a domino so that affect it orientation and it will fall down and hit other domino.
But it looks like no gravity. I am not quite sure. Because the dominos looks like have force properties. However they won't fall down(sometime they do) evening i rotate the ground!!
I hope someone can give me some hints on how to implement it.
Thanks a lot
Ken
LabPhysics_CEGUIApp::LabPhysics_CEGUIApp(void)
{
}
//-------------------------------------------------------------------------------------
LabPhysics_CEGUIApp::~LabPhysics_CEGUIApp(void)
{
delete mWorld;
OgreNewt::Debugger::getSingleton().deInit();
}
//-------------------------------------------------------------------------------------
void LabPhysics_CEGUIApp::createScene(void)
{
// setup GUI system
mGUIRenderer = new CEGUI::OgreCEGUIRenderer(mWindow,
Ogre::RENDER_QUEUE_OVERLAY, false, 3000, mSceneMgr);
mGUISystem = new CEGUI::System(mGUIRenderer);
CEGUI::Logger::getSingleton().setLoggingLevel(CEGUI::Informative);
// load scheme and set up defaults
CEGUI::SchemeManager::getSingleton().loadScheme(
(CEGUI::utf8*)"TaharezLookSkin.scheme");
mGUISystem->setDefaultMouseCursor(
(CEGUI::utf8*)"TaharezLook", (CEGUI::utf8*)"MouseArrow");
mGUISystem->setDefaultFont((CEGUI::utf8*)"BlueHighway-12");
CEGUI::MouseCursor::getSingleton().setImage("TaharezLook", "MouseArrow");
CEGUI::MouseCursor::getSingleton().show( );
setupEventHandlers();
// Set ambient light
mSceneMgr->setAmbientLight(ColourValue(0.5, 0.5, 0.5));
// Create a light
Light* l = mSceneMgr->createLight("MainLight");
l->setPosition(20,200,-50);
//Create newton world
mWorld = new OgreNewt::World();
mWorld->setWorldSize(Vector3(-200, -200, -200), Vector3(3300, 500, 3300));
Entity* box = mSceneMgr->createEntity("box", "box.mesh");
SceneNode* boxNode = mSceneMgr->getRootSceneNode()->createChildSceneNode("BoxNode");
boxNode->attachObject(box);
Entity* ground = mSceneMgr->createEntity("ground", "Ground.mesh");
SceneNode* groundNode = mSceneMgr->getRootSceneNode()->createChildSceneNode("GroundNode");
groundNode->attachObject(ground);
box->setMaterialName("box_material");
ground->setMaterialName("ground_material");
Vector3 size = Vector3(1,1,1);
OgreNewt::Collision* col = new OgreNewt::CollisionPrimitives::Box( mWorld, size );
OgreNewt::Body* Box_body = new OgreNewt::Body( mWorld, col );
delete col;
Box_body->attachToNode(boxNode); // Attach the body to scene node
col = new OgreNewt::CollisionPrimitives::TreeCollision( mWorld, groundNode, true );
OgreNewt::Body* Ground_body = new OgreNewt::Body( mWorld, col );
delete col;
Ground_body->attachToNode(groundNode);
Ground_body->setPositionOrientation(Vector3(0, 0, 0), Quaternion::IDENTITY);
Real mass = size.length();
Vector3 inertia = OgreNewt::MomentOfInertia::CalcBoxSolid( mass, size );
boxNode->setScale(size);
Box_body->setPositionOrientation(Vector3(0,50,0),Quaternion::IDENTITY);
Box_body->setMassMatrix(mass, inertia);
Box_body->setStandardForceCallback();
const OgreNewt::MaterialID* defaulted = mWorld->getDefaultMaterialID();
Box_body->setMaterialGroupID(defaulted);
OgreNewt::MaterialID* ground_material_id = new OgreNewt::MaterialID(mWorld);
Ground_body->setMaterialGroupID(ground_material_id);
OgreNewt::MaterialPair* pair1 = new OgreNewt::MaterialPair(mWorld, defaulted, defaulted);
OgreNewt::MaterialPair* pair2 = new OgreNewt::MaterialPair(mWorld, defaulted,ground_material_id );
pair2->setDefaultElasticity(0.5f);
pair2->setDefaultFriction(0.1,0.05);
pair2->setDefaultSoftness(0.5);
BoxContactCallback* mBoxCallback = new BoxContactCallback();
pair2->setContactCallback(mBoxCallback);
//start another box
/*Create a body (using box.mesh with the material named “box_material”) and
the variable name is “child” located at (0,3,0) with size 0.5x2.0x0.1. The mass
is the length of its size. Remember that a collider and standard force callback
should be applied to this body*/
//Entity* box2 = mSceneMgr->createEntity("box2", "box.mesh");
//SceneNode* box2Node = mSceneMgr->getRootSceneNode()->createChildSceneNode("Bo2xNode");
//box2Node->attachObject(box2);
//box->setMaterialName("box2_material");
//Vector3 newSize = Vector3(0.5, 2.0, 0.1);
//col = new OgreNewt::CollisionPrimitives::Box( mWorld, newSize );
//OgreNewt::Body* child = new OgreNewt::Body( mWorld, col );
//delete col;
//child->attachToNode(box2Node); // Attach the body to scene node
//child->setPositionOrientation(Vector3(0, 3, 0), Ogre::Quaternion::IDENTITY);
//Vector3 pos = Vector3(0,3, -5);
//OgreNewt::Joint* joint = new OgreNewt::BasicJoints::BallAndSocket(mWorld, child, NULL, pos);
//end another box
//try to create a straight line of box first
Vector3 boxSize = Vector3(0.5, 1.5, 0.2);
mass = boxSize.length();
inertia = OgreNewt::MomentOfInertia::CalcBoxSolid( mass, boxSize );
Ogre::Vector3 offset;
Entity* boxArray[20];
SceneNode* boxArrayNode[20];
OgreNewt::Body* BoxArray_body[20];
for(int i=0; i<20; i++)
{
boxArray[i] = mSceneMgr->createEntity("Box"+StringConverter::toString(i), "box.mesh");
boxArray[i]->setMaterialName("box_material");
boxArrayNode[i] = mSceneMgr->getRootSceneNode()->createChildSceneNode("Box"+StringConverter::toString(i)+"Node");
boxArrayNode[i]->attachObject(boxArray[i]);
//boxArrayNode[i]->setPosition(Vector3(0, 0, i));
boxArrayNode[i]->setScale(boxSize);
col = new OgreNewt::CollisionPrimitives::Box( mWorld, boxSize );
BoxArray_body[i] = new OgreNewt::Body( mWorld, col );
delete col;
BoxArray_body[i]->attachToNode(boxArrayNode[i]); // Attach the body to scene node
//BoxArray_body[i]->setMaterialGroupID(defaulted);
BoxArray_body[i]->setMassMatrix(mass, inertia);
BoxArray_body[i]->setStandardForceCallback();
BoxArray_body[i]->setPositionOrientation(Vector3(0, 2, 0-i*0.8), Quaternion::IDENTITY);
}
}
//override createFrameListener
void LabPhysics_CEGUIApp::createFrameListener()
{
Ogre::FrameListener* mNewtonListener;
mNewtonListener = new OgreNewt::BasicFrameListener( mWindow, mSceneMgr, mWorld);
mRoot->addFrameListener(mNewtonListener);
BaseApplication::createFrameListener();
}
bool LabPhysics_CEGUIApp::frameStarted(const FrameEvent& evt)
{
CEGUI::Point mousePos = CEGUI::MouseCursor::getSingleton().getPosition();
Real mx = mousePos.d_x / CEGUI::System::getSingleton().getRenderer()->getWidth();
Real my = mousePos.d_y / CEGUI::System::getSingleton().getRenderer()->getHeight();
Ray mouseRay = mCamera->getCameraToViewportRay( mx,my );
OgreNewt::BasicRaycast* ray = new OgreNewt::BasicRaycast( mWorld, mouseRay.getOrigin(),
mouseRay.getPoint(100));
OgreNewt::BasicRaycast::BasicRaycastInfo info = ray->getFirstHit();
OgreNewt::Body* body = info.mBody;
Real distance = info.mDistance;
if( mMouse->getMouseState().buttonDown(OIS::MouseButtonID::MB_Left) && info.mBody )
{
SceneNode* pick = (SceneNode*)body->getOgreNode();
//pick->showBoundingBox(true);
//pick->setVisible(false);
pick->pitch(Degree(-30));
body->setPositionOrientation(pick->getPosition(), pick->getOrientation());
}
return BaseApplication::frameStarted(evt);
}