• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
PeterJobs

Keyboard triggers animation

5 posts in this topic

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]
0

Share this post


Link to post
Share on other sites
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]
0

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 [i]while[/i] 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 [url="http://www.cygnus-software.com/papers/comparingfloats/comparingfloats.htm"]comparison that will be never true probably[/url]. 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.



[b]Post the whole stuff[/b] and sorry for the random questions and things. Edited by szecs
0

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.
0

Share this post


Link to post
Share on other sites
Read my previous post again.
1. [url="http://www.cygnus-software.com/papers/comparingfloats/comparingfloats.htm"]comparison that will be never true probably[/url].

2. you are doing all the movement in the [i]while [/i]loop. So even if the [i]while[/i] statement would evaluate to false, it would mean that the object will be instantly in the new position. But since the [url="http://www.cygnus-software.com/papers/comparingfloats/comparingfloats.htm"]comparisons that will be never true probably[/url], 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 [i]while[/i], but an [b][i]if[/i][/b] 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 [url="http://www.koonsolo.com/news/dewitters-gameloop/"]proper timing[/url] (for starters, see "[i]Game Speed dependent on Variable FPS[/i]" section of the linked article).

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

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.
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0