leakingpear
16-02-2007 01:33:30
I've been trying to integrate OgreNewt into my group's game engine and i've hit a few problems, we're using oFusion and the OSM loader with a custom callback reading in physics data added in the scene file. Anyhoo it loads a scene fine from what I can see when debugging, it's creating the right objects as I step through the code, only when it's running it all goes wrong.
In a test scene I positioned a series of spheres above a cube, the spheres are ellipsoids and the cube is a CollisionTree, the spheres should fall to the cube and bounce off or atleast land. The problem is that when it's actually running the spheres disappear for about a second then suddenly appear under the cube in a stream and move slowly downwards indicating that gravity is working in some respect.
Anyway here's the relevant code, this first part is where the OgreNewt world is created and initialised:
and the second part is the code for reading in the physics data through the OSMCallbacks interface:
Anyway if anyone can please help me find out where i'm going wrong i'll be indebted to you, getting any of the physics packages to work has been a real problem for some reason but OgreNewt seems to be working atleast in some respect.
In a test scene I positioned a series of spheres above a cube, the spheres are ellipsoids and the cube is a CollisionTree, the spheres should fall to the cube and bounce off or atleast land. The problem is that when it's actually running the spheres disappear for about a second then suddenly appear under the cube in a stream and move slowly downwards indicating that gravity is working in some respect.
Anyway here's the relevant code, this first part is where the OgreNewt world is created and initialised:
LOG_MESSAGE("OgreNewt: Initializing, creating world.");
m_pNewtWorld = new OgreNewt::World();
if(m_pNewtWorld == NULL)
{
throw new COgreNewtPhysicsException(CException::eCritical, "OgreNewt: World failed creation for unknown reason.");
return;
}
// Register OgreNewt frame listener //
m_pOgreNewtListener = new OgreNewt::BasicFrameListener(CGameEngine::getSingleton()->getGraphicsComponent()->getRenderWindow(),
CGameEngine::getSingleton()->getGraphicsComponent()->getSceneManager(),
m_pNewtWorld);
Ogre::Real size = 10000;
m_pNewtWorld->setWorldSize(Ogre::Vector3(-size, -size, -size), Ogre::Vector3(size, size, size));
m_update = 1.0f / 80.0f;
Ogre::Root::getSingleton().addFrameListener(m_pOgreNewtListener);
and the second part is the code for reading in the physics data through the OSMCallbacks interface:
OgreNewt::Collision* pCol = NULL;
Ogre::Real density = 1.0f;
OgreNewt::World* pNewtWorld = NULL;
pNewtWorld = CGameEngine::getSingleton()->getPhysicsComponent()->getNewtWorld();
Ogre::SceneNode* pSceneNode = pEntity->getParentSceneNode();
TiXmlElement* physElem = pEntityDesc->FirstChildElement("physics");
if(!physElem)
{
throw new COgreNewtPhysicsException(CException::eFixable, "OgreNewt: Physics data not found for entity: " + pEntity->getName());
return;
}
bool isStatic = false;
const std::string* moveType = new std::string(physElem->Attribute("type"));
if(moveType)
{
if(*moveType == "static")
isStatic = true;
else if (*moveType == "dynamic")
isStatic = false;
else
{
throw new COgreNewtPhysicsException(CException::eFixable, "OgreNewt: Movement type data not found for entity: " + pEntity->getName());
return;
}
delete moveType;
}
float mass = 0.0f;
if(!isStatic)
physElem->QueryFloatAttribute("mass", &mass);
Ogre::Vector3 inertia = Ogre::Vector3::ZERO;
const std::string* bodyType = new std::string(physElem->Attribute("body"));
if(bodyType)
{
if((*bodyType) == "tree")
{
if(isStatic)
pCol = new OgreNewt::CollisionPrimitives::TreeCollision(pNewtWorld, pSceneNode, true);
else
{
throw new COgreNewtPhysicsException(CException::eFixable, "OgreNewt: Tree body type cannot be used with dynamic objects: " + pEntity->getName());
return;
}
}
else if((*bodyType) == "cube")
{
Ogre::Vector3 size(0, 0, 0);
physElem->QueryFloatAttribute("sizex", &size.x);
physElem->QueryFloatAttribute("sizey", &size.y);
physElem->QueryFloatAttribute("sizez", &size.z);
if(size != Ogre::Vector3::ZERO)
{
pCol = new OgreNewt::CollisionPrimitives::Box(pNewtWorld, size);
inertia = OgreNewt::MomentOfInertia::CalcBoxSolid(mass, size);
}
else
{
throw new COgreNewtPhysicsException(CException::eFixable, "OgreNewt: Cube body type chosen without supplying a size: " + pEntity->getName());
return;
}
}
else if((*bodyType) == "ellipsoid")
{
Ogre::Vector3 size(0, 0, 0);
physElem->QueryFloatAttribute("sizex", &size.x);
physElem->QueryFloatAttribute("sizey", &size.y);
physElem->QueryFloatAttribute("sizez", &size.z);
if(size != Ogre::Vector3::ZERO)
{
pCol = new OgreNewt::CollisionPrimitives::Ellipsoid(pNewtWorld, size);
inertia = OgreNewt::MomentOfInertia::CalcEllipsoidSolid(mass, size);
}
else
{
throw new COgreNewtPhysicsException(CException::eFixable, "OgreNewt: Ellipsoid body type chosen without supplying a size: " + pEntity->getName());
return;
}
}
else if((*bodyType) == "capsule")
{
float radius = 0, height = 0;
physElem->QueryFloatAttribute("radius", &radius);
physElem->QueryFloatAttribute("height", &height);
if(radius && height)
{
pCol = new OgreNewt::CollisionPrimitives::Capsule(pNewtWorld, radius, height);
inertia = OgreNewt::MomentOfInertia::CalcCylinderSolid(mass, radius, height);
}
else
{
throw new COgreNewtPhysicsException(CException::eFixable, "OgreNewt: Capsule body type chosen without supplying a radius and/or height: " + pEntity->getName());
return;
}
}
else
{
throw new COgreNewtPhysicsException(CException::eFixable, "OgreNewt: Body type not expected, entity: " + pEntity->getName());
return;
}
delete bodyType;
}
OgreNewt::Body *pBody = new OgreNewt::Body(pNewtWorld, pCol);
pBody->attachToNode(pSceneNode);
pBody->setStandardForceCallback(); //Standard gravity callback, good for objects that will just fall down, later I should
//incorporate the callback type into the XML.
pBody->setMassMatrix(mass, inertia);
CGameEngine::getSingleton()->getPhysicsComponent()->addBody(pBody);
delete pCol;
Anyway if anyone can please help me find out where i'm going wrong i'll be indebted to you, getting any of the physics packages to work has been a real problem for some reason but OgreNewt seems to be working atleast in some respect.