character walking (ray/capsule) problem

smewemek

14-04-2010 00:57:36

Hello, I'm using OgreODE for a personal project, a game and I have some problems with control of the character.

Comment I am Spanish and use the Google translator, so sorry for the quality of the text.

Now I'm watching the main character's collision with the environment, my idea is to use a ray / capsule, such as this picture:



With the lightning I have no problem, works fine.

My problem is to move the character with the physical body. Then do a summary of the process:

1. Create nodes, bodies and geometries:


Entity* ninja_ent = _mgr->createEntity("ninja","ninja.mesh");
ninja_node = _mgr->getRootSceneNode()->createChildSceneNode("ninja");
model_node = ninja_node->createChildSceneNode("ninja_model");
model_node->attachObject(ninja_ent);
ninja_node->scale(1,1,1);
ninja_node->setPosition(Vector3(200,630,2500));

AxisAlignedBox aab = model_node->getAttachedObject("ninja")->getBoundingBox();
Ogre::Vector3 min = aab.getMinimum()*ninja_node->getScale();
Ogre::Vector3 max = aab.getMaximum()*ninja_node->getScale();
Ogre::Vector3 center = aab.getCenter()*ninja_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* dollTorsoBody = new OgreOde::Body(_world,"torso");
dollTorsoBody->setMass(OgreOde::CapsuleMass(70*2.5,radius,Vector3::UNIT_Y,radius));
dollTorsoBody->setAffectedByGravity(true);
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);
ninja_node->attachObject(dollTorsoBody);
_torso = dollTorsoBody;
_bodies.push_back(_torso);
_geoms.push_back(_torso->getGeometry(0));


2. Move the physical and visual body across the stage: Here is where I have trouble really.

I can not move the capsule as coordinates x,y allowing this collides with the objects of the stage.


Vector3 direction;
if( input->isKeyDown(OIS::KC_W) )
{
direction = _torso->getOrientation() * Ogre::Vector3::NEGATIVE_UNIT_Z;
//Move Physic
_torso->setPosition(direction);
//Move Visual
ninja_node->translate(_torso->getPosition());
}
if( input->isKeyDown(OIS::KC_S) )
{
direction = _torso->getOrientation() * Ogre::Vector3::UNIT_Z;
//Move Physic
_torso->setPosition(direction);
//Move Visual
ninja_node->translate(_torso->getPosition());
}
if( input->isKeyDown(OIS::KC_A) )
{
Quaternion q1 = _torso->getOrientation();
Quaternion q2(Degree(-4),Ogre::Vector3::UNIT_Y);
_torso->setOrientation(q1*q2);
}
if( input->isKeyDown(OIS::KC_D) )
{
Quaternion q1 = _torso->getOrientation();
Quaternion q2(Degree(4),Ogre::Vector3::UNIT_Y);
_torso->setOrientation(q1*q2);
}


What happens is that the character moves ignoring physical collisions, I suppose there is any way to move the capsule and that this complies with the collision.

As I can implement this system? the capsule is to be moved by applying force? must update the physical world between physical and setPosition setPosition visual?

Thanks for your help!

smewemek

15-04-2010 15:50:33

ok, the solution is move capsule by setLinearVelocity().

When finished it will put the code.