Bizzare problems

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:


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.

leakingpear

19-02-2007 03:21:02

If anyone is out there I really need help, i've tried everything I can think of, some of it appears working so I can't see what i've done wrong :(

walaber

19-02-2007 04:49:25

can you please post the code where you update the OgreNewt::World?

leakingpear

19-02-2007 14:56:00

It uses the OgreNewt::BasicFrameListener to update, i've tried updating at a constant rate (0.0125) as well as doing the update time based off time between frames and so forth. At the moment it's just using the default update_framerate of 60, I don't imagine it would update at all if the frame listener wasn't working (there's no other updates to the world) and considering the BasicFrameListener works well for everything else I don't think there's anything wrong there unless i'm doing something wrong with adding the frame listener.

walaber

19-02-2007 17:50:41

if you spawn the spheres up higher, do they properly collide with the terrain after the "stall"?

leakingpear

19-02-2007 18:18:03

Nope, same thing happens, exact same thing, in that all the balls came out at a stream even though one was significantly higher than the others, if it helps here's the scene OSM file:


<oe_scene>
<sceneManager type="1" />
<bkgcolor r="0" g="0" b="0" />
<lightColor r="0.5" g="0.5" b="0.5" />
<shadowTechnique type="0" tex_size="512" tex_count="1">
<color r="0" g="0" b="0" />
</shadowTechnique>
<entities>
<entity name="Sphere01" hidden="false" filename="Sphere01.mesh" CastShadows="yes" ReceiveShadows="yes">
<position x="0" y="27.4021" z="-0" />
<rotation x="-0.707107" y="0" z="-0" w="-0.707107" />
<scale x="0.258166" y="0.258166" z="0.258166" />
<physics type="dynamic" body="ellipsoid" sizex="0.5" sizey="0.5" sizez="0.5" mass="2.0" />
</entity>
<entity name="Sphere02" hidden="false" filename="Sphere02.mesh" CastShadows="yes" ReceiveShadows="yes">
<position x="0" y="200.5358" z="-0" />
<rotation x="-0.707107" y="0" z="-0" w="-0.707107" />
<scale x="0.258166" y="0.258166" z="0.258166" />
<physics type="dynamic" body="ellipsoid" sizex="0.5" sizey="0.5" sizez="0.5" mass="2.0" />
</entity>
<entity name="Sphere03" hidden="false" filename="Sphere03.mesh" CastShadows="yes" ReceiveShadows="yes">
<position x="0" y="47.3" z="-0" />
<rotation x="-0.707107" y="0" z="-0" w="-0.707107" />
<scale x="0.258166" y="0.258166" z="0.258166" />
<physics type="dynamic" body="ellipsoid" sizex="0.5" sizey="0.5" sizez="0.5" mass="2.0" />
</entity>
<entity name="Box01" hidden="false" filename="Box01.mesh" CastShadows="yes" ReceiveShadows="yes">
<position x="0" y="0" z="-0" />
<rotation x="0" y="0" z="-0" w="-1" />
<scale x="0.0222823" y="0.0222823" z="0.0222823" />
<physics type="static" body="tree" />
</entity>
</entities>
<lights>
<light name="Omni01" type="omni" on="true" CastShadows="yes" intensity="0.47">
<position x="783.728" y="801.154" z="-395.216" />
<rotation x="-0.707107" y="0" z="-0" w="-0.707107" />
<scale x="1" y="1" z="1" />
<color r="0.47" g="0.47" b="0.47" />
<specular r="0.47" g="0.47" b="0.47" />
</light>
<light name="Omni02" type="omni" on="true" CastShadows="yes" intensity="0.52">
<position x="-751.973" y="801.154" z="-395.216" />
<rotation x="-0.707107" y="0" z="-0" w="-0.707107" />
<scale x="1" y="1" z="1" />
<color r="0.52" g="0.52" b="0.52" />
<specular r="0.52" g="0.52" b="0.52" />
</light>
<light name="Omni03" type="omni" on="true" CastShadows="yes" intensity="0.22">
<position x="-20.2221" y="801.154" z="949.977" />
<rotation x="-0.707107" y="0" z="-0" w="-0.707107" />
<scale x="1" y="1" z="1" />
<color r="0.22" g="0.22" b="0.22" />
<specular r="0.22" g="0.22" b="0.22" />
</light>
</lights>
<cameras>
<camera name="Camera01" FOV="0.785398">
<position x="460.722" y="231.939" z="-90.5682" />
<rotation x="-0.343815" y="-0.649411" z="0.417982" w="-0.534179" />
<scale x="1" y="1" z="1" />
<target name="Camera01.Target">
<position x="0" y="18.2871" z="-0" />
<rotation x="-0.17235" y="-0.702269" z="0.181619" w="-0.666429" />
<scale x="0.258166" y="0.258166" z="0.258166" />
</target>
</camera>
</cameras>
</oe_scene>



Thanks for all the help so far!

walaber

19-02-2007 18:25:57

try changing the "optimize" bool on the TreeCollision to false really quick, and let me know if the same thing still happens.

leakingpear

19-02-2007 19:06:20

Same problem, couldn't see any noticable difference :(

walaber

19-02-2007 21:26:39

I assume pressing F3 shows that the collision is lining up properly with all of the visual objects?

leakingpear

19-02-2007 21:55:21

Doing that made me realise the ellipsoids were 10x too small, but after changing the sizes to 5.0 fixes it so the debug lines match up, but the problem with the falling still persists.

leakingpear

20-02-2007 18:33:14

Wooo! Got it working, thanks for all the help Walaber, it turns out I was forgetting to update the body's position and orientation from the data loaded into the scene node.

walaber

20-02-2007 18:49:05

great news!!