OpenGL - 2D Movement with respondtokeypress
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9

Thread: OpenGL - 2D Movement with respondtokeypress

  1. #1
    Join Date
    Mar 2014
    Posts
    4

    OpenGL - 2D Movement with respondtokeypress

    Hello,

    I'm trying to implement keyboard controls to move a sphere(Player) with respondtokeypress.
    Currently, when I press any key my character will move to the right by 0.1. This might be a noobie question but how can I move my character with w(up),a(left),s(down),d(right) in their respective directions using respondtokeypress?
    Code:
    class Player
    {
    private:
      double x, y;
    public:
      Player(double a, double b){x=a;y=b;} 
          void respondtokeypress(char a)
      {
    	  a +=0.1;
    	  a -=0.1;
    	  b +=0.1;
    	  b -=0.1;
    	  glutPostRedisplay();
      }
      void draw()
      {
       //draw player 
       glLoadIdentity();
       glColor3f (1.0, 0.0, 0.0);
       glTranslatef(x,y,0);
       glutSolidSphere(0.03,20,20);
      }
    };
    
    void keyboard(unsigned char key, int x, int y)
    {      
    Theseus.respondtokeypress(key);
            glutPostRedisplay();
    }

  2. #2
    Join Date
    Jun 2002
    Location
    Stockholm, Sweden
    Posts
    1,592

    Re: OpenGL - 2D Movement with respondtokeypress

    The code you posted will not compile, but at the same time you say that your object moves when you press a key. This does not make sense.

    Also, the code you posted makes me think you don't understand the very basics of C++ programming (or any other programming language for that matter). I recommend you read a book or go through some tutorials.

    Regards
    Nobody cares how it works as long as it works

  3. #3
    Join Date
    Mar 2014
    Posts
    4

    Re: OpenGL - 2D Movement with respondtokeypress

    Quote Originally Posted by zerver View Post
    The code you posted will not compile, but at the same time you say that your object moves when you press a key. This does not make sense.

    Also, the code you posted makes me think you don't understand the very basics of C++ programming (or any other programming language for that matter). I recommend you read a book or go through some tutorials.

    Regards
    The code I posted is everything to do with keyboard inputs, I didn't think the rest mattered.
    I do understand c++ basics but, I'm having a hard time figuring out how to use respondtokeypress correctly.
    If anything could you explain how to use respondtokeypress with moving an object in 2d?
    If it helps I have attached my coding.
    Attached Files Attached Files

  4. #4
    Join Date
    May 2007
    Posts
    800

    Re: OpenGL - 2D Movement with respondtokeypress

    I don't think you do understand c++.

    Code:
    void respondtokeypress(char a)
    {
       a +=0.1;
       a -=0.1;
       b +=0.1;
       b -=0.1;
       glutPostRedisplay();
    }
    What are you doing here in respect to a and b? a is char but you trying to add and subtract the same float number and there is no b declared anywhere within that method.

  5. #5
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,321

    Re: OpenGL - 2D Movement with respondtokeypress

    Assuming that respondtokeypress(char a) is called when a key is pressed and that a contains the char pressed, then the function could look something like this
    Code:
    void respondtokeypress(char a)
    {
         switch (a) {
             //up
             case 'w':
                 y -= 0.1;
                 break;
    
             //down
             case 's':
                  y += 0.1;
                  break;
    
             //left
             case 'a':
                 x -= 0.1;
                 break;
    
             //right
             case 'd':
                  x += 0.1;
                  break;
         }
    }
    Depending upon the co-ordinate system, you might need to swap the-=/+= for left, right/up,down being the correct way.
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  6. #6
    Join Date
    Mar 2014
    Posts
    4

    Re: OpenGL - 2D Movement with respondtokeypress

    Quote Originally Posted by STLDude View Post
    I don't think you do understand c++.

    Code:
    void respondtokeypress(char a)
    {
       a +=0.1;
       a -=0.1;
       b +=0.1;
       b -=0.1;
       glutPostRedisplay();
    }
    What are you doing here in respect to a and b? a is char but you trying to add and subtract the same float number and there is no b declared anywhere within that method.
    Sorry for the confusion, I left out the Comments for:
    Code:
    void respondtokeypress(char a)
    {
       a +=0.1; // Left *Testing, to change when working*
      //a -=0.1; //Right 
      //b +=0.1; //Up
      //b -=0.1; //Down
       glutPostRedisplay();
    }
    And I've made more errors, My understanding was that respondtokeypress(char a) was in fact the key that moved the character. This was indeed wrong as taking into consideration 2kaud's answer and Reading a book I know understand where I have gone wrong.


    And I've made more errors, taking "2kaud's" answer I needed to implement a switch statement.

    Quote Originally Posted by 2kaud View Post
    Assuming that respondtokeypress(char a) is called when a key is pressed and that a contains the char pressed, then the function could look something like this
    Code:
    void respondtokeypress(char a)
    {
         switch (a) {
             //up
             case 'w':
                 y -= 0.1;
                 break;
    
             //down
             case 's':
                  y += 0.1;
                  break;
    
             //left
             case 'a':
                 x -= 0.1;
                 break;
    
             //right
             case 'd':
                  x += 0.1;
                  break;
         }
    }
    Depending upon the co-ordinate system, you might need to swap the-=/+= for left, right/up,down being the correct way.
    Thanks for this, it's helped me understand that I need a Switch Statement something which I won't forgot anytime soon .

    And thanks to everyone for helping me I can be a right idiot at times
    Last edited by MrBawzz; March 4th, 2014 at 12:19 PM. Reason: Thanks

  7. #7
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,564

    Re: OpenGL - 2D Movement with respondtokeypress

    FYI: Instead of implementing a callback for glutKeyboardFunc, you could implement
    a callback for glutSpecialFunc and catch the keys: GLUT_KEY_UP , GLUT_KEY_DOWN,
    GLUT_KEY_RIGHT , and GLUT_KEY_LEFT.

    The signature of the callback is slightly different. The first argument is of type int, instead
    of unsigned char.

  8. #8
    Join Date
    Jun 2002
    Location
    Stockholm, Sweden
    Posts
    1,592

    Re: OpenGL - 2D Movement with respondtokeypress

    Quote Originally Posted by MrBawzz View Post
    Thanks for this, it's helped me understand that I need a Switch Statement something which I won't forgot anytime soon
    This illustrates another problem. Now our assistance has made you think a switch statement is something awesome, and you will surely try to use it soon again. In reality, the switch statement is probably the last statement that you would learn if you study C++, because it is rarely used, a common source of bugs, and was originally included in the language mostly for performance reasons, which I will not explain here.

    Please understand that a programming language provides many ways of doing the same thing, and the bottom line is that you don't need a switch statement at all. What you instead need is basic understanding of C++, and you can demonstrate that you have acquired that by replacing the switch statement with the following and for each of them verify that it still works:
    * if statements.
    * if..else statements.
    * while loops
    * for loops

    The latter two are not recommended solutions and the code may look ugly, but it is still a good way to verify your programming skill.

    Regards
    Nobody cares how it works as long as it works

  9. #9
    Join Date
    Mar 2014
    Posts
    4

    Re: OpenGL - 2D Movement with respondtokeypress

    Quote Originally Posted by zerver View Post
    This illustrates another problem. Now our assistance has made you think a switch statement is something awesome, and you will surely try to use it soon again. In reality, the switch statement is probably the last statement that you would learn if you study C++, because it is rarely used, a common source of bugs, and was originally included in the language mostly for performance reasons, which I will not explain here.

    Please understand that a programming language provides many ways of doing the same thing, and the bottom line is that you don't need a switch statement at all. What you instead need is basic understanding of C++, and you can demonstrate that you have acquired that by replacing the switch statement with the following and for each of them verify that it still works:
    * if statements.
    * if..else statements.
    * while loops
    * for loops

    The latter two are not recommended solutions and the code may look ugly, but it is still a good way to verify your programming skill.

    Regards
    I understand that Switch Statements are not the answers to all my problems. But, for this type of problem and for the time being until I learn more about c++ a Switch statement will be adequate. In retrospect I think I need to learn more about c++ before posting my problems.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center