Hanfling
29-04-2007 19:50:49
im trying to make a player controlled character, and i already managed to get the movement working (forward/back strafe right/left), but i still have a huge problem getting the character to rotate using the mouse.
this is how i setup my collision objects (using a simple capsule and an UpVector):
desired rotation amount is set using the MouseListener interface
and this is my force callback function:
the force callback still has some other attempts to rotate the char in it, of which none worked.
as soon as i move the mouse, the character starts rotating as wanted, but will eventually just bounce off the ground, rotate around some point at the center of the scene (like a ball on a rope) and then fly off.
i searched the forums for character control threads, and saw different approaches for rotating a character using torque, setting the rotation speed manually, or applying forces like you can see above.
i tried nearly all of these (and obviously im too dumb to get it working or something ), except the one where the torque is calculated using the current and desired rotation.
so here are my questions:
how can i calculate the desired rotation using the mouse event?
or, if my code isn't that far from a possible solution: where are my mistakes?
this is how i setup my collision objects (using a simple capsule and an UpVector):
//create materials and friction setup!
OgreNewt::MaterialID *mat1 = terMat;
OgreNewt::MaterialID *mat2 = new OgreNewt::MaterialID(mWorld);
OgreNewt::MaterialPair *pair = new OgreNewt::MaterialPair(mWorld, mat1, mat2);pair->setDefaultCollidable(1);
pair->setDefaultFriction(0.0,0.0);
pair->setDefaultElasticity(0);
// UPPER BODY
OgreNewt::Collision *col;
Vector3 *pos = new Vector3(0,4.5,0);
Quaternion *q = new Quaternion(Degree(90), Vector3::UNIT_Z);
col = new OgreNewt::CollisionPrimitives::Capsule(mWorld, max.x, max.y*0.8, *q, *pos);
OgreNewt::Body *uBody = new OgreNewt::Body(mWorld, col);
uBody->setMaterialGroupID(mat2);
Ogre::Vector3 inertia;
size = new Vector3(1,1,1);
inertia = OgreNewt::MomentOfInertia::CalcEllipsoidSolid(2, *size);
uBody->attachToNode( ninjaNode );
uBody->setAutoFreeze(false);
uBody->setMassMatrix( 2, inertia );
// add a joint to keep the upper body upright
OgreNewt::Joint* joint;
joint = new OgreNewt::BasicJoints::UpVector( mWorld, uBody, Vector3(Vector3::UNIT_Y));
delete col;
desired rotation amount is set using the MouseListener interface
bool MouseListener::mouseMoved( const OIS::MouseEvent &e ) {
using namespace OIS;
mCamRot_y = e.state.X.rel * mRotate;
return true;
}
and this is my force callback function:
void updateForce(OgreNewt::Body *me){
Real gain = 20.0;
if(moveFB != 0)
mDirection.z = moveFB * mAccel;
else {
mDirection.z = 0;
Vector3 netForce = Vector3(0,0,-mBody->getVelocity().z*gain);
mBody->addLocalForce(netForce, Vector3::ZERO);
}
if(moveLR != 0)
mDirection.x = moveLR * mAccel;
else {
mDirection.x = 0;
Vector3 netForce = Vector3(-mBody->getVelocity().x*gain,0,0);
mBody->addLocalForce(netForce, Vector3::ZERO);
}
if(moveUD != 0)
mDirection.y = moveUD * mAccel;
else {
mDirection.y = 0;
}
me->addLocalForce(mDirection, Vector3(0,0,0));
if(mCamRot_y != 0){
me->setOmega(Vector3(0,mCamRot_y,0));
mCamRot_y = 0;
} else {
me->setOmega(Vector3::ZERO);
}
/*
if(mCamRot_y != 0){
Vector3 torq = Vector3(0,mCamRot_y*0.2,0);
me->addTorque(torq);
}
*/
/*
if(mCamRot_y != 0){
Vector3 force = Vector3(0,0,mCamRot_y);
me->addLocalForce(force, Vector3(3,0,0));
force.z=-mCamRot_y;
me->addLocalForce(force, Vector3(-3,0,0));
mCamRot_y=0;
}
*/
//add gravity force
Real mass;
Vector3 in;
me->getMassMatrix(mass, in);
Vector3 *grav = new Vector3(0,-18.0f,0);
*grav = *grav * mass;
me->addForce(*grav);
//limit speed
if(mDirection.length() > mMoveMax){
mDirection.normalise();
mDirection = mDirection * mMoveMax;
}
}
the force callback still has some other attempts to rotate the char in it, of which none worked.
as soon as i move the mouse, the character starts rotating as wanted, but will eventually just bounce off the ground, rotate around some point at the center of the scene (like a ball on a rope) and then fly off.
i searched the forums for character control threads, and saw different approaches for rotating a character using torque, setting the rotation speed manually, or applying forces like you can see above.
i tried nearly all of these (and obviously im too dumb to get it working or something ), except the one where the torque is calculated using the current and desired rotation.
so here are my questions:
how can i calculate the desired rotation using the mouse event?
or, if my code isn't that far from a possible solution: where are my mistakes?