aj1nkya
04-03-2013 15:43:31
Hi! I am having problem with creating collison shape for node.
I have this three functions which creates mesh for ground, pillar and slab of building.
Then i have created createBuilding function which takes as input name of building, number of floors and position of building
first i have used BtOgre::RigidBodyStare(node) but it was giving wrong output because it was taking local position with respect to building node. So used _getDerivedPosition() to get position of node in world.
It works correctly. But node get shifted from shape.
Here are two images
this one is with building positioned at 0,0,0. Output of it is correct. Both node and shape are in correct position.
This one is with building at 400,0,0. Here shape is correctly at 400,0,0 but building node is at 800,0,0.
I dont know why. But i think building node gets shifted twice.
Any idea what is wrong with code or it's other problem?
I have this three functions which creates mesh for ground, pillar and slab of building.
void CSceneEditorView::createPillarMesh()
{
Ogre::ManualObject* lManualObject = NULL;
{
Ogre::String lManualObjectName = "pillar";
lManualObject = m_SceneManager->createManualObject(lManualObjectName);
bool lDoIWantToUpdateItLater = false;
lManualObject->setDynamic(lDoIWantToUpdateItLater);
lManualObject->begin("", Ogre::RenderOperation::OT_TRIANGLE_LIST);
{
float cp = 10.0f;
float cm = -10.0f;
lManualObject->position(cm, 8*cp, -10);// a vertex
lManualObject->position(cp, 8*cp, -10);// a vertex
lManualObject->position(cp, cm, -10);// a vertex
lManualObject->position(cm, cm, -10);// a vertex
lManualObject->position(cm, 8*cp, 10);// a vertex
lManualObject->position(cp, 8*cp, 10);// a vertex
lManualObject->position(cp, cm, 10);// a vertex
lManualObject->position(cm, cm, 10);// a vertex
// face behind / front
lManualObject->triangle(0,1,2);
lManualObject->triangle(2,3,0);
lManualObject->triangle(4,6,5);
lManualObject->triangle(6,4,7);
// face top / down
lManualObject->triangle(0,4,5);
lManualObject->triangle(5,1,0);
lManualObject->triangle(2,6,7);
lManualObject->triangle(7,3,2);
// face left / right
lManualObject->triangle(0,7,4);
lManualObject->triangle(7,0,3);
lManualObject->triangle(1,5,6);
lManualObject->triangle(6,2,1);
}
lManualObject->end();
}
Ogre::String lNameOfTheMesh = "pillar";
Ogre::String lResourceGroup = Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME;
lManualObject->convertToMesh(lNameOfTheMesh);
}
void CSceneEditorView::createSlabMesh()
{
Ogre::ManualObject* lManualObject = NULL;
{
Ogre::String lManualObjectName = "slab";
lManualObject = m_SceneManager->createManualObject(lManualObjectName);
bool lDoIWantToUpdateItLater = false;
lManualObject->setDynamic(lDoIWantToUpdateItLater);
lManualObject->begin("", Ogre::RenderOperation::OT_TRIANGLE_LIST);
{
float cp = 10.0f;
float cm = -10.0f;
lManualObject->position(cm, cp, -10);// a vertex
lManualObject->position(8*cp, cp, -10);// a vertex
lManualObject->position(8*cp, cm, -10);// a vertex
lManualObject->position(cm, cm, -10);// a vertex
lManualObject->position(cm, cp, 80);// a vertex
lManualObject->position(8*cp, cp, 80);// a vertex
lManualObject->position(8*cp, cm, 80);// a vertex
lManualObject->position(cm, cm, 80);// a vertex
// face behind / front
lManualObject->triangle(0,1,2);
lManualObject->triangle(2,3,0);
lManualObject->triangle(4,6,5);
lManualObject->triangle(6,4,7);
// face top / down
lManualObject->triangle(0,4,5);
lManualObject->triangle(5,1,0);
lManualObject->triangle(2,6,7);
lManualObject->triangle(7,3,2);
// face left / right
lManualObject->triangle(0,7,4);
lManualObject->triangle(7,0,3);
lManualObject->triangle(1,5,6);
lManualObject->triangle(6,2,1);
}
lManualObject->end();
}
Ogre::String lNameOfTheMesh = "slab";
Ogre::String lResourceGroup = Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME;
lManualObject->convertToMesh(lNameOfTheMesh);
}
void CSceneEditorView::createFloorMesh()
{
Ogre::ManualObject* lManualObject = NULL;
{
Ogre::String lManualObjectName = "ground";
lManualObject = m_SceneManager->createManualObject(lManualObjectName);
bool lDoIWantToUpdateItLater = false;
lManualObject->setDynamic(lDoIWantToUpdateItLater);
lManualObject->begin("", Ogre::RenderOperation::OT_TRIANGLE_LIST);
{
float cp = 10.0f;
float cm = -10.0f;
lManualObject->position(cm, cp, -10);// a vertex
lManualObject->position(15*cp, cp, -10);// a vertex
lManualObject->position(15*cp, cm, -10);// a vertex
lManualObject->position(cm, cm, -10);// a vertex
lManualObject->position(cm, cp, 150);// a vertex
lManualObject->position(15*cp, cp, 150);// a vertex
lManualObject->position(15*cp, cm, 150);// a vertex
lManualObject->position(cm, cm, 150);// a vertex
// face behind / front
lManualObject->triangle(0,1,2);
lManualObject->triangle(2,3,0);
lManualObject->triangle(4,6,5);
lManualObject->triangle(6,4,7);
// face top / down
lManualObject->triangle(0,4,5);
lManualObject->triangle(5,1,0);
lManualObject->triangle(2,6,7);
lManualObject->triangle(7,3,2);
// face left / right
lManualObject->triangle(0,7,4);
lManualObject->triangle(7,0,3);
lManualObject->triangle(1,5,6);
lManualObject->triangle(6,2,1);
}
lManualObject->end();
}
Ogre::String lNameOfTheMesh = "ground";
Ogre::String lResourceGroup = Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME;
lManualObject->convertToMesh(lNameOfTheMesh);
}
Then i have created createBuilding function which takes as input name of building, number of floors and position of building
void CSceneEditorView::createBuilding(Ogre::String name, int floor, long x,long y,long z)
{ int i = 0, j = 0;
//create building
Ogre::SceneNode *building_node = m_SceneManager->getRootSceneNode()->createChildSceneNode(name, Ogre::Vector3(x,y,z));
//create floor
Ogre::SceneNode *ground_node = building_node->createChildSceneNode(Ogre::String("ground_node_"+Ogre::StringConverter::toString(groundCounter)),(Ogre::Vector3(-30,0,-30)));
Ogre::Entity *ground_entity = m_SceneManager->createEntity(Ogre::String("ground_entity_"+Ogre::StringConverter::toString(groundCounter)), "ground");
ground_node->attachObject(ground_entity);
groundCounter++;
//Create the ground shape.
BtOgre::StaticMeshToShapeConverter converter1(ground_entity);
mGroundShape[groundCounter-1] = converter1.createTrimesh();
//Create BtOgre MotionState (connects Ogre and Bullet).
groundState[groundCounter-1] = new btDefaultMotionState(btTransform(btQuaternion(0,0,0,1),BtOgre::Convert::toBullet(ground_node->_getDerivedPosition())));
//Create the Body.
mGroundBody[groundCounter-1] = new btRigidBody(0, groundState[groundCounter-1], mGroundShape[groundCounter-1], btVector3(0,0,0));
Globals::phyWorld->addRigidBody(mGroundBody[groundCounter-1]);
//mass of pillar
btScalar mass = 5;
btVector3 inertia;
for(int k=0; k<floor; k++)
{ //first pillar
Ogre::SceneNode *pillar_node_1 = building_node->createChildSceneNode(Ogre::String("pillar_node_"+Ogre::StringConverter::toString(counter)),((Ogre::Vector3(0,20,0))+k*Ogre::Vector3(0,110,0)));//,Ogre::Quaternion::IDENTITY);
Ogre::Entity *pillar_entity_1 = m_SceneManager->createEntity(Ogre::String("pillar_entity_"+Ogre::StringConverter::toString(counter)), "pillar");
pillar_node_1->attachObject(pillar_entity_1);
counter++;
//Create shape.
BtOgre::StaticMeshToShapeConverter converter2(pillar_entity_1);
mPillarShape[groundCounter-1] = converter2.createConvex();
//Calculate inertia.
btScalar mass = 5;
btVector3 inertia;
mPillarShape[groundCounter-1]->calculateLocalInertia(mass, inertia);
//Create BtOgre MotionState (connects Ogre and Bullet).
mPillarState[groundCounter-1] = new BtOgre::RigidBodyState(pillar_node_1,btTransform(btQuaternion(0,0,0,1),BtOgre::Convert::toBullet(pillar_node_1->_getDerivedPosition())));
mPillarState;
//Create the Body.
mPillarBody[groundCounter-1] = new btRigidBody(mass, mPillarState[groundCounter-1] , mPillarShape[groundCounter-1], inertia);
Globals::phyWorld->addRigidBody(mPillarBody[groundCounter-1]);
i++;
//second pillar
Ogre::SceneNode *pillar_node_2 = building_node->createChildSceneNode(Ogre::String("pillar_node_"+Ogre::StringConverter::toString(counter)), (Ogre::Vector3(70,20,0)+k*Ogre::Vector3(0,110,0)));
Ogre::Entity *pillar_entity_2 = m_SceneManager->createEntity(Ogre::String("pillar_entity_"+Ogre::StringConverter::toString(counter)), "pillar");
pillar_node_2->attachObject(pillar_entity_2);
counter++;
//Create shape.
BtOgre::StaticMeshToShapeConverter converter3(pillar_entity_2);
mPillarShape[groundCounter-1] = converter3.createConvex();
//Calculate inertia.
mPillarShape[groundCounter-1]->calculateLocalInertia(mass, inertia);
//Create BtOgre MotionState (connects Ogre and Bullet).
mPillarState[groundCounter-1] = new BtOgre::RigidBodyState(pillar_node_2,btTransform(btQuaternion(0,0,0,1),BtOgre::Convert::toBullet(pillar_node_2->_getDerivedPosition())));
//Create the Body.
mPillarBody[groundCounter-1] = new btRigidBody(mass, mPillarState[groundCounter-1], mPillarShape[groundCounter-1], inertia);
Globals::phyWorld->addRigidBody(mPillarBody[groundCounter-1]);
i++;
//third pillar
Ogre::SceneNode *pillar_node_3 = building_node->createChildSceneNode(Ogre::String("pillar_node_"+Ogre::StringConverter::toString(counter)), (Ogre::Vector3(0,20,70)+k*Ogre::Vector3(0,110,0)));
Ogre::Entity *pillar_entity_3 = m_SceneManager->createEntity(Ogre::String("pillar_entity_"+Ogre::StringConverter::toString(counter)), "pillar");
pillar_node_3->attachObject(pillar_entity_3);
counter++;
//Create shape.
BtOgre::StaticMeshToShapeConverter converter4(pillar_entity_3);
mPillarShape[groundCounter-1] = converter4.createConvex();
//Calculate inertia.
mPillarShape[groundCounter-1]->calculateLocalInertia(mass, inertia);
//Create BtOgre MotionState (connects Ogre and Bullet).
mPillarState[groundCounter-1] = new BtOgre::RigidBodyState(pillar_node_3,btTransform(btQuaternion(0,0,0,1),BtOgre::Convert::toBullet(pillar_node_3->_getDerivedPosition())));
//Create the Body.
mPillarBody[groundCounter-1] = new btRigidBody(mass, mPillarState[groundCounter-1], mPillarShape[groundCounter-1], inertia);
Globals::phyWorld->addRigidBody(mPillarBody[groundCounter-1]);
i++;
//fourth pillar
Ogre::SceneNode *pillar_node_4 = building_node->createChildSceneNode(Ogre::String("pillar_node_"+Ogre::StringConverter::toString(counter)), (Ogre::Vector3(70,20,70)+k*Ogre::Vector3(0,110,0)));
Ogre::Entity *pillar_entity_4 = m_SceneManager->createEntity(Ogre::String("pillar_entity_"+Ogre::StringConverter::toString(counter)), "pillar");
pillar_node_4->attachObject(pillar_entity_4);
counter++;
//Create shape.
BtOgre::StaticMeshToShapeConverter converter5(pillar_entity_4);
mPillarShape[groundCounter-1] = converter5.createConvex();
//Calculate inertia.
mPillarShape[groundCounter-1]->calculateLocalInertia(mass, inertia);
//Create BtOgre MotionState (connects Ogre and Bullet).
mPillarState[groundCounter-1] = new BtOgre::RigidBodyState(pillar_node_4,btTransform(btQuaternion(0,0,0,1),BtOgre::Convert::toBullet(pillar_node_4->_getDerivedPosition())));
//Create the Body.
mPillarBody[groundCounter-1] = new btRigidBody(mass, mPillarState[groundCounter-1], mPillarShape[groundCounter-1], inertia);
Globals::phyWorld->addRigidBody(mPillarBody[groundCounter-1]);
i++;
//slab
Ogre::SceneNode *slab_node = building_node->createChildSceneNode(Ogre::String("slab_node_"+Ogre::StringConverter::toString(counter)), (Ogre::Vector3(0,110,0)+k*Ogre::Vector3(0,110,0)));
Ogre::Entity *slab_entity = m_SceneManager->createEntity(Ogre::String("slab_entity_"+Ogre::StringConverter::toString(counter)),"slab");
slab_node->attachObject(slab_entity);
counter++;
//Create shape.
BtOgre::StaticMeshToShapeConverter converter(slab_entity);
mSlabShape[groundCounter-1][j] = converter.createConvex();
//Calculate inertia.
btScalar mass1 = 10;
btVector3 inertia1;
mSlabShape[groundCounter-1][j]->calculateLocalInertia(mass1, inertia1);
//Create BtOgre MotionState (connects Ogre and Bullet).
mSlabState[groundCounter-1][j] = new BtOgre::RigidBodyState(slab_node,btTransform(btQuaternion(0,0,0,1),BtOgre::Convert::toBullet(slab_node->_getDerivedPosition())));
//Create the Body.
mSlabBody[groundCounter-1][j] = new btRigidBody(mass, mSlabState[groundCounter-1][j], mSlabShape[groundCounter-1][j], inertia1);
Globals::phyWorld->addRigidBody(mSlabBody[groundCounter-1][j]);
j++;
}
}
first i have used BtOgre::RigidBodyStare(node) but it was giving wrong output because it was taking local position with respect to building node. So used _getDerivedPosition() to get position of node in world.
It works correctly. But node get shifted from shape.
Here are two images
this one is with building positioned at 0,0,0. Output of it is correct. Both node and shape are in correct position.
This one is with building at 400,0,0. Here shape is correctly at 400,0,0 but building node is at 800,0,0.
I dont know why. But i think building node gets shifted twice.
Any idea what is wrong with code or it's other problem?