• Advertisement
Sign in to follow this  

Keyboard triggers animation

This topic is 2035 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi everyone!

I needed a little help with a similar question this morning, but in this one I'm really stuck and don't know what I'm doing, so any help will be appreciated.

I have to move smoothly an object from one side of my 3d room to another, every time a single key is pressed. I declare a global variable with the initial x position of it, and then I render it:

[source lang="cpp"]glPushMatrix();
glTranslatef(positionObject, 1, -6);
object->Render();
glPopMatrix();[/source]

So I guess that, for the smooth movement, I'll need a timer that shows the object position several times per second, right? I'm doing it like this (not the proper way to do it, I'm aware):

[source lang="cpp"]void Timer (int value)
{
if (positionObject == -1.5)
{
while (positionObject <= -2.3)
positionObject += -0.1;
}
else
{
while (positionObject != -1.5)
positionObject -= -0.1;
}

glutPostRedisplay();
glutTimerFunc (33, Timer, 1);
}[/source]

And my final doubt is: what do I call in my keyboard callback function for it to work?

[source lang="cpp"]void KeyboardCallback (unsigned char key, int x, int y){
switch (key)
{
case 'r':
case 'R':
//do sth;
break;
}
}[/source]

Share this post


Link to post
Share on other sites
Advertisement
I'm sorry, my Timer function didn't appear correctly, so I'm posting it here:

[source lang="cpp"]void Timer(int value) {
if (positionObject == -1.5)
{
while (positionObject <= -2.3)
positionObject += -0.1;
}
else
{
while (positionObject != -1.5)
positionObject -= -0.1;
}

glutPostRedisplay();
glutTimerFunc(33, Timer, 1);
}[/source]

Share this post


Link to post
Share on other sites
Um.... Your Timer is still posted broken.

How do you call Timer? How is your code structured?

Do you have a game loop? That while statement in the Timer looks extremely suspicious, it looks like you intend to move the object all the way inside Timer? Or are you using some nested gameloop? How?

"Intend to move" (?), because you use an if statement with a comparison that will be never true probably. I don't even get the intention really, what are you trying to do when the position is equal with -1.5.

The use of timer implies that you have a event driven game loop (advancing only if an input event happens) instead of a continuously running game loop. Objects and rooms imply imply the need of a continuously running game loop.



Post the whole stuff and sorry for the random questions and things. Edited by szecs

Share this post


Link to post
Share on other sites
Sorry, hadn't seen the code was still posted broken. I'll try it again:

[source lang="cpp"]void Timer(int value)
{
if (posInitialxObject == -1.5)
{
while (posInitialxObject != posFinalxObject)
posInitialxObject += -0.1;
}
else
{
while (posFinalxObject != posInitialxObject)
posInitialxObject -= -0.1;
}

glutPostRedisplay();
glutTimerFunc(33, Timer, 1);
}[/source]

The -1.5 was the initial X position of the object on the screen. Until that position was different then -2.3, the X variable should decrease -0.1, making it translate. Now I've created two global variables w/ the initial and final position, so I'm comparing these two.

Share this post


Link to post
Share on other sites
Read my previous post again.
1. comparison that will be never true probably.

2. you are doing all the movement in the while loop. So even if the while statement would evaluate to false, it would mean that the object will be instantly in the new position. But since the comparisons that will be never true probably, you are more likely get stuck in an infinite loop. (Plus a sidenote, in cases like this ("increase some value until it reaches a certain value") I would use '<' or '<=' instead of '!='. Because it's pretty easy to make the mistake of jumping over the value even with integers, so that the statement will never evaluate to false.)
To solve this, you need to check and advance the motion in the main game loop or in the TimerFunc but only with the small, time dependant value (so not with a while, but an if statement).

3. Are you sure you don't want a continuously running game loop? If you want a continuously running game loop, then don't use a TimerFunc, but an IdleFunc with proper timing (for starters, see "Game Speed dependent on Variable FPS" section of the linked article).

It's hard to explain everything, because you seem to miss fundamental stuff in game programming. Edited by szecs

Share this post


Link to post
Share on other sites
Yeah, I know, my doubt is more related to basic programming than anything else.

Now I did read the links you recommended properly, and I'm starting to understand a bit more about the problems related to floating comparisons (although it looks like I'm way too noob to get some of those stuff as of now). And a TimerFunc, in the case I need, looks a better option (than an IdleFunc).

Thanks for your reply, 'cause it opened my eyes to problems I didn't know existed. I'll just test and study more to get to the result I want.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement