wood burning stoves 2.0*
The moose likes Game Development and the fly likes Timely response to user input Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Game Development
Bookmark "Timely response to user input" Watch "Timely response to user input" New topic
Author

Timely response to user input

Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
I am writing a basic Bricks game in Java. At the moment, I have a ball that bounces around and a paddle that the user can move back and forth with the arrow keys. I've implemented some fundamental collision detection for the sides of the window, the paddle, and additional balls, although the later isn't used yet.

My question mostly revolves around implementing the paddle. I'm not very satisfied with the response from the user input. When the user presses or holds down the arrow keys, the paddle moves much more slowly than I think it should. The Ball class extends Runnable and it is animated in the run() method. I think this helps in the user input response somewhat. However, are there any other tricks used in Java game programming? Would threading the Paddle class help at all? I have my doubts about this since the key event handling runs on the same thread as painting and other Swing or AWT events, afaik.

Anyway, I'm more interested in some theory and examples, so I decided not to post my exact code here. I hope someone has some insights for me anway.

Thanks in advance.

Layne


Java API Documentation
The Java Tutorial
Nick George
Ranch Hand

Joined: Apr 04, 2004
Posts: 815
I always found it easiest to just run a single thread... I have an ArrayList of all the things that need to advance (implements Advanceable) and that need to be drawn (implements Drawable), and every cylce I advance the Advanceables and draw the Drawables. As for better response time, with only the things you list, there really shouldn't be any lagging... If the problem is the paddle doesn't move enough with per cycle, why not increase the amount it moves per cycle? Instead of incrementing the x value by 5, try 10.

Anyhoo... if the problem is really that the whole thing moves too slowly, which I rather doubt, I'd suggest going to JOGL, an OpenGL binding that I've run a rediculous amount of things on without feeling any slow up. And I mean a couple hundred moving and calculating parts. With trig values.


I've heard it takes forever to grow a woman from the ground
Nick George
Ranch Hand

Joined: Apr 04, 2004
Posts: 815
Now that I think about it... Is the problem that when the user holds down a key, the paddle moves a little, stops for a second, and then moves in a steady stream? If so, that's because the event fires once, and then pauses before it fires continuously when the user holds down a key. You have to use keyPressed and keyReleased to toggle a boolean.
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
Thanks for the input. I can't remember exactly how I implented this and I don't have the code handy since I'm not on my own computer. I'm probably just responding to keyTyped() and ignoring keyPressed() and keyReleased(). Now that you mention it, I can see how the keyboard's typematic rate (That's not the correct terminology since typematic rate is how fast it repeats, not the pause. What's the pause called? I forget.) could be causing the behavior I'm seeing. Since that's a hardware issue, there isn't much I can do about it in software.

I might also look into JOGL when I have the time (which probably won't be all that soon). I've already done some OpenGL in C++. I haven't tried any bindings for other languages though.

Anyways, thank you for the comments. I'll definitely keep them in mind when I find more time to work on this project.

Keep Coding! (TM)

Layne
Nick George
Ranch Hand

Joined: Apr 04, 2004
Posts: 815
you can certainly do something about the !typematic problem... Have a boolean keyDown that is set true whenever keyPressed is fired, and is set false when keyReleased is fired. Have the program react not to keyPressed, but to keyDown being true.
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
Thanks for the clarification. I think I was trying to say there isn't anything you can do about it directly. AFAIK, you can't change the typematic rate or the pause duration in Java (although I've done it with assembly before).

Layne
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
Okay, I finally found some more time to work on this project over the weekend. The problem was more or less the typematic rate as you said. I was only responding to keyPressed() and relying on it being called multiple times from the Swing event mechanism. I've added a flag that keyPressed() now sets to true and keyReleased() sets to false. There were also a few other changes to get this to all work correctly. The paddle moves a lot more smoothly, although there are some other issues. I think I need to rethink my design, but I'll post that as a new thread since it isn't related to the topic of this thread.

Layne
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Timely response to user input
 
Similar Threads
Pong
multiple painting components
Designing classes and methods for a bricks game
BreakOut Java Bug
Java Intersects