gJoystick Class Reference

special joystick controls for the lightcycles. Simple left-right controls More...

#include <gJoystick.h>

Collaboration diagram for gJoystick:

Collaboration graph
[legend]

List of all members.

Public Member Functions

 gJoystick (gCycleMovement *cycle)
bool Act (uActionPlayer *act, REAL value)
 process input events
void Turn ()
 turn the cycle

Private Member Functions

bool ActInternal (uActionPlayer *act, REAL value)

Private Attributes

gCycleMovementcycle_
 the cycle controlled by this joystick
tCoord cameraDirection_
 the direction the camera is supposed to look in
tCoord driveDirection_
 the direction the cycle is supposed to drive in
tCoord joyDirection_
 direction the joystick is currently pointing in
double lastCommand_
 last time the joystick was clearly pushed into some direction
int lastTurn_
 last ordered turn direction
bool glance_
 whether the glance button is pressed
bool turnRequested_
 internal flag indicating further turns may be required

Friends

class gCycle


Detailed Description

special joystick controls for the lightcycles. Simple left-right controls

Definition at line 40 of file gJoystick.h.


Constructor & Destructor Documentation

gJoystick::gJoystick ( gCycleMovement cycle  )  [inline]

Definition at line 44 of file gJoystick.h.

00045     : cycle_( cycle ), lastCommand_( 0 ), lastTurn_(0), glance_( false ), turnRequested_( false )
00046     {
00047     }


Member Function Documentation

bool gJoystick::Act ( uActionPlayer act,
REAL  value 
)

process input events

Definition at line 42 of file gJoystick.cpp.

References ActInternal(), eGameObject::CamDir(), cameraDirection_, tCoord::Conj(), cycle_, gCycleMovement::Direction(), driveDirection_, glance_, joyDirection_, lastCommand_, tCoord::Normalize(), tCoord::NormSquared(), tSysTimeFloat(), tCoord::Turn(), and turnRequested_.

Referenced by gCycle::Act().

00043 {
00044     if ( joyDirection_.NormSquared() > .5 )
00045     {
00046         lastCommand_ = tSysTimeFloat();
00047     }
00048 
00049     if ( ActInternal( act, value ) )
00050     {
00051         // update directions
00052         if ( joyDirection_.NormSquared() > .5 )
00053         {
00054             // fetch driving direction if required
00055             if ( driveDirection_.NormSquared() < .1 )
00056             {
00057                 // con << joyDirection_ << "\n";
00058 
00059                 driveDirection_ = cycle_->Direction();
00060                 driveDirection_.Normalize();
00061             }
00062 
00063             // fetch camera direction if required
00064             if ( cameraDirection_.NormSquared() < .1 )
00065             {
00066                 cameraDirection_ = cycle_->CamDir();
00067                 cameraDirection_.Normalize();
00068             }
00069 
00070             tCoord dir = joyDirection_;
00071             dir.Normalize();
00072             if ( glance_ )
00073             {
00074                 // adapt viewing direction to driving direction
00075                 cameraDirection_ = driveDirection_.Turn( dir.Conj() );
00076             }
00077             else
00078             {
00079                 // adapt driving direction to view direction
00080                 driveDirection_ = cameraDirection_.Turn( dir );
00081             }
00082 
00083             // possibly turn
00084             turnRequested_ = true;
00085             // Turn();
00086         }
00087 
00088         return true;
00089     }
00090 
00091     return false;
00092 }

Here is the call graph for this function:

Here is the caller graph for this function:

void gJoystick::Turn (  ) 

turn the cycle

Definition at line 95 of file gJoystick.cpp.

References cameraDirection_, gCycleMovement::CanMakeTurn(), cycle_, driveDirection_, eCoord, eGrid::GetDirection(), eGameObject::Grid(), joyDirection_, lastCommand_, lastTurn_, tCoord::NormSquared(), REAL, tSysTimeFloat(), gCycleMovement::Turn(), eGrid::Turn(), turnRequested_, and gCycleMovement::WindingNumber().

Referenced by gCycle::TimestepCore().

00096 {
00097     if ( joyDirection_.NormSquared() < .1 && lastCommand_ < tSysTimeFloat() - .03 )
00098     {
00099         // if ( driveDirection_.NormSquared() > .5 )
00100         // con << joyDirection_ << "\n";
00101 
00102         // joystick was released, reeset driving direction and camera direction.
00103         driveDirection_ = tCoord();
00104         cameraDirection_ = tCoord();
00105         return;
00106     }
00107 
00108     // nothing to do
00109     if ( !turnRequested_ )
00110     {
00111         return;
00112     }
00113 
00114     // fetch current and possible turn driving directions
00115     int winding = cycle_->WindingNumber();
00116     int windingLeft = winding, windingRight = winding;
00117     eGrid * grid = cycle_->Grid();
00118     grid->Turn( windingLeft, -1 );
00119     grid->Turn( windingRight, +1 );
00120     eCoord leftTurn = grid->GetDirection( windingLeft );
00121     eCoord rightTurn = grid->GetDirection( windingRight );
00122     eCoord straightOn = grid->GetDirection( winding );
00123 
00124     // normalize them (axes may be unnormalized)
00125     leftTurn.Normalize();
00126     rightTurn.Normalize();
00127     straightOn.Normalize();
00128 
00129     // calculate distances between desired and possible driving directions
00130     REAL left     = ( leftTurn - driveDirection_ ).NormSquared();
00131     REAL right    = ( rightTurn - driveDirection_ ).NormSquared();
00132     REAL straight = ( straightOn - driveDirection_ ).NormSquared();
00133 
00134     // possibly turn
00135     if ( left < right && left < straight * ( lastTurn_ != 1 ? 2 : .8 ) )
00136     {
00137         if ( cycle_->CanMakeTurn( -1 ) )
00138         {
00139             cycle_->Turn( -1 );
00140             lastTurn_ = -1;
00141         }
00142     }
00143     else if ( left > right && right < straight * ( lastTurn_ != -1 ? 2 : .8 )  )
00144     {
00145         if( cycle_->CanMakeTurn( +1 ) )
00146         {
00147             cycle_->Turn( +1 );
00148             lastTurn_ = +1;
00149         }
00150     }
00151     else
00152     {
00153         // not much use repeating this calculation until new input arrives
00154         turnRequested_ = false;
00155         lastTurn_ = 0;
00156     }
00157 }

Here is the call graph for this function:

Here is the caller graph for this function:

bool gJoystick::ActInternal ( uActionPlayer act,
REAL  value 
) [private]

Definition at line 159 of file gJoystick.cpp.

References glance_, joyDirection_, sg_JoyDown, sg_JoyGlance, sg_JoyLeft, sg_JoyRight, sg_JoyUp, tCoord::x, and tCoord::y.

Referenced by Act().

00160 {
00161     if ( act == & sg_JoyUp )
00162     {
00163         joyDirection_.x = value;
00164         return true;
00165     }
00166     if ( act == & sg_JoyDown )
00167     {
00168         joyDirection_.x = -value;
00169         return true;
00170     }
00171     if ( act == & sg_JoyLeft )
00172     {
00173         joyDirection_.y = value;
00174         return true;
00175     }
00176     if ( act == & sg_JoyRight )
00177     {
00178         joyDirection_.y = -value;
00179         return true;
00180     }
00181     if ( act == & sg_JoyGlance )
00182     {
00183         glance_ = ( value > .5 );
00184         return true;
00185     }
00186 
00187     return false;
00188 }

Here is the caller graph for this function:


Friends And Related Function Documentation

friend class gCycle [friend]

Definition at line 42 of file gJoystick.h.


Member Data Documentation

gCycleMovement* gJoystick::cycle_ [private]

the cycle controlled by this joystick

Definition at line 57 of file gJoystick.h.

Referenced by Act(), and Turn().

tCoord gJoystick::cameraDirection_ [private]

the direction the camera is supposed to look in

Definition at line 58 of file gJoystick.h.

Referenced by Act(), gCycle::CamDir(), gCycle::Direction(), and Turn().

tCoord gJoystick::driveDirection_ [private]

the direction the cycle is supposed to drive in

Definition at line 59 of file gJoystick.h.

Referenced by Act(), and Turn().

tCoord gJoystick::joyDirection_ [private]

direction the joystick is currently pointing in

Definition at line 60 of file gJoystick.h.

Referenced by Act(), ActInternal(), and Turn().

double gJoystick::lastCommand_ [private]

last time the joystick was clearly pushed into some direction

Definition at line 62 of file gJoystick.h.

Referenced by Act(), and Turn().

int gJoystick::lastTurn_ [private]

last ordered turn direction

Definition at line 63 of file gJoystick.h.

Referenced by Turn().

bool gJoystick::glance_ [private]

whether the glance button is pressed

Definition at line 65 of file gJoystick.h.

Referenced by Act(), and ActInternal().

bool gJoystick::turnRequested_ [private]

internal flag indicating further turns may be required

Definition at line 66 of file gJoystick.h.

Referenced by Act(), and Turn().


The documentation for this class was generated from the following files:
Generated on Sat Mar 15 23:39:59 2008 for Armagetron Advanced by  doxygen 1.5.4