Rotate error in Walking Character Tut

gnuser

19-11-2009 02:22:44

Hi all:

I just start use OGREODE, and see the tut from this link :
http://www.ogre3d.org/wiki/index.php/Og ... _Character
and
http://www.ogre3d.org/wiki/index.php/Ni ... _Character

From the tut , it uses 2 SceneNode to "get the right relation between the mesh and the body position".

So I modified the code, but when I rotate the Ninja, it give me an assert error, after search many pages, I still can't solve this problem, anyone know why?


#ifndef _SIMPLESCENES_NINJA_H_
#define _SIMPLESCENES_NINJA_H_

class SimpleScenes_Ninja:public SimpleScenes
{
public:
SimpleScenes_Ninja(OgreOde::World *world) : SimpleScenes(world)
{
Entity* hero_ent = _mgr->createEntity("ninja","ninja.mesh");
SceneNode* hero_node = _mgr->getRootSceneNode()->createChildSceneNode("ninja");
SceneNode* modelNode = hero_node->createChildSceneNode("ninja_model");
modelNode->attachObject(hero_ent);
hero_node->scale(0.01,0.01,0.01);

// init feet radius
AxisAlignedBox aab = modelNode->getAttachedObject("ninja")->getBoundingBox();
Ogre::Vector3 min = aab.getMinimum()*hero_node->getScale();
Ogre::Vector3 max = aab.getMaximum()*hero_node->getScale();
Ogre::Vector3 center = aab.getCenter()*hero_node->getScale();
Ogre::Vector3 size(fabs(max.x-min.x),fabs(max.y-min.y),fabs(max.z-min.z));
float radius = (size.x>size.z)?size.z/2.0f:size.x/2.0f;

OgreOde::Body* dollFeetBody = new OgreOde::Body(_world,"feet");
dollFeetBody->setMass(OgreOde::SphereMass(70*2.5,radius));
OgreOde::SphereGeometry* feetGeom = new OgreOde::SphereGeometry(radius,_world);
OgreOde::TransformGeometry* feetTrans = new OgreOde::TransformGeometry(_world,_space);
modelNode->translate(Vector3(0,-radius/hero_node->getScale().y,0));

feetTrans->setBody(dollFeetBody);
feetTrans->setEncapsulatedGeometry(feetGeom);
hero_node->attachObject(dollFeetBody);
_feet = dollFeetBody;
_bodies.push_back(_feet);
_geoms.push_back(_feet->getGeometry(0));

OgreOde::Body* dollTorsoBody = new OgreOde::Body(_world,"torso");
dollTorsoBody->setMass(OgreOde::CapsuleMass(70*2.5,radius,Vector3::UNIT_Y,radius));
dollTorsoBody->setAffectedByGravity(false);
dollTorsoBody->setDamping(0,50000);
OgreOde::TransformGeometry* torsoTrans = new OgreOde::TransformGeometry(_world,_space);
OgreOde::CapsuleGeometry* torsoGeom = new OgreOde::CapsuleGeometry(radius,size.y-4*radius,_world);
torsoGeom->setPosition(Ogre::Vector3(0,size.y-((size.y-4*radius)/2+2*radius),0));
torsoGeom->setOrientation(Quaternion(Degree(90),Vector3::UNIT_X));
torsoTrans->setBody(dollTorsoBody);
torsoTrans->setEncapsulatedGeometry(torsoGeom);
hero_node->attachObject(dollTorsoBody);
_torso = dollTorsoBody;
_bodies.push_back(_torso);
_geoms.push_back(_torso->getGeometry(0));

OgreOde::HingeJoint* joint = new OgreOde::HingeJoint(_world);
joint->attach(dollTorsoBody,dollFeetBody);
joint->setAxis(Ogre::Vector3::UNIT_X);
_joints.push_back(joint);
}

virtual ~SimpleScenes_Ninja(){}

virtual void frameEnded(Real time, OIS::Keyboard* input, OIS::Mouse* mouse)
{
SimpleScenes::frameEnded(time, input, mouse);

_ninja_rotate = 0;
if (input->isKeyDown(KC_J))
_ninja_rotate += -1;
if (input->isKeyDown(KC_L))
_ninja_rotate += 1;

_ninja_thrust = 0;
if (input->isKeyDown(KC_I))
_ninja_thrust += -1;
if (input->isKeyDown(KC_K))
_ninja_thrust += 1;

if (_ninja_rotate == 0)
{
_feet->wake();
_feet->setAngularVelocity(Vector3(_feet->getAngularVelocity().x,0,0));
}
else
{
_feet->wake();
Quaternion q1 = _torso->getOrientation();
Quaternion q2(Degree(-4*_ninja_rotate),Ogre::Vector3::UNIT_Y);
_torso->setOrientation(q1*q2);
}

if (_ninja_thrust == 0)
{
_feet->wake();
_feet->setLinearVelocity(Vector3(0,_feet->getLinearVelocity().y,0));
_feet->setAngularVelocity(Vector3(0,_feet->getAngularVelocity().y,0));
}
else
{
float speed = 30;
_feet->wake();
Quaternion q = _torso->getOrientation();
_feet->setAngularVelocity(q*Ogre::Vector3(speed*_ninja_thrust*cos(1.0),_feet->getAngularVelocity().y,0));
}

Quaternion q = _torso->getOrientation();
Vector3 x = q.xAxis();
Vector3 y = q.yAxis();
Vector3 z = q.zAxis();
_torso->wake();
_torso->setOrientation(Quaternion(x,Vector3::UNIT_Y,z));
_torso->setAngularVelocity(Vector3(0,0,0));

OgreOde::Body* body = 0;
if (input->isKeyDown(KC_Z))
body = createRandomObject(OgreOde::Geometry::Class_Sphere);
else if (input->isKeyDown(KC_X))
body = createRandomObject(OgreOde::Geometry::Class_Box);
else if (input->isKeyDown(KC_C))
body = createRandomObject(OgreOde::Geometry::Class_Capsule);
else if (input->isKeyDown(KC_T))
body = createRandomObject(OgreOde::Geometry::Class_TriangleMesh);
}

virtual const String& getName()
{
static String name = "Ninja Walking Character";
return name;
}

virtual const String& getKeys()
{
static String keys = "IJKL - move character Z-sphere X-box C-capsule T-trimesh";
return keys;
}

protected:
OgreOde::Body *_hero;
OgreOde::Body *_feet;
OgreOde::Body *_torso;
int _ninja_rotate,_ninja_thrust;
};

#endif

shanefarris

19-11-2009 16:54:04

No sure myself, but have you checked out this post:
viewtopic.php?f=7&t=7253