A graphics related question

This is a discussion on A graphics related question within the C++ Programming forums, part of the General Programming Boards category; Hi guys, I'm using opengl to create a 2-player pong game. I've added controls to both players. One player is ...

  1. #1
    Registered User
    Join Date
    Nov 2007
    Posts
    34

    A graphics related question

    Hi guys,

    I'm using opengl to create a 2-player pong game.

    I've added controls to both players. One player is using the 'A' and 'D' keys to control his racket while the other is using the left and right arrow keys.

    The key event code is like this:
    Code:
    void dKeyPressed(int key, int x, int y) 
    {
    	switch (key) 
    	{
    		case GLUT_KEY_LEFT:
    			drawer->moveBat(0, vl);
    			break;
    		case GLUT_KEY_RIGHT:
    			drawer->moveBat(0, vr);
    			break;
    	}
    }
    
    void nKeyPressed(unsigned char key, int x, int y)
    {
    	if(key==97||key==65) 
    	{
    		drawer->moveBat(1, vl);
    	}
    	if(key==68||key==100)
    	{
    		drawer->moveBat(1, vr);
    	}
    	if(key==32)
    	{
    		drawer->restart();
    	}
    }
    and the moveBat in the Drawer class is like this:
    Code:
    void Drawer::moveBat(int bat_id, cyclone::Vector3 &force)
    {
    	game->bat[bat_id]->body->addForce(force);
    }
    Each time a key is pressed, a certain amount of force is added to the racket to make it move. I'm using the cyclone physics engine, the addForce(Vector3 &f) function is provided by the engine.

    I noticed that if I press key 'a' and the left arrow key at the same time, only one racket is moving. If I try to press the key again to make the other one move, the current one stops.

    Any idea what am I doing wrong here?

    Thanks!

  2. #2
    Registered User
    Join Date
    Oct 2006
    Location
    UK/Norway
    Posts
    485
    Code:
    	switch (key) 
    	{
    		case GLUT_KEY_LEFT:
    			drawer->moveBat(0, vl);
    			break;
    		case GLUT_KEY_RIGHT:
    			drawer->moveBat(0, vr);
    			break;
    	}
    If one key is found, it will break out of the switch statement
    Try something like this (I am sure there are better ways of doing it)
    Code:
    if(GLUT_KEY_LEFT)
       drawer->moveBat(0, vl);
    if(GLUT_KEY_RIGHT)
       drawer->moveBat(0, vr);

  3. #3
    Registered User
    Join Date
    Nov 2007
    Posts
    34
    Thanks for the tip. But that's not the problem I'm having, I still can't move 2 rackets at the same time.

  4. #4
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,230
    Quote Originally Posted by h3ro View Post
    Code:
    	switch (key) 
    	{
    		case GLUT_KEY_LEFT:
    			drawer->moveBat(0, vl);
    			break;
    		case GLUT_KEY_RIGHT:
    			drawer->moveBat(0, vr);
    			break;
    	}
    If one key is found, it will break out of the switch statement
    Try something like this (I am sure there are better ways of doing it)
    Code:
    if(GLUT_KEY_LEFT)
       drawer->moveBat(0, vl);
    if(GLUT_KEY_RIGHT)
       drawer->moveBat(0, vr);
    That can't work. The switch statement compares exact equality, it's not like "flag checking." If key == GLUT_KEY_LEFT, then it can't possibly be true that key == GLUT_KEY_RIGHT.

    I suspect that GLUT doesn't support reporting multiple keydowns at the same time.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  5. #5
    Registered User
    Join Date
    Oct 2006
    Location
    UK/Norway
    Posts
    485
    That can't work. The switch statement compares exact equality, it's not like "flag checking." If key == GLUT_KEY_LEFT, then it can't possibly be true that key == GLUT_KEY_RIGHT.

    I suspect that GLUT doesn't support reporting multiple keydowns at the same time.
    You are right. I missed that when typing up my reply.

  6. #6
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,590
    switch() in this instance will certainly guarantee you can only handle 1 key press at a time.

  7. #7
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Wouldn't that limitation make GLUT practically useless as far as game development is concerned?

    I haven't used it, but what the code does is that the functions receive a single char (key) which we don't know whence it came from as input and then pass it through a switch for a test. Unless you call it with different keys in a loop, how's it supposed to work.

    I would assume that the whole thing should look more like the following

    Code:
    void HandleInput() 
    {
    	if (QueryKeyStatusSomehow(GLUT_KEY_LEFT)
    	    drawer->moveBat(0, vl);
    	if (QueryKeyStatusSomehow(GLUT_KEY_RIGHT)
                drawer->moveBat(0, vr);
            if (QueryKeyStatusSomehow(GLUT_KEY_A?)
                drawer->moveBat(1, vl);
    	...
    }
    As a side note, it seems that you are drawing the bats as you process input? In game development it is usually recommended to keep graphics separate from logic (which input processing is). So what you should be doing here is actually only modifying the coordinates of the bats (perhaps you might also do collision checking to see that they don't leave the window) and drawing the scene only after you have processed all the logic (input, bat/ball movement, collisions and collision response).
    Last edited by anon; 12-18-2008 at 05:47 PM.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  8. #8
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A question related to dynamic memory allocation
    By spiit231 in forum C Programming
    Replies: 2
    Last Post: 03-11-2008, 12:25 AM
  2. Noobish question about graphics
    By ThWolf in forum Game Programming
    Replies: 5
    Last Post: 09-01-2006, 11:40 AM
  3. Architecture Question
    By Orborde in forum C++ Programming
    Replies: 1
    Last Post: 06-01-2005, 08:05 AM
  4. Question related to array and pointers
    By Q4u in forum C++ Programming
    Replies: 6
    Last Post: 07-26-2002, 12:54 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21