BrainTrain
 All Classes Namespaces Files Functions Variables Macros Pages
brtr::FPSCameraManipulator Class Reference

A FPS style CameraManipulator with ground clamping and intersection. More...

#include <FPSCameraManipulator.h>

Inheritance diagram for brtr::FPSCameraManipulator:

Public Member Functions

 FPSCameraManipulator (double movementSpeed, double zHeight, osg::Node *root, bool flightMode=false)
 Constructor. More...
 
double getMovementSpeed () const
 
FPSCameraManipulatorsetMovementSpeed (double val)
 
double getZHeight () const
 
FPSCameraManipulatorsetZHeight (double val)
 
double getJumpHeight () const
 
FPSCameraManipulatorsetJumpHeight (double val)
 

Protected Member Functions

 ~FPSCameraManipulator ()
 
virtual bool handleMouseMove (const osgGA::GUIEventAdapter &ea, osgGA::GUIActionAdapter &us)
 Handles the movement of the mouse. More...
 
virtual bool handleFrame (const osgGA::GUIEventAdapter &ea, osgGA::GUIActionAdapter &us)
 Handles, what happens every frame. More...
 
virtual bool handleKeyDown (const osgGA::GUIEventAdapter &ea, osgGA::GUIActionAdapter &us)
 Handle key down presses. For supported keys see the class desc. More...
 
virtual bool handleKeyUp (const osgGA::GUIEventAdapter &ea, osgGA::GUIActionAdapter &us)
 Handle key up presses. For supported keys see the class desc. More...
 
virtual bool performMovement ()
 MouseLook is implemented in this method. More...
 
virtual bool performMovementLeftMouseButton (const double eventTimeDelta, const double dx, const double dy)
 
virtual bool handleMouseWheel (const osgGA::GUIEventAdapter &ea, osgGA::GUIActionAdapter &us)
 

Private Member Functions

bool performEyeMovement ()
 moves the cameraEye, checking various conditions More...
 
bool intersect (const osg::Vec3d start, const osg::Vec3d end, double &distance)
 Finds the distance between start and end intersection, if there is any. More...
 
bool groundIntersection (osg::Vec3d &newEye)
 checks, whether the newEye is still clamped to ground More...
 

Private Attributes

osg::ref_ptr
< osg::PositionAttitudeTransform > 
_body
 
bool _flightMode
 
bool _forwardMovement
 
bool _backwardMovement
 
bool _leftMovement
 
bool _rightMovement
 
bool _upMovement
 
bool _downMovement
 
bool _attachBody
 
bool _shift
 
bool _ctrl
 
bool _jumpingUp
 
bool _jumpingDown
 
bool _crouch
 
double _maxFallHeight
 
double _movementSpeed
 
double _zHeight
 
double _savedzHeight
 
double _intensity
 
double _frameFactor
 
double _bodyLength
 
double _jumpHeight
 
double _savedzHeightCrouch
 

Detailed Description

A FPS style CameraManipulator with ground clamping and intersection.

Controls:
                   W       = Move forward.
                 A S D     = (A)=Move Left, (S)=Move backward, (D)=Move Right.
                   F       = Toggle FlightMode on/off
                 Q / E     = Up/Down (in FlightMode)
                   G       = Attach/Detach Body
                   X       = Crouch
                 SPACE     = Jump (if not flying)
                 SHIFT     = Sprint
                 CTRL      = Walk
             
Inspiration for Intersection and Clamping Testing:
Official OSG Source (mostly DriveManipulator)
GameManipulator and Pod by Viggo Løvli, http://markmail.org/message/e6magjobl7fywbe6 , visited 26/05/2014
For Nodes, which should be passable regardless of FlightMode (e.g. FakeWalls) one must set the
NodeMask to ~brtrcollisionMask
Body Code not used anymore (we did not like it). Not deleted, because it works.
Author
Gleb Ostrowski
Version
1.0
Date
06/2014
Precondition
Need to be attached to a viewer, so, create a viewer first
Bug:
Jumping forward if there is no ground is not working

Definition at line 33 of file FPSCameraManipulator.h.

Constructor & Destructor Documentation

brtr::FPSCameraManipulator::FPSCameraManipulator ( double  movementSpeed,
double  zHeight,
osg::Node *  root,
bool  flightMode = false 
)

Constructor.

Parameters
movementSpeedthe player "movement" speed
zHeightthe camera height
rootroot node for attaching the body to, not used anymore
flightModeflightmoe true or false in the beginning

Definition at line 12 of file FPSCameraManipulator.cpp.

brtr::FPSCameraManipulator::~FPSCameraManipulator ( )
protected

Definition at line 54 of file FPSCameraManipulator.cpp.

Member Function Documentation

double brtr::FPSCameraManipulator::getJumpHeight ( ) const

Definition at line 303 of file FPSCameraManipulator.cpp.

double brtr::FPSCameraManipulator::getMovementSpeed ( ) const

Definition at line 280 of file FPSCameraManipulator.cpp.

double brtr::FPSCameraManipulator::getZHeight ( ) const

Definition at line 289 of file FPSCameraManipulator.cpp.

bool brtr::FPSCameraManipulator::groundIntersection ( osg::Vec3d &  newEye)
private

checks, whether the newEye is still clamped to ground

Performs a LineIntersectionTest from newEye to -Z_AXIS * FPSCameraManipulator::_maxFallHeight if a ground is found (any geometry within _maxFallHeight), the z-Value of newEye will be corrected to be _zHeight above ground. some smoothing is applied for not-so-abrupt jumps

Parameters
newEyethe wannabe new cameraEye Position
Returns
true, if Position is valid, false otherwise

Definition at line 246 of file FPSCameraManipulator.cpp.

bool brtr::FPSCameraManipulator::handleFrame ( const osgGA::GUIEventAdapter &  ea,
osgGA::GUIActionAdapter &  us 
)
protectedvirtual

Handles, what happens every frame.

Every frame there is a movement check, if the player holds one of the move buttons down the camera moves.

Parameters
eathe GUIEventAdapter
usthe GUIActionAdapter
Returns
always false otherwise the method would block other frame handle methods

Definition at line 63 of file FPSCameraManipulator.cpp.

bool brtr::FPSCameraManipulator::handleKeyDown ( const osgGA::GUIEventAdapter &  ea,
osgGA::GUIActionAdapter &  us 
)
protectedvirtual

Handle key down presses. For supported keys see the class desc.

Parameters
ea
us
Returns
true, if the keypress is handled, false otherwise

Definition at line 71 of file FPSCameraManipulator.cpp.

bool brtr::FPSCameraManipulator::handleKeyUp ( const osgGA::GUIEventAdapter &  ea,
osgGA::GUIActionAdapter &  us 
)
protectedvirtual

Handle key up presses. For supported keys see the class desc.

Parameters
eaea the GUIEventAdapter
usus the GUIActionAdapter
Returns
true, if the key-de-press is handled, false otherwise

Definition at line 123 of file FPSCameraManipulator.cpp.

bool brtr::FPSCameraManipulator::handleMouseMove ( const osgGA::GUIEventAdapter &  ea,
osgGA::GUIActionAdapter &  us 
)
protectedvirtual

Handles the movement of the mouse.

Warp the mouse back to the center, adds the mouse movement to the event stack and calls performMovement(). If it was succesfull, request redraw.

Parameters
eathe GUIEventAdapter
usthe GUIActionAdapter
Returns
always false otherwise the method would block other mouse movement handle methods

Definition at line 56 of file FPSCameraManipulator.cpp.

bool brtr::FPSCameraManipulator::handleMouseWheel ( const osgGA::GUIEventAdapter &  ea,
osgGA::GUIActionAdapter &  us 
)
protectedvirtual

Definition at line 311 of file FPSCameraManipulator.cpp.

bool brtr::FPSCameraManipulator::intersect ( const osg::Vec3d  start,
const osg::Vec3d  end,
double &  distance 
)
private

Finds the distance between start and end intersection, if there is any.

performs a LineIntersectionTest in Model coordinates, from intersect::start to intersect::end the distance to the nearest intersection point, if any exist, is stored in intersect::distance

Parameters
startthe start point of the line
endthe end point of the line
distancevar, which will hold the distance to the nearest intersection point, if any
Returns
true, if there is at least one intersection, false otherwise

Definition at line 263 of file FPSCameraManipulator.cpp.

bool brtr::FPSCameraManipulator::performEyeMovement ( )
private

moves the cameraEye, checking various conditions

directions + speed depends on the pressed keys. if flightmode is off, clamping to ground is performed, _zHeight is height of eye also, if flightmode is off, LineIntersection from the eye is performed so no walking trough walls again, with flightmode off, jumping request is also handled if flightmode is on, then just moves the cameraEye

Returns
true, if there was any movement, false otherwise

Definition at line 170 of file FPSCameraManipulator.cpp.

bool brtr::FPSCameraManipulator::performMovement ( )
protectedvirtual

MouseLook is implemented in this method.

Because the mouse is always at the center, it is enough to get the previous mouse position (_ga_t0, the current position is always the center = _ga_t1) and rotate the camera to that position.

Returns
true, if there was any movement, false otherwise

Definition at line 154 of file FPSCameraManipulator.cpp.

bool brtr::FPSCameraManipulator::performMovementLeftMouseButton ( const double  eventTimeDelta,
const double  dx,
const double  dy 
)
protectedvirtual

Definition at line 307 of file FPSCameraManipulator.cpp.

FPSCameraManipulator & brtr::FPSCameraManipulator::setJumpHeight ( double  val)

Definition at line 298 of file FPSCameraManipulator.cpp.

FPSCameraManipulator & brtr::FPSCameraManipulator::setMovementSpeed ( double  val)

Definition at line 284 of file FPSCameraManipulator.cpp.

FPSCameraManipulator & brtr::FPSCameraManipulator::setZHeight ( double  val)

Definition at line 293 of file FPSCameraManipulator.cpp.

Member Data Documentation

bool brtr::FPSCameraManipulator::_attachBody
private

Definition at line 158 of file FPSCameraManipulator.h.

bool brtr::FPSCameraManipulator::_backwardMovement
private

Definition at line 153 of file FPSCameraManipulator.h.

osg::ref_ptr<osg::PositionAttitudeTransform> brtr::FPSCameraManipulator::_body
private

Definition at line 150 of file FPSCameraManipulator.h.

double brtr::FPSCameraManipulator::_bodyLength
private

Definition at line 170 of file FPSCameraManipulator.h.

bool brtr::FPSCameraManipulator::_crouch
private

Definition at line 163 of file FPSCameraManipulator.h.

bool brtr::FPSCameraManipulator::_ctrl
private

Definition at line 160 of file FPSCameraManipulator.h.

bool brtr::FPSCameraManipulator::_downMovement
private

Definition at line 157 of file FPSCameraManipulator.h.

bool brtr::FPSCameraManipulator::_flightMode
private

Definition at line 151 of file FPSCameraManipulator.h.

bool brtr::FPSCameraManipulator::_forwardMovement
private

Definition at line 152 of file FPSCameraManipulator.h.

double brtr::FPSCameraManipulator::_frameFactor
private

Definition at line 169 of file FPSCameraManipulator.h.

double brtr::FPSCameraManipulator::_intensity
private

Definition at line 168 of file FPSCameraManipulator.h.

double brtr::FPSCameraManipulator::_jumpHeight
private

Definition at line 171 of file FPSCameraManipulator.h.

bool brtr::FPSCameraManipulator::_jumpingDown
private

Definition at line 162 of file FPSCameraManipulator.h.

bool brtr::FPSCameraManipulator::_jumpingUp
private

Definition at line 161 of file FPSCameraManipulator.h.

bool brtr::FPSCameraManipulator::_leftMovement
private

Definition at line 154 of file FPSCameraManipulator.h.

double brtr::FPSCameraManipulator::_maxFallHeight
private

Definition at line 164 of file FPSCameraManipulator.h.

double brtr::FPSCameraManipulator::_movementSpeed
private

Definition at line 165 of file FPSCameraManipulator.h.

bool brtr::FPSCameraManipulator::_rightMovement
private

Definition at line 155 of file FPSCameraManipulator.h.

double brtr::FPSCameraManipulator::_savedzHeight
private

Definition at line 167 of file FPSCameraManipulator.h.

double brtr::FPSCameraManipulator::_savedzHeightCrouch
private

Definition at line 172 of file FPSCameraManipulator.h.

bool brtr::FPSCameraManipulator::_shift
private

Definition at line 159 of file FPSCameraManipulator.h.

bool brtr::FPSCameraManipulator::_upMovement
private

Definition at line 156 of file FPSCameraManipulator.h.

double brtr::FPSCameraManipulator::_zHeight
private

Definition at line 166 of file FPSCameraManipulator.h.


The documentation for this class was generated from the following files: