[Solved]FPS Camera on OGRENewt

Sgw32

07-01-2010 17:00:59

I tried to make this using topic:
viewtopic.php?t=2110
but the camera only moved by keys WASD,when i moved mouse it didn't change the direction of movement.
I don't know what this function do:
OgreNewt::Body->setOmega(Vector3(0,camera_rotation_x,0)? - rotates the body?
That is my class for first person camera movement, can you say me what's wrong :( ?:
Player.cpp
#include "Player.h"

Player::Player()
{
}

Player::~Player()
{
}

void Player::init(Ogre::SceneManager* scene,OgreNewt::World* world,Ogre::Camera* cam)
{
if( !scene )
{
throw Ogre::Exception( -1, "SceneMgr : assertion failed at LoadMap::init","" );
}
mSceneMgr=scene;

if( !world )
{
throw Ogre::Exception( -1, "World : assertion failed at PhysObject::init","" );
}
mWorld=world;
if( !cam )
{
throw Ogre::Exception( -1, "World : assertion failed at PhysObject::init","" );
}
Ogre::Entity* ent;
mCamera=cam;
mCamNode = mSceneMgr->getRootSceneNode()->createChildSceneNode();
mCamNode->setPosition(Vector3(0,100,0));
mViewNode = mCamNode->createChildSceneNode();
mViewNode->attachObject(mCamera);
mDirection = Vector3::ZERO;
noclip=false;
no_movement=true;
mRotate = 0.13;
mMove = 250;
gravity = 10;
y_rotation_cont = 0;
y_limit_a = 90;
y_limit_b = -90;
jump_power = 5;
fps_speed = 3;
size = Vector3(2,2,2);
mCamNode->setScale(size);
OgreNewt::Collision* col = new OgreNewt::CollisionPrimitives::Ellipsoid(mWorld, size);
bod = new OgreNewt::Body( mWorld, col );
bod->attachToNode(mCamNode);
bod->setPositionOrientation( mCamNode->getPosition(), Ogre::Quaternion::IDENTITY );
delete col;
inertia = OgreNewt::MomentOfInertia::CalcEllipsoidSolid(40,size);
bod->setMassMatrix( 40, inertia );
bod->setCustomForceAndTorqueCallback<Player>( &Player::camera_force_callback ,this); // add the previous defined callback function as the body custom force and torque callback
//bod->setStandardForceCallback();
bod->setAutoFreeze(0);
OgreNewt::BasicJoints::UpVector* uv2 = new OgreNewt::BasicJoints::UpVector(mWorld,bod,Vector3::UNIT_Y);
jump=false;
}

void Player::camera_force_callback( OgreNewt::Body* me )
{

Player *camPlayer = (Player*)me->getUserData();
Ogre::Real mass;
Ogre::Radian yaw;
Ogre::Vector3 inertia;
Ogre::Quaternion qb;
me->getMassMatrix(mass, inertia);
force = Vector3(0,gravity,0);
qb = mViewNode->getOrientation();
direction = (Quaternion(Degree(x_rotation_cont),Vector3::UNIT_Z) * Vector3::NEGATIVE_UNIT_Z);
Ogre::Vector3 V0 = me->getVelocity();
Ogre::Vector3 V1(direction * mDirection);
acel = (V1 - V0);

force.x = acel.x;
//force.y = acel.y;
force.z = acel.z;
force *= mass;
me->addForce( -force );
me->addForce(Vector3(0,1,0));
bod->setOmega(Vector3(0,camera_rotation_x,0)); //to rotate camera in x (this also don't work)
//bod->setPositionOrientation(get_body_position(bod),mViewNode->getOrientation()); - this don't work:(
}

Ogre::Quaternion Player::get_body_orientation(OgreNewt::Body* bod)
{

Quaternion orient;
Vector3 pos;

bod->getPositionOrientation(pos, orient);

return orient;

}

Ogre::Vector3 Player::get_body_position(OgreNewt::Body* bod)
{

Quaternion orient;
Vector3 pos;

bod->getPositionOrientation(pos, orient);

return pos;

}


void Player::FCPress(const OIS::KeyEvent &arg)
{
if(noclip)
{
switch (arg.key)
{
case OIS::KC_UP:
case OIS::KC_W:
mDirection.z = -fps_speed*30;
break;

case OIS::KC_DOWN:
case OIS::KC_S:
mDirection.z = fps_speed*30;
break;

case OIS::KC_LEFT:
case OIS::KC_A:
mDirection.x = -fps_speed*30;
break;

case OIS::KC_RIGHT:
case OIS::KC_D:
mDirection.x = fps_speed*30;
break;

default:
break;
}
}
else
{
//FPS Mode
switch (arg.key)
{
case OIS::KC_UP:
case OIS::KC_W:
mDirection.z = -fps_speed*mMove*30;
break;

case OIS::KC_DOWN:
case OIS::KC_S:
mDirection.z = fps_speed*mMove*30;
break;

case OIS::KC_LEFT:
case OIS::KC_A:
mDirection.x = -fps_speed*mMove*30;
break;

case OIS::KC_RIGHT:
case OIS::KC_D:
mDirection.x = fps_speed*mMove*30;
break;

default:
break;
}
}
}

void Player::MouseMove(const OIS::MouseEvent &arg)
{
camera_rotation_x = -mRotate * arg.state.X.rel;
camera_rotation_y = -mRotate * arg.state.Y.rel;
camera_rotation_xa = -mRotate * arg.state.X.abs;
camera_rotation_ya = -mRotate * arg.state.Y.abs;
mViewNode->yaw(Degree(camera_rotation_x), Node::TS_WORLD);
mViewNode->pitch(Degree(camera_rotation_y), Node::TS_LOCAL);
y_rotation_cont += camera_rotation_y;
x_rotation_cont += camera_rotation_x;
if (x_rotation_cont >= 360)
{
x_rotation_cont = 0;
}
if (y_rotation_cont > y_limit_a || y_rotation_cont < y_limit_b)
{
mViewNode->pitch(Degree(-camera_rotation_y ));
y_rotation_cont -= camera_rotation_y;
}
}

void Player::FCRelease(const OIS::KeyEvent &arg)
{

switch (arg.key)
{
case OIS::KC_UP:
case OIS::KC_W:
mDirection.z = 0;
bod->setVelocity(Vector3(0,0,0));
break;

case OIS::KC_DOWN:
case OIS::KC_S:
mDirection.z = 0;
bod->setVelocity(Vector3(0,0,0));
break;

case OIS::KC_LEFT:
case OIS::KC_A:
mDirection.x = 0;
bod->setVelocity(Vector3(0,0,0));
break;

case OIS::KC_RIGHT:
case OIS::KC_D:
mDirection.x = 0;
bod->setVelocity(Vector3(0,0,0));
break;

default:
break;
} // switch

}

void Player::FCUpdate(const FrameEvent &evt)
{

if(noclip)
{
acel = Vector3(0,0,0);
bod->setVelocity(Vector3(0,0,0));
mViewNode->translate(mDirection * evt.timeSinceLastFrame, Node::TS_LOCAL);
mCamNode->setPosition(mViewNode->getPosition());
Vector3 pos;
Ogre::Quaternion orient;
bod->getPositionOrientation(pos,orient);
bod->freeze();
bod->setPositionOrientation(mCamNode->getPosition(), Ogre::Quaternion::IDENTITY);
}
else
{
bod->unFreeze();
}
}



Player.h

#pragma once
#include <Ogre.h>
#include <OgreNewt.h>
#include "OgreConsole.h"
#include <OIS/OIS.h>

class Player
{
public:
Player();
~Player();
void init(SceneManager *SceneMgr,OgreNewt::World* mWorld,Ogre::Camera* Camera);
void camera_force_callback( OgreNewt::Body* body );
void MouseMove(const OIS::MouseEvent &arg);
void FCPress(const OIS::KeyEvent &arg);
void FCRelease(const OIS::KeyEvent &arg);
void FCUpdate(const FrameEvent &evt);
Ogre::Quaternion Player::get_body_orientation(OgreNewt::Body* bod);
Ogre::Vector3 Player::get_body_position(OgreNewt::Body* bod);
bool noclip;
private:
Ogre::SceneManager* mSceneMgr;
OgreNewt::World* mWorld;
Ogre::Camera* mCamera;
Real mRotate;
Real mMove;
Real jump_power;
Vector3 poo2;
Vector3 acel;
Vector3 force;
Real gravity;
Real camera_rotation_x;
Real camera_rotation_y;
Real camera_rotation_xa;
Real camera_rotation_ya;
Real y_rotation_cont;
Real x_rotation_cont;
Real y_limit_a;
Real y_limit_b;
SceneNode *mCamNode;
SceneNode *mViewNode;
Real cam_height;
Real fps_speed;
OgreNewt::Body* bod;
Ogre::Vector3 size;
Vector3 mDirection;
Vector3 direction;
Vector3 stra;
bool jump;
bool no_movement;
Ogre::Vector3 MyVel;
Ogre::Vector3 inertia;
};

This supports 2 modes of movement - collision and noclip(as in Quake or Half-life)
I can change the noclip variable with pressing "f" button(not in this class,in my main.cpp)
It only moves camera forward,left,right,backward.I can't control it by mouse, but mouse control the camera view with pitch and yaw of mViewNode(the view moves,but camera moves wrong, with bad direction)
Have anybody of you a working fps collision camera demo for ogrenewt?I'm tired of it! :?: :?: :?: :?:

Sgw32

09-01-2010 15:26:42

Anybody help me!!! :!: :( :x :evil:

majc

19-01-2010 17:37:30

I saw very quilckly your post try to change:

OgreNewt::Body->setOmega(Vector3(0,camera_rotation_x,0)?

to:

OgreNewt::Body->setOmega(Vector3(camera_rotation_x,0,0))

pra

31-01-2010 09:44:37

hm.
you should not rotate the camera with the x omega, apply this only to the body itself in the callback.
otherwise, i'm not sure

Sgw32

31-01-2010 16:49:46

Yeah, I already had done this. Sorry for no answering. Now I'm building jump, duck in-game features,weapons.

Sgw32

31-01-2010 16:49:46

Yeah, I already had done this. Sorry for no answering. Now I'm building jump, duck in-game features,weapons.