I have this code for yaw and pitch camera movement:
DarkFuture->mCamNode->yaw( Degree(-mRotate * mInputDevice->getMouseRelativeX()) );
DarkFuture->mCamNode->getChild(0)->pitch( Degree(-mRotate * mInputDevice->getMouseRelativeY()) );
Now i want to apply this yaw and pitch movement to the body i created for my camera.
I already saw another posts but everything i did didnt work
I have forward, backwards and strafe working now i need this.
Thanks in advance!
lonwolf
03-09-2006 19:46:54
well your problem is really simple. Heres the simpliest way to do that:
Assuming your camera is attache to a scene node called mCamNode and u have a newton body attached to that node after the line youve just posted add these
Vector3 pos;
Quaternion orn;
mCameraBody->getPositionOrientation(pos, orn);
orn=mCamNode->getOrientation();
mCameraBody->setPositionOrientation(pos, orn);
everithing should work just fine
I tryed that mate but the problem is that the pitch dont work and the yaw movement its strange initialy the camera make a good yaw movement but when i walk a little the camera moves around the y axis like an orbit instead of rotate.
By the way i saw your website and your game seems very nice.
lonwolf
04-09-2006 10:49:28
Well the game is still under development, should be ready in 2 weeks.. only a part of it..
Only one question, can i see your cam movement code? i have the feeling its not right. First of all the camnode should be atached directly to the Root and you should move/rotate the cam node only. So if you move only the camera you will get an orbit.. you need to move the node. The cam will move along
Of course you can im thinking in release the fps camera code when i put it work.
cPlayer class code:
#include "cPlayer.h"
#include "DF.h"
cPlayer::cPlayer(Camera *cam, InputReader* InputDevice, SceneManager* SceneMgr, DF* _DF, OgreNewt::Body* body)
{
DarkFuture = _DF;
mCamera = cam;
mSceneMgr = SceneMgr;
mInputDevice = InputDevice;
cam_body = body;
mRotate = 0.1;
}
void cPlayer::moveOrientation(){
//camera orientation
DarkFuture->mCamNode->setOrientation(quat/*Quaternion::IDENTITY*/);
DarkFuture->cam_body->setPositionOrientation(lastPosition,quat/*Quaternion::IDENTITY*/);
DarkFuture->mCamNode->pitch( Degree(pitchAngle));
DarkFuture->cam_body->setPositionOrientation(lastPosition,Quaternion(Radian(Degree(yawAngle)),Vector3::UNIT_Y));
}
void cPlayer::Input(const FrameEvent &evt)
{
mCamera->getParentSceneNode()->detachObject( mCamera );//get camera nodes
DarkFuture->mCamNode = mSceneMgr->getSceneNode( "CamNode" );
mSceneMgr->getSceneNode( "PitchNode1" )->attachObject( mCamera );
Vector3 trans, strafe, vec;
quat = DarkFuture->mCamNode->getOrientation();
vec = Vector3(0.0,0.0,-2);
trans = quat * vec;
vec = Vector3(2,0.0,0.0);
strafe = quat * vec;
bool up_enable;
up_enable = true;
//DarkFuture->cam_body->getPositionOrientation
if (mInputDevice->isKeyDown(Ogre::KC_UP) || mInputDevice->isKeyDown(Ogre::KC_W))
{
DarkFuture->cam_body->setVelocity( (trans * 6.0) );//add velocity to the body
}
if (mInputDevice->isKeyDown(Ogre::KC_DOWN) || mInputDevice->isKeyDown(Ogre::KC_S))
{
DarkFuture->cam_body->unFreeze();
DarkFuture->cam_body->setVelocity( (trans * -1 * 6.0) );
}
if (mInputDevice->isKeyDown(Ogre::KC_LEFT) || mInputDevice->isKeyDown(Ogre::KC_A))
{
DarkFuture->cam_body->unFreeze();
DarkFuture->cam_body->setVelocity( (strafe * -1 * 6.0) );
}
if (mInputDevice->isKeyDown(Ogre::KC_RIGHT) || mInputDevice->isKeyDown(Ogre::KC_D))
{
DarkFuture->cam_body->unFreeze();
DarkFuture->cam_body->setVelocity( (strafe * 6.0) );
}
if (mInputDevice->isKeyDown(Ogre::KC_PGDOWN))
{
Ogre::Vector3 dir(0,-1,0);
DarkFuture->cam_body->setVelocity( (dir * 6.0) );
}
if (mInputDevice->isKeyDown(Ogre::KC_PGUP))
{
Ogre::Vector3 dir(0,1,0);
DarkFuture->cam_body->setVelocity( (dir * 6.0) );
}
DarkFuture->cam_body->setOmega(Ogre::Vector3::ZERO );//for camera dont start rotate
//DarkFuture->cam_body->getPositionOrientation(lastPosition,quat);
yawAngle += -mRotate * mInputDevice->getMouseRelativeX();
pitchAngle += -mRotate * mInputDevice->getMouseRelativeY();
Ogre::Quaternion quater;
Ogre::Matrix3 mMatrix;
Ogre::Vector3 vect, mTorque;
//DarkFuture->mCamNode->yaw( Degree(-mRotate * mInputDevice->getMouseRelativeX()) );
//DarkFuture->mCamNode->getChild(0)->pitch( Degree(-mRotate * mInputDevice->getMouseRelativeY()) );
mTorque = Ogre::Vector3(yawAngle,pitchAngle,0);
DarkFuture->cam_body->setTorque(mTorque);
//Ogre::lo
//vect = DarkFuture->mCamNode->getPosition();
DarkFuture->cam_body->getPositionOrientation(vect,quater);
quater = DarkFuture->mCamNode->getOrientation();
//DarkFuture->mCamNode->pitch(Ogre::Degree(quater.y));
//DarkFuture->mCamNode->yaw(Ogre::Degree(quater.x));
DarkFuture->cam_body->setPositionOrientation(vect,quater);
//moveOrientation();// give the movement of the camera to the body
//quat = DarkFuture->mCamNode->getOrientation();
//lastPosition = DarkFuture->mCamNode->getPosition(); //get the last position of the camera
}
create camera code:
void DF::createCamera() {
mCamera = mSceneMgr->createCamera("PlayerCam");
mCamNode = mSceneMgr->getRootSceneNode()->createChildSceneNode("CamNode");
// Create the pitch node
mCamNode = mCamNode->createChildSceneNode( "PitchNode1" );
mCamNode->attachObject(mCamera);
mCamNode->setPosition(0,50,0);
mCamera->setNearClipDistance(0.1);
}
Create camera body code:
void DF::createCamBody()
{
col = new OgreNewt::CollisionPrimitives::Ellipsoid( m_World, Ogre::Vector3(1,1,1) );
cam_body = new OgreNewt::Body( m_World, col );
delete col;
Ogre::Real mass = 20.0;
Ogre::Vector3 inertia = OgreNewt::MomentOfInertia::CalcEllipsoidSolid( mass,Ogre::Vector3(1,1,1));
cam_body->setMassMatrix( mass, inertia );
Vector3 posi = mCamNode->getPosition();
cam_body->setPositionOrientation(posi, mCamNode->getOrientation());
cam_body->addForce(Vector3(0,-9.8,0));
cam_body->setStandardForceCallback();
cam_body->setAutoFreeze(0);
cam_body->attachToNode(mCamNode);
cam_body->setLinearDamping ( 0.01f );
cam_body->setAngularDamping ( Vector3(0.01f, 0.01f, 0.01f ));
cam_body->setUserData(this);
const OgreNewt::MaterialID* mMatDefault = m_World->getDefaultMaterialID();
const OgreNewt::MaterialID* mMatCamera = new OgreNewt::MaterialID( m_World );
OgreNewt::MaterialPair *mMatPairDefaultCamera = new OgreNewt::MaterialPair( m_World, mMatDefault, mMatCamera );
mMatPairDefaultCamera->setDefaultFriction( 0.4, 0.4 );
mMatPairDefaultCamera->setDefaultSoftness(0.1);
mMatPairDefaultCamera->setDefaultElasticity(0.0);
mMatPairDefaultCamera->setContinuousCollisionMode(1);
cam_body->setMaterialGroupID(mMatCamera);
new OgreNewt::BasicJoints::UpVector( m_World, cam_body, Ogre::Vector3::UNIT_Y );
}
I think all code needed is here.
If you can see what is the problem i will thank you alot.
lonwolf
04-09-2006 21:26:03
as i can see from yourr code:
1. this line is reduntant: cam_body->addForce(Vector3(0,-9.8,0));
this has only efect in an forca callback function but u dont need it coz u use standard force callback.
2. that elipsoid is a bit to small i guess. if u want the cam not to collide and have a nice view when hitting a wall you should enlarge it in such manner that when u open OgreNewt::Debugger yoou can see the cameras body from the inside if u understand wat i mean.
3.the orbiting camera is abnormal since its parent mCamNode is at 0,0,0 and the pitch node at 0,50,0 shouldnt pose problems..
4. heres my code for yawing a body (main character and it could work to pitch it too)
void mouseDragged (MouseEvent *e)
{
if(currMouse && !the_hero.isInFirstPerson)
{
CEGUI::MouseCursor::getSingleton().hide();
mCamNode->yaw( Degree(-e->getRelX( ) * mRotateM) );
the_hero.playerBody->setPositionOrientation(mCamNode->getPosition(), mCamNode->getOrientation());
mCamNode->getChild( 0 )->pitch( Degree(-e->getRelY() * mRotateM) );
}
mouseMoved(e);
}
if u raplace mCamNode->getChild( 0 )->pitch( Degree(-e->getRelY() * mRotateM) );
with mCamNode->pitch( Degree(-e->getRelY() * mRotateM) );
you can have o working pitch code.
hope it helps.
Thanks mate i will try it i think i will try to join my code tou yours and hansel code.
And see what i get.
When i aply pitch all my screen becames black but without pitch it works fine.
This is the code of the creation of the camera and the 2 nodes:
void DF::createCamera() {
mCamera = mSceneMgr->createCamera("PlayerCam");
mCamera->setNearClipDistance(0.1);
mCamBodyNode = mSceneMgr->getRootSceneNode()->createChildSceneNode("CamBodyNode");
//mCamBodyNode->setPosition(0,0,0);
mCamBodyNode->setScale(cam_size);
mCamView = mCamBodyNode->createChildSceneNode(Vector3(0,5,0));
mCamView->attachObject(mCamera);
}
This is where i call the pitch:
void cPlayer::Input(const FrameEvent &evt)
{
/*mCamera->getParentSceneNode()->detachObject( mCamera );//get camera nodes
DarkFuture->mCamBodyNode = mSceneMgr->getSceneNode( "CamBodyNode" );
mSceneMgr->getSceneNode( "CamView" )->attachObject( mCamera );*/
Vector3 trans, strafe, vec;
quat = DarkFuture->mCamBodyNode->getOrientation();
vec = Vector3(0.0,0.0,-2);
trans = quat * vec;
vec = Vector3(2,0.0,0.0);
strafe = quat * vec;
bool up_enable;
up_enable = true;
if (mInputDevice->isKeyDown(Ogre::KC_UP) || mInputDevice->isKeyDown(Ogre::KC_W))
{
DarkFuture->cam_body->setVelocity( (trans * 15.0) );//add velocity to the body
}
if (mInputDevice->isKeyDown(Ogre::KC_DOWN) || mInputDevice->isKeyDown(Ogre::KC_S))
{
DarkFuture->cam_body->unFreeze();
DarkFuture->cam_body->setVelocity( (trans * -1 * 15.0) );
}
if (mInputDevice->isKeyDown(Ogre::KC_LEFT) || mInputDevice->isKeyDown(Ogre::KC_A))
{
DarkFuture->cam_body->unFreeze();
DarkFuture->cam_body->setVelocity( (strafe * -1 * 15.0) );
}
if (mInputDevice->isKeyDown(Ogre::KC_RIGHT) || mInputDevice->isKeyDown(Ogre::KC_D))
{
DarkFuture->cam_body->unFreeze();
DarkFuture->cam_body->setVelocity( (strafe * 15.0) );
}
if (mInputDevice->isKeyDown(Ogre::KC_PGDOWN))
{
Ogre::Vector3 dir(0,-1,0);
DarkFuture->cam_body->setVelocity( (dir * 6.0) );
}
if (mInputDevice->isKeyDown(Ogre::KC_PGUP))
{
Ogre::Vector3 dir(0,1,0);
DarkFuture->cam_body->setVelocity( (dir * 6.0) );
}
camRotationX = -mInputDevice->getMouseRelativeX() * xSensitivity /(100 * evt.timeSinceLastFrame);
camRotationY = -mInputDevice->getMouseRelativeY() * ySensitivity /(100 * evt.timeSinceLastFrame);
DarkFuture->cam_body->setOmega(Ogre::Vector3(0 ,camRotationX ,0));//for camera dont start rotate
// Create the pitch node
DarkFuture->mCamView->pitch( Degree(camRotationY) );
}