Ask Your Question
0

Force cv waitKey regardless of keypressing

asked 2013-08-09 15:18:58 -0500

eujono gravatar image

Hey Girls and Boys,

if have the following problem... I wan't to create a pong game in opencv. The bar should be controlled via the eye movement laterly. For now I control it with the keyboard and that's the matter.

I use the waitkey function to detect if the player moves the bar to the right or left, simultaneously the time from waitkey is needed by imshow.

What happens now is, that if the player hits a key, the game accelerates cause waitkey doesn't wait its time anymore. Unfortunately I need a fixed wait time regardless if a key is pressed or not for a constant game speed.

Heres the critical code part:

while (keypressed != 'q')
    {
        cv::Mat output = blank.clone();
        mybar.drawBar(output);
        myball.drawBall(output);
        cv::imshow("PongGame", output);

        keypressed = cv::waitKey(50);
        //std::cout << "key: " << (int)keypressed << std::endl;

        if(keypressed == 'g')
            mybar.position.x = mybar.position.x -10;
        if(keypressed == 'j')
            mybar.position.x = mybar.position.x +10;
        myball.position = myball.position + cv::Point2i(myball.xVelocity, myball.yVelocity);
        myball.checkCollision();
    }

Any Ideas?

TNX

eujono

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
1

answered 2013-08-09 18:41:03 -0500

You should used an external timer. Get the time before the waitKey call, get the time after, and wait the difference if needed. The functions will depend on your OS, but look at the C++--11x options for time, if your compiler support it.

edit flag offensive delete link more
Login/Signup to Answer

Question Tools

Stats

Asked: 2013-08-09 15:18:58 -0500

Seen: 1,177 times

Last updated: Aug 09 '13