Creating a simple first-person camera system

 

Creating a simple first-person camera system

Original version by xplozyph.

Contents

[hide]

Introduction

If, like me, you want to use some kind of first-person camera in the Quake 3 Arena style, thus a free-look camera with a pitch limited between -90 degrees and 90 degrees, you are at the right page!

To make it relatively simple and powerful, we are going to use multiple SceneNodes. Do not be afraid if you have never used them, it is a really simple concept, at least, for what we are going to use them.

Basic understanding of the SceneNode concept

A SceneNode is just some kind of special spatial "container". It can "contain" just other SceneNode as well as Entity, Camera and other inheritent of SceneNodeand MovableObject classes (and their own inheritent as well).

Why should we worry about them? Because each SceneNode has its own space transformations. Space transformations are scale, rotation (orientation) and translation (position). But what is still more interesting it is that they also inherit their parent's space transformations.

A small example being worth a thousand words: we have SceneNode B which inherits SceneNode A. Then, we apply a rotation of 90 degrees around SceneNode A's Y-axis. Now, if we read the SceneNode B's Y-axis orientation angle (we are going to see how to do that in this particular case), we see it is 90 degrees!

For those ones who would not have understood or realised, if SceneNode B would not inherit from SceneNode A, when we would have read SceneNode B's Y-axis orientation angle, it would have returned 0 degrees (default orientation), despite the fact that SceneNode A's Y-axis orientation angle of 90 degrees.

Note that this is also true for inheritent of MovableObject (Entity, Camera, etc...).

It is that SceneNode-SceneNode and SceneNode-MovableObject space transformation inheritage property we are going to use to build up our customised first-person camera.

So, let's go :)

Details about how to achieve the desired effect

Here is the layout of our SceneNode hierarchy:

cameraNode (Ogre::SceneNode *)
                    ||
                    \/
     cameraYawNode (Ogre::SceneNode *)
                    ||
                    \/
    cameraPitchNode (Ogre::SceneNode *)
                    ||
                    \/
    cameraRollNode (Ogre::SceneNode *)
                    ||
                    \/
          camera (Ogre::Camera *)

cameraNode will be the hierarchy's top SceneNode. It is going to handle camera's position (camera will inherit its translation space transformation fromcameraRollNode which will inherit it from cameraPitchNode, etc...).

As you have probably guessed, cameraYawNode will handle... camera's yaw "orientation", cameraPitchNode will handle camera's pitch "orientation" and, finally,cameraRollNode will handle camera's roll "orientation".

Euler's angles and gimbal lock effect

Each rotation (orientation) space transformation axis angles will be independent of each others. The yaw, pitch and roll will be hermetically separated. This will avoid the gimbal lock effect, inherent to Euler's angles (yaw, pitch and roll) manipulation.

The effect appears as soon as you change the value of one of those three angles, you also ineluctably change the others two. That results in completely disordered orientation and crazy rotation, that is the so-called gimbal lock effect.

What about yaw, pitch and roll?

But you may wonder: "What is the yaw, the pitch and the roll?".

Well, simply put, the yaw is the angle of rotation around the Y-axis, pitch is the one around the X-axis and the roll is the one around the Z-axis.

Dive into the code

First, we declare some new variables in our FrameListener class:

Ogre::SceneNode *cameraNode;
Ogre::SceneNode *cameraYawNode;
Ogre::SceneNode *cameraPitchNode;
Ogre::SceneNode *cameraRollNode;

Then, we jump into the FrameListener's constructor and initialise those fancy new variables the right way with respect to the layout we drew above:

// Create the camera's top node (which will only handle position).
this->cameraNode = this->sceneManager->getRootSceneNode()->createChildSceneNode();
this->cameraNode->setPosition(0, 0, 500);

// Create the camera's yaw node as a child of camera's top node.
this->cameraYawNode = this->cameraNode->createChildSceneNode();

// Create the camera's pitch node as a child of camera's yaw node.
this->cameraPitchNode = this->cameraYawNode->createChildSceneNode();

// Create the camera's roll node as a child of camera's pitch node
// and attach the camera to it.
this->cameraRollNode = this->cameraPitchNode->createChildSceneNode();
this->cameraRollNode->attachObject(this->camera);

The first stone has been dropped.

Note that you should ensure that your camera and camera's yaw, camera's pitch and camera's roll nodes are all located at (0, 0, 0), which is the default location. Ensure not to call this->camera->setPosition(someX, someY, someZ) or this->camera->translate(someOtherX, someOtherY, someOtherZ) anywhere in your program's code (or to cancel its effect by calling this->camera->setPosition(0.0f, 0.0f, 0.0f)). Else you will have undesired camera-rotates-around-a-point effect (it is my experience that is speaking ;-P).

Now, delete all FrameListener's moveCamera() method content (do not remove the method itself, just its content) and replace it by the following:

Ogre::Real pitchAngle;
Ogre::Real pitchAngleSign;

// Yaws the camera according to the mouse relative movement.
this->cameraYawNode->yaw(this->mRotX);

// Pitches the camera according to the mouse relative movement.
this->cameraPitchNode->pitch(this->mRotY);

// Translates the camera according to the translate vector which is
// controlled by the keyboard arrows.
//
// NOTE: We multiply the mTranslateVector by the cameraPitchNode's
// orientation quaternion and the cameraYawNode's orientation
// quaternion to translate the camera accoding to the camera's
// orientation around the Y-axis and the X-axis.
this->cameraNode->translate(this->cameraYawNode->getOrientation() *
                            this->cameraPitchNode->getOrientation() *
                            this->mTranslateVector,
                            Ogre::SceneNode::TS_LOCAL);

// Angle of rotation around the X-axis.
pitchAngle = (2 * Ogre::Degree(Ogre::Math::ACos(this->cameraPitchNode->getOrientation().w)).valueDegrees());

// Just to determine the sign of the angle we pick up above, the
// value itself does not interest us.
pitchAngleSign = this->cameraPitchNode->getOrientation().x;

// Limit the pitch between -90 degress and +90 degrees, Quake3-style.
if (pitchAngle > 90.0f)
{
    if (pitchAngleSign > 0)
        // Set orientation to 90 degrees on X-axis.
        this->cameraPitchNode->setOrientation(Ogre::Quaternion(Ogre::Math::Sqrt(0.5f),
                                                               Ogre::Math::Sqrt(0.5f), 0, 0));
    else if (pitchAngleSign < 0)
        // Sets orientation to -90 degrees on X-axis.
        this->cameraPitchNode->setOrientation(Ogre::Quaternion(Ogre::Math::Sqrt(0.5f),
                                                               -Ogre::Math::Sqrt(0.5f), 0, 0));
}

Then, we replace some proposed default controls and add some others in the FrameListener's processUnbufferedKeyInput() method (I assume you base yourself on the ExampleFrameListener, else you should be able to apply it to your own particular case ;-]):

// Move camera upwards along to world's Y-axis.
if(inputManager->isKeyDown(Ogre::KC_PGUP))
 //this->translateVector.y = this->moveScale;
 this->cameraNode->setPosition(this->cameraNode->getPosition() + Ogre::Vector3(0, 5, 0));

// Move camera downwards along to world's Y-axis.
if(inputManager->isKeyDown(Ogre::KC_PGDOWN))
 //this->translateVector.y = -(this->moveScale);
 this->cameraNode->setPosition(this->cameraNode->getPosition() - Ogre::Vector3(0, 5, 0));

// Move camera forward.
if(inputManager->isKeyDown(Ogre::KC_UP))
 this->translateVector.z = -(this->moveScale);

// Move camera backward.
if(inputManager->isKeyDown(Ogre::KC_DOWN))
 this->translateVector.z = this->moveScale;

// Move camera left.
if(inputManager->isKeyDown(Ogre::KC_LEFT))
 this->translateVector.x = -(this->moveScale);

// Move camera right.
if(inputManager->isKeyDown(Ogre::KC_RIGHT))
 this->translateVector.x = this->moveScale;

// Rotate camera left.
if(inputManager->isKeyDown(Ogre::KC_Q))
 this->cameraYawNode->yaw(this->rotateScale);

// Rotate camera right.
if(inputManager->isKeyDown(Ogre::KC_D))
 this->cameraYawNode->yaw(-(this->rotateScale));

// Strip all yaw rotation on the camera.
if(inputManager->isKeyDown(Ogre::KC_A))
 this->cameraYawNode->setOrientation(Ogre::Quaternion::IDENTITY);

// Rotate camera upwards.
if(inputManager->isKeyDown(Ogre::KC_Z))
 this->cameraPitchNode->pitch(this->rotateScale);

// Rotate camera downwards.
if(inputManager->isKeyDown(Ogre::KC_S))
 this->cameraPitchNode->pitch(-(this->rotateScale));

// Strip all pitch rotation on the camera.
if(inputManager->isKeyDown(Ogre::KC_E))
 this->cameraPitchNode->setOrientation(Ogre::Quaternion::IDENTITY);

// Tilt camera on the left.
if(inputManager->isKeyDown(Ogre::KC_L))
 this->cameraRollNode->roll(-(this->rotateScale));

// Tilt camera on the right.
if(inputManager->isKeyDown(Ogre::KC_M))
 this->cameraRollNode->roll(this->rotateScale);

// Strip all tilt applied to the camera.
if(inputManager->isKeyDown(Ogre::KC_P))
 this->cameraRollNode->setOrientation(Ogre::Quaternion::IDENTITY);

// Strip all rotation to the camera.
if(inputManager->isKeyDown(Ogre::KC_O))
{
 this->cameraYawNode->setOrientation(Ogre::Quaternion::IDENTITY);
 this->cameraPitchNode->setOrientation(Ogre::Quaternion::IDENTITY);
 this->cameraRollNode->setOrientation(Ogre::Quaternion::IDENTITY);
}

If you want to see what is your current orientation around each axis, here is a way to proceed: copy the following code into the FrameListener'supdateStats() method (in the try { } code block).

this->renderWindow->setDebugText("Camera orientation: (" 
 + ((this->cameraYawNode->getOrientation().y >= 0) ? std::string("+") : 
 std::string("-")) + "" + Ogre::StringConverter::toString(Ogre::Math::Floor(2 * 
 Ogre::Degree(Ogre::Math::ACos(this->cameraYawNode->getOrientation().w)).valueDegrees())) + ", " + 
 ((this->cameraPitchNode->getOrientation().x >= 0) ? std::string("+") : std::string("-")) + "" + 
 Ogre::StringConverter::toString(Ogre::Math::Floor(2 * 
 Ogre::Degree(Ogre::Math::ACos(this->cameraPitchNode->getOrientation().w)).valueDegrees())) + ", " + 
 ((this->camera->getOrientation().z >= 0) ? std::string("+") : std::string("-")) + "" + 
 Ogre::StringConverter::toString(Ogre::Math::Floor(2 * 
 Ogre::Degree(Ogre::Math::ACos(this->camera->getOrientation().w)).valueDegrees())) + ")");

That is it! Now, you should have a working first-person camera :-) Just glance at your code, ensure you have not comitted any mispelling or bad copy/paste or forgot any parts of this tutorial, compile it and enjoy!

Some remarks

Some people may argue: "Why should I bother handling a camera's roll node? I could apply the roll rotation directly to the camera!". Yes, you are right, and it would theoretically change nothing, except you would get rid of an intermediary SceneNode. To be honest, I've even done it in my own code, but let's keep that a secret, otherwise I could get flamed by the purists!

The order of the SceneNodes is important for the FPS camerasystem, so when you connect first the cameraPitchNode and then the cameraYawNode, you will get a complete different transformation. Because Matrix multiplications are not commutative. Remeber that and try it yourself.

And of course apply them separately - that is the requirement against the gimbal lock effect. For more information about the quaternions, check out theQuaternion and Rotation Primer article.

Finally, I would say that the same rule as explained in the preceding paragraph applies for the cameraNode translation (for the camera to move into the world according to the keyboard's arrow keys) about the order of the orientation quaternions (you know, the getOrientation() methods). Also, despite my very little knowledge about quaternion and vectors, you should respect the order of multiplication between the translateVector and the orientation quaternions.

Last words, you might wonder why I have not included the cameraRollNode's orientation quaternion in the multiplication given to the cameraNode's translate()method: that is because I do not want the roll rotation of my camera to influence its displacement, it is as simple as that :-)


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
目标检测(Object Detection)是计算机视觉领域的一个核心问题,其主要任务是找出图像中所有感兴趣的目标(物体),并确定它们的类别和位置。以下是对目标检测的详细阐述: 一、基本概念 目标检测的任务是解决“在哪里?是什么?”的问题,即定位出图像中目标的位置并识别出目标的类别。由于各类物体具有不同的外观、形状和姿态,加上成像时光照、遮挡等因素的干扰,目标检测一直是计算机视觉领域最具挑战性的任务之一。 二、核心问题 目标检测涉及以下几个核心问题: 分类问题:判断图像中的目标属于哪个类别。 定位问题:确定目标在图像中的具体位置。 大小问题:目标可能具有不同的大小。 形状问题:目标可能具有不同的形状。 三、算法分类 基于深度学习的目标检测算法主要分为两大类: Two-stage算法:先进行区域生成(Region Proposal),生成有可能包含待检物体的预选框(Region Proposal),再通过卷积神经网络进行样本分类。常见的Two-stage算法包括R-CNN、Fast R-CNN、Faster R-CNN等。 One-stage算法:不用生成区域提议,直接在网络中提取特征来预测物体分类和位置。常见的One-stage算法包括YOLO系列(YOLOv1、YOLOv2、YOLOv3、YOLOv4、YOLOv5等)、SSD和RetinaNet等。 四、算法原理 以YOLO系列为例,YOLO将目标检测视为回归问题,将输入图像一次性划分为多个区域,直接在输出层预测边界框和类别概率。YOLO采用卷积网络来提取特征,使用全连接层来得到预测值。其网络结构通常包含多个卷积层和全连接层,通过卷积层提取图像特征,通过全连接层输出预测结果。 五、应用领域 目标检测技术已经广泛应用于各个领域,为人们的生活带来了极大的便利。以下是一些主要的应用领域: 安全监控:在商场、银行
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值