It is currently Tue Jul 25, 2017 1:50 am

All times are UTC - 5 hours




 Page 1 of 1 [ 6 posts ] 
Author Message
 Post subject: Caligari VMK 2 User Interface
PostPosted: Tue Feb 13, 2007 7:05 am 
Site Admin

Joined: Sun Feb 11, 2007 8:59 am
Posts: 1102
Location: Ontario Canada
This VMK shows you how to add keyboard controls to your scene so that you can move around in the 3D world.


Offline
 Profile  
 
 Post subject: Keyboard here, keyboard there
PostPosted: Sun Sep 14, 2008 4:16 am 

Joined: Wed Aug 06, 2008 7:53 pm
Posts: 182
Location: Russia
First, a coupe remarks.

1). To initialize m_bKeys[256] in ctor GameOGL::GameOGL() instead of the cycle next code can be used (that is nothing new):
memset( m_bKeys, 0, sizeof(m_bKeys) );
In general, memset, memcpy, memmove, memcmp RTL function are written in pure assembler, highly optimized and very fast.
From the other side, if we make bool m_bKeys[256] global static data inside GameOGL.cpp, the compiler will initialize the array with zeros for us. :)

2). I think that all the troubles with typing VK_KEYx-set is superfluous. I mean if we need to handle say Z-key we can write 'Z' instead of VK_KEYZ and so on. Again, that is noncritical.

3). To close the app by pressing ESC-key we should add in GameOGL::HandleKeyboard() one more line of code:
if (m_bKeys[VK_ESCAPE]) // TEMPORARY!
    m_bQuit = true;

I know that later the ESC will be used for other purposes. But so long as the development stage is in full blast we can gain the advantages of quick escape. :)

Second, I would like to show my keyboard handler. It does the same as bool m_bKeys[256] , and I know that it is very probably that later it will be replaced with something more appropriate, such as std::map() and the like. But at present we can play around the array.

Initially, I have decided to rewrite the code to use bits to store key-press-flags. In such case the size of array is reduced to 32 bytes = 256 / 8. There are some advantages in smaller working set, i.e. smaller data better “fitâ€


Offline
 Profile  
 
 Post subject: Keyboard here, keyboard there. Part II.
PostPosted: Sun Sep 14, 2008 4:22 am 

Joined: Wed Aug 06, 2008 7:53 pm
Posts: 182
Location: Russia
Keyboard handler class code:
//------------------------------------------------------------------------------
// Class Keyboard   - represents the state of all 256 virtual keys of keyboard.
//
// Interface:
//      bool Get( key ) - get key state
//      void Set( key ) - set key state to true
//      void Reset( key ) - set key state to false (reset)
//      operator( key ) - get key state (alias for get())
//      operator( key, state ) - set key state to true or false
//
// Specialization:
//      General case - working set is T[256 * sizeof(T)].
//      There is one special case - TKeyboard<BYTE> packs the data: BYTE[32].
//      Pros and cons: bitwise class is space effective and better fits into CPU
//      cache, but has worse time efficiency. bool-based has antipodal traits.
//------------------------------------------------------------------------------
template< typename T > class TKeyboard
{
    template< typename T> class Keys // general case
    {   
        enum { SIZE = 256 };
        T o[SIZE];
    public:
        Keys() { clear(); }
        void clear() { ::memset( o, 0, SIZE * sizeof(T) ); }
        bool get(BYTE pos) const { return 0 != o[ pos ]; }
        void set(BYTE pos) { o[ pos ] = true; }
        void reset(BYTE pos) { o[ pos ] = false; }
    };
   
    template<> class Keys< typename BYTE > // specialization
    {   
        enum { SIZE = 32 };
        BYTE o[SIZE];
        BYTE mask(BYTE pos) const { return BYTE(1 << (pos & 7)); }
    public:
#    if _MSC_VER <= 1200 // VC6
        template<> Keys< typename BYTE > () { clear(); }
#    else // VC7+ and some other compilers
        Keys () { clear(); }
#    endif
        void clear() { ::memset( o, 0, SIZE ); }
        bool get(BYTE pos) const { return (0 != (( mask(pos) & o[ pos >> 3 ]))); }
        void set(BYTE pos) { o[ pos >> 3 ] |= mask(pos); }
        void reset(BYTE pos) { o[ pos >> 3 ] &= ~mask(pos); }
    };
   
    Keys< T > keys;
   
public:
    bool Get(BYTE key) const { return keys.get( key ); }
    void Set(BYTE key) { keys.set( key ); }
    void Reset(BYTE key) { keys.reset( key ); }
    void Clear() { keys.clear();}
    bool operator[] (BYTE key) const { return Get(key); }
    bool operator() (BYTE key) const { return Get(key); } // alias
    void operator() (BYTE key, bool state) { state ? Set(key) : Reset(key); }
};

typedef TKeyboard<bool> KbBool;
typedef TKeyboard<BYTE> KbBits;


The use (almost the same as in the reference case):
// in GameOGL.h
KbBits m_keys;

// in GameOGL.cpp
void GameOGL::HandleKeyboard()
{
    if (m_keys[VK_ESCAPE]) // TEMPORARY!
        m_quit = true;

    if (m_keys[VK_UP] || m_keys['W'])
        m_player.Move(MOVE_FORWARD, m_timeDelta);
    if (m_keys[VK_DOWN] || m_keys['S'])
        m_player.Move(MOVE_BACK, m_timeDelta);
    if (m_keys[VK_LEFT] || m_keys['A'])
        m_player.Move(MOVE_LEFT, m_timeDelta);
    if (m_keys[VK_RIGHT] || m_keys['D'])
        m_player.Move(MOVE_RIGHT, m_timeDelta);
    if (m_keys['Q'])
        m_player.Move(LOOK_LEFT, m_timeDelta);
    if (m_keys['E'])
        m_player.Move(LOOK_RIGHT, m_timeDelta);
    if (m_keys[VK_SPACE])
        m_player.Jump(m_timeGame);
...
    m_player.Crouch(m_timeGame, m_keys['C'] ? Player::CROUCH_DOWN : Player::CROUCH_UP);
}

// in message handler:
...
case WM_KEYDOWN:
    m_keys.Set( BYTE(wparam) );
    return 0;
case WM_KEYUP:
    m_keys.Reset( BYTE(wparam) );
    return 0;


8)


Offline
 Profile  
 
 Post subject: Re: GameDev VMK 9 - Keyboard Control
PostPosted: Sat May 21, 2011 7:54 am 

Joined: Thu Apr 14, 2011 6:41 pm
Posts: 4
Hi Marek

I seem to be having problem with the keyboard controls.
When I run it, I can move forward and back and side to side but when I turn left or right. It somehow reverse the move controls, so forward is now back and back is forward and vice versa with the side to side controls.

Any idea what would be causing this?

Thanks
IC


Offline
 Profile  
 
 Post subject: Re: GameDev VMK 9 - Keyboard Control
PostPosted: Sat May 21, 2011 11:02 am 
Site Admin

Joined: Sun Feb 11, 2007 8:59 am
Posts: 1102
Location: Ontario Canada
It sounds to me like the equations for the rotation matix may be incorrect in your code.... either that, or you're pushing a rotation onto your stack but you forgot to pop it back off.

Since you know what is causing the problem, setup a break point in your code right before you calculate your rotation matrix and then use the debugger to make sure that the values that you are calculating are what you are expecting.


Offline
 Profile  
 
 Post subject: Re: GameDev VMK 9 - Keyboard Control
PostPosted: Sun May 22, 2011 11:35 pm 

Joined: Thu Apr 14, 2011 6:41 pm
Posts: 4
You were right, it was a equation in the rotation matrix. I've been left a bit confused when I was debugging this one but I think I should take your recommendation at heart and spend some time on learning about 3D Vectors. The mathematics is a big part of your VMKs.

Thanks again
IC


Offline
 Profile  
 
Display posts from previous:  Sort by  
 Page 1 of 1 [ 6 posts ] 

All times are UTC - 5 hours


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Jump to:  

cron