OpenSceneGraph:第三人称视角

问题描述 投票:1回答:1

目标:使用OpenSceneGraph(OSG)创建第三人称视图,其中包括控制模型。该模型可以使用WASD和鼠标移动。单击鼠标右键时,相机和视图应该更改。按住鼠标右键时,模型和相机的方向应相同。

我现在一直在研究这个问题,我可以同时改变模型和相机的方向,但我只能让相机显示自上而下的视图。我希望相机能够以其他角度渲染世界(距世界平面0-90度)。

bool EventHandlerMouse::performMovementRightMouseButton( 
    const double eventTimeDelta, 
    const double dx, 
    const double dy
) {

// Read in the mouse movement to determine if the user is turning left 
// or right. Then set the bool attribute of this->_screen_model to true
// for the direction that the user is moving and then sets the other to
// false. The else statement turns them both off.
if      ( dx < -0.01 ) { 
    this->_screen_model->turn_left();
    this->_screen_model->stop_turn_right();
}
else if ( dx >  0.01 ) {
    this->_screen_model->turn_right();
    this->_screen_model->stop_turn_left();
}
else {
    this->_screen_model->stop_turn_right();
    this->_screen_model->stop_turn_left();
}

// Get the model's orientation. Orientation is a float between 0.0 and
// 360.0 representing the models orientation. 0.0 and 360.0 are true 
// north.
float local_o = this->_screen_model->get_orientation();

// Create the OSG variable to hold the camera manipulator's current and 
// future rotation.
osg::Quat rotation, new_rotation;
osg::Vec3d eye;

// Retrieve the camera's current rotation and eye.
this->getTransformation( eye, rotation );

// Retrieve the camera's rotation matrix.
osg::Matrixd rotation_matrix;
rotation.get( rotation_matrix );

new_rotation = rotation_matrix.getRotate();

// I don't really know what makeRotate does. I couldn't find good
// documentation. From what I'm seeing, it takes the current attitude
// ( rotation ) and changes it to what you have specified. I think this 
// is where my problem/solution will be. Here, I set the angle to the
// orientation of the model. I am then setting that angle to the z axis in
// the Vec3d. I think that this is what is giving me the top down view,
// but it does turn with the model. I don't know how I can change this so
// that it gives a view that would be at an over the shoulder type of
// angle similar to a typical 3rd person view.
new_rotation.makeRotate( 
    osg::DegreesToRadians( local_o ), Vec3d( 0.0, 0.0, 1.0 )
    );

// Setting the camera's eye and rotation to the newly calculated
// perspective.
this->setTransformation( eye, new_rotation );
}

我仍然很擅长使用OSG。我正在阅读书籍并完成教程。我一直在寻找如何做我想做的事,但我没有成功。如有任何帮助,我将不胜感激。如果我可以从我刚刚起步的应用程序的任何其他部分提供代码片段,请告诉我您想要看到的内容。

c++ openscenegraph
1个回答
1
投票

我最终通过一个相当简单的NodeTrackManipulator实现获得了所需的结果。我刚刚写了一些鼠标处理,它符合我的需要。

event_handler_mouse.hpp

#ifndef EVENT_HANDLER_MOUSE_H
#define EVENT_HANDLER_MOUSE_H

#include <osgGA/GUIEventHandler>
#include <osgGA/NodeTrackerManipulator>
#include <osgViewer/View>
#include <iostream>

#include "../headers/client_obj_char.hpp"

using std::cout;
using std::endl;


class EventHandlerMouse : public osgGA::NodeTrackerManipulator {
    public:
        virtual bool performMovementLeftMouseButton(
            const double eventTimeDelta, 
            const double dx, 
            const double dy 
            );

        virtual bool performMovementRightMouseButton(
                const double eventTimeDelta, 
                const double dx, 
                const double dy
            );

        virtual bool performMovementMiddleMouseButton(
                const double eventTimeDelta, 
                const double dx, 
                const double dy 
            );

        EventHandlerMouse( ClientObjChar* sm ) :
            _client_char( sm ),
            _distance( 1.0 )
            {}
protected:
        virtual ~EventHandlerMouse() {}

        osg::Vec3      _center;
        double         _distance;
        ClientObjChar* _client_char;

        osg::observer_ptr<osg::Node> _target;
};


#endif

event_handler_mouse.cpp

#include "../headers/event_handler_mouse.hpp"


bool EventHandlerMouse::performMovementLeftMouseButton( 
        const double eventTimeDelta, 
        const double dx, 
        const double dy
    ) {

    rotateWithFixedVertical( dx, dy );
    return true;
}

bool EventHandlerMouse::performMovementRightMouseButton( 
        const double eventTimeDelta, 
        const double dx, 
        const double dy
    ) {
    if ( true ) {
        if      ( dx < -0.01 ) { 
            this->_client_char->turn_left();
            this->_client_char->stop_turn_right();
        }
        else if ( dx >  0.01 ) {
            this->_client_char->turn_right();
            this->_client_char->stop_turn_left();
        }
        else {
            this->_client_char->stop_turn_right();
            this->_client_char->stop_turn_left();
        }
    }
    return true;
}


bool EventHandlerMouse::performMovementMiddleMouseButton(
        const double eventTimeDelta, 
        const double dx, 
        const double dy 
    ) {
    return false;
}
© www.soinside.com 2019 - 2024. All rights reserved.