Character Controller

twojmisiek

26-08-2009 11:56:09

Hi,

Currently, I'm working on character controller in my game. I make a OgreNewt::Body to handle the physics body. I'm doing a moving this way:
1. When pressed W - set the Vector3 velocity.
2. In each call ForceCallback:

b->addForce(velocity);

3. If the camera moves(the character controller node is a parent of camera):
camera->yaw(Degree(-0.19 * e.state.X.rel));
body->setPositionOrientation(camera->getPosition(),camera->getOrientation());


Everything is ok. But, when I press W, and the player is on the some object - It's star shaking to the left or right. Maybe somebody have already implemented good Character Controller and share me with your code? And please do not write posts are describes how to do it with Newton, I don't know how to translate it to OgreNewt.

Greetings!

P.S. Sorry for me english ;)

kallaspriit

27-08-2009 17:01:19

Check out latest OgreNewt wrapper, there is a character controller build in thought it is still under active development. It's not as easy to do as you might think, I suggest trying the built-in if you dont need total control.

PJani

29-08-2009 13:56:33

Hi,

[b]camera->yaw(Degree(-0.19 * e.state.X.rel));
body->setPositionOrientation(camera->getPosition(),camera->getOrientation());[/b]


This is bad idea for me. Becouse setting position of body from camera makes some weard sheaking effects when you move camera.
I think you should be doing this in another way.
like

const float max_push_force = 1000.0; //Newtons or any other force unit...
const float max_jump_force = 2000.0; //Newtons
const float max_rotation_torque = 500.0; //NewtonMeters

float jump_force = (keyTouch_bJump)? max_jump_force : 0.0;
float push_force = (key_bForward)? max_push_force : 0.0;
push_force = (key_bBackward)? -max_push_force : 0.0;



Ogre::Vector3 p;Ogre::Quaternion q;
body->getPositionOrientation(p,q);
Ogre::Radians yaw = Ogre::Radians(q->getYaw(True));

Ogre::Real x = Ogre::Math::Sin(yaw)*push_force;
Ogre::Real z = Ogre::Math::Cos(yaw)*push_force;
//Jump
Ogre::Real y = jump_force;


Ogre::Real rot_torque = Degree(-0.19 * e.state.X.rel).valueRadians() * max_rotation_torque;
Ogre::Vector3 move_force = Ogre::Vector3(x,y,z);

//add push force
body->addForce(move_force);
body->addTorque(Ogre::Vector3(0,rot_torque,0));



//The position of camera is set by delay of one frame...
//becouse body positition and orientation needs to be calculated
cam->setPosition(p);
cam->setOrientation(q);



Here is my code writen from the head. You will need some adjustments if you want rotation not by torque but by degrees:D

twojmisiek

05-09-2009 13:31:05

Hi, Thanks!

I changed my code, and now it looks like this:
node->yaw(Degree(-0.13 * e.state.X.rel));


Ogre::Real rot_torque = Degree(-0.19 * e.state.X.rel).valueRadians() * max_rotation_torque;
body->addTorque(Ogre::Vector3(0,rot_torque,0));


Ogre::Vector3 p;Ogre::Quaternion q;
body->getPositionOrientation(p,q);

camNode->setOrientation(q);
camNode->setPosition(p);


And I met a another problem... Player rotates, but when the MouseMoved() function ends, Player back to previous angle. I'm bad using this code? :]

I hope you understand my english :P

PJani

07-09-2009 16:43:11

Try to remove this...

node->yaw(Degree(-0.13 * e.state.X.rel));

twojmisiek

07-09-2009 18:54:43

If I remove this, character doesn't rotates at all. Maybe you can share me with your more complete code?

PJani

08-09-2009 14:47:52

Are you 100% that you are calling all this in forceCallback?

twojmisiek

08-09-2009 19:29:33

Nevermind...
I make that I can move character by mouse but it's still shaking! Other ideas?