jparril1
|
 |
«
Posted
2011-03-08 19:00:50 » |
|
Hey guys,
So I have both Killer Game Programming in Java and Developing Games in Java. Im trying desperately to use them and work through them, and I am getting NOWHERE. Before I had these books, I used my own knowledge and help from you guys and managed to put together a decent game of pong. I thought I could dive into these books to get better and do things the "right" way, but all thats happening is confusion. KGPIJ is talking about a million different ways to achieving certain FPS, and then has a bunch of unfinished, broken methods. Im trying to get something out of these but Im getting discouraged. Is it recommended to just forget these "official" books and just use trial and error along with the few online tutorials that exist? I have some links that have been suggested such as Kevs Space Invader tutorial and a few others. In your experience, is it possible to get good at this by just writing code, asking for help, and fixing it until it works?? Right now Im attempting to find some "path" that I seem to be failing at discovering.
|
|
|
|
|
BatKid
Senior Newbie 
|
 |
«
Reply #1 - Posted
2011-03-08 19:13:41 » |
|
I personally don't think there is a "right" way to learn. Everyone learns differently and you need to find a way that suits you. Some people can learn from just reading, others can't understand the concept unless it is presented in code form. Yet others learn from watching others.
If you find reading the book confusing, I think you should start search for online tutorials/exercises that are right for you. Once you have some of the basics down, go back to the book and it will make more sense.
|
Learn Java in 3D with env3d
|
|
|
Eli Delventhal
|
 |
«
Reply #2 - Posted
2011-03-08 19:28:52 » |
|
I have never used books except for occasional reference - they just don't work for me. Instead, I decide on a project to make (Pong for example), then make it using hackery, crappy code, and a lot of plagiarism from examples on the net. Eventually I learn what it all means from doing that enough times.  Although to be honest these days I don't do that either, because I know so many programming languages that I don't need to. Instead I just start writing code and debug it as I go.
|
|
|
|
Games published by our own members! Check 'em out!
|
|
pjt33
|
 |
«
Reply #3 - Posted
2011-03-08 21:43:30 » |
|
Probably half of us learned by hacking together stuff before we had access to the Internet (let alone knew where to look for help). So just trying, failing, and trying again clearly works for some.
|
|
|
|
|
Sinuath
|
 |
«
Reply #4 - Posted
2011-03-08 22:55:04 » |
|
You sure the books aren't to advanced for you? there's usually a certain progression you want to follow, the lower level books help you get ready for the higher level books. I'm usually a fan of books when i can get my hands on them, there's to many tutorials out there and they randomly become useless in some instances. Or they don't end up teaching me what i'm looking for. With a book it knows what it's taught you and points you to where you need to go next.
Anyway i wouldn't give up on all books if it's just two that your having trouble with.. buut~ if you can manage to learn this without a book and would rather do it go for it. Some people can manage it, i just can't imagine that it's really easier.
|
|
|
|
philfrei
|
 |
«
Reply #5 - Posted
2011-03-08 23:07:37 » |
|
I recall "failing" at trying to learn about Java game programming from a book by an author named Harbour. The example program was based on Asteroids. I do recall being able to change some parameters here and there, getting some of the code working. But a lot of it was incantation as far as I could tell at the time. I had to move on in order to get a better foundation in Java programming.
I would like to suggest putting some specific questions from the books you are reading onto the forum, including examples of what seems to be buggy code. It would be a lot easier to give advice if we knew more specifics. "I'm getting nowhere with books. Should I give up reading?" That's not a lot to work on.
|
"Greetings my friends! We are all interested in the future, for that is where you and I are going to spend the rest of our lives!" -- The Amazing Criswell
|
|
|
aazimon
|
 |
«
Reply #6 - Posted
2011-03-09 00:44:13 » |
|
I have both books, but haven't finished them. The reason being, that I started with my on game program and ideas, and used the book to improve what I already had. If the book had something that I didn't think worked for me, I chunked it. I recommend you start with the game program you have, and as you read the book, see if it is something that you can use in your game program to improve it. You are right the first chapter of KGPJ spent way to much time going over FPS loops. Just skip to his final game loop and see if it's something you can use.
|
|
|
|
|
dah01
|
 |
«
Reply #7 - Posted
2011-03-09 02:43:56 » |
|
I'm in the same boat as the original poster. I've been trying to read these books but they're old and the java3d timer is deprecated. This board doesn't seem to help because there aren't any basic tutorials. Every night I spend all my time trying to find something that isn't deprecated or unclear. It's depressing and I'm considering not returning to java game development.
|
|
|
|
|
ra4king
|
 |
«
Reply #8 - Posted
2011-03-09 04:03:11 » |
|
The best way is to make your own game loop: 1 2 3 4 5 6 7 8 9 10 11 12
| int FPStime = 1000/FPS;
while(!isGameOver) { long startTime = System.currentTimeMillis(); try{ long sleepTime = FPStime - (System.currentTimeMillis()-startTime); Thread.sleep((int)sleepTime); } catch(Exception exc) { exc.printStackTrace(); } } |
|
|
|
|
jparril1
|
 |
«
Reply #9 - Posted
2011-03-09 04:32:56 » |
|
Thanks for all of the help guys. I think my problem is that Im trying to use the books in a logical order (like a book should be) but in reality I need to use them more as a reference to fix my problems. I think Im just going to hack at my own ideas and designs until I run into a wall, and then either post here or figure it out with the internet or these books. Then from that I can build better ways to do things and start making sense of all this. Any other suggestions are always appreciated! 
|
|
|
|
|
Games published by our own members! Check 'em out!
|
|
deadly72
|
 |
«
Reply #10 - Posted
2011-03-09 04:40:33 » |
|
The best way is to make your own game loop: 1 2 3 4 5 6 7 8 9 10 11 12
| int FPStime = 1000/FPS;
while(!isGameOver) { long startTime = System.currentTimeMillis(); try{ long sleepTime = FPStime - (System.currentTimeMillis()-startTime); Thread.sleep((int)sleepTime); } catch(Exception exc) { exc.printStackTrace(); } } |
I actually disagree with a tiny point in this code. The use of currentTimeMillis over nanoTime. Maybe I am just naive but I've read in a few places that depending on the os currentTimeMillis is slower overall then nanoTime. The one advantage it has, is being compatible pre 5.0?
|
|
|
|
|
ra4king
|
 |
«
Reply #11 - Posted
2011-03-09 05:16:52 » |
|
For some weird reason, nanoTime is NOT the same as currentTimeMillisx1000000. For example, this does not work: 1 2 3 4 5 6 7 8 9 10 11 12
| int FPStime = 1000000/FPS; while(!isGameOver) { long startTime = System.nanoTime(); try{ long sleepTime = FPStime - (System.nanoTime()-startTime); Thread.sleep(0,(int)sleepTime); } catch(Exception exc) { exc.printStackTrace(); } } |
|
|
|
|
Eli Delventhal
|
 |
«
Reply #12 - Posted
2011-03-09 05:32:33 » |
|
For some weird reason, nanoTime is NOT the same as currentTimeMillisx1000. For example, this does not work: 1 2 3 4 5 6 7 8 9 10 11 12
| int FPStime = 1000000/FPS; while(!isGameOver) { long startTime = System.nanoTime(); try{ long sleepTime = FPStime - (System.nanoTime()-startTime); Thread.sleep(0,(int)sleepTime); } catch(Exception exc) { exc.printStackTrace(); } } |
Look up nano versus milli. mill = 1/1000 nano = 1/1000000000 1/1000 of a millisecond is not a nanosecond, it's a microsecond. A nanosecond is 1/1000 of a microsecond. That aside, currentTimeMillis() is less accurate than nanoTime() so I wouldn't expect them to always line up exactly.
|
|
|
|
ra4king
|
 |
«
Reply #13 - Posted
2011-03-09 08:00:53 » |
|
Oh how could I make such a stupid mistake  However, millis and nano are very different, since if you look it up, System.nanoTime() is not from January 1, 1970. Updated: 1 2 3 4 5 6 7 8 9 10 11 12 13 14
| int FPStime = 1000000000/FPS; while(!isGameOver) { long startTime = System.nanoTime(); try{ long sleepTime = FPStime - (System.nanoTime()-startTime); int m = (int)(sleepTime/1000000); int n = (int)(sleepTime%1000000); Thread.sleep(m,n); } catch(Exception exc) { exc.printStackTrace(); } } |
|
|
|
|
pjt33
|
 |
«
Reply #14 - Posted
2011-03-09 10:04:10 » |
|
Why does the epoch matter? On the other hand, you're going to get compile-time errors for exceeding the range of int. With nanos, use long.
|
|
|
|
|
ReBirth
|
 |
«
Reply #15 - Posted
2011-03-09 13:52:40 » |
|
In my learning process (which is going now), I always start from scratch. When meet a problem 1. try break the problem by myself 2. no result, read some books 3. no result, post here  just use any way that suit you. for me, try and error is the best.
|
|
|
|
Riven
|
 |
«
Reply #16 - Posted
2011-03-09 15:31:34 » |
|
Oh, how we all wished that method did what it suggested... it actually rounds the nanos to the nearest millisecond and sleeps for the calculated amount in milliseconds (at which it is very inaccurate). It's a big joke, really.
|
|
|
|
dah01
|
 |
«
Reply #17 - Posted
2011-03-10 00:22:08 » |
|
Seriously? That's disgusting.
|
|
|
|
|
ra4king
|
 |
«
Reply #18 - Posted
2011-03-10 02:39:16 » |
|
Oh, how we all wished that method did what it suggested... it actually rounds the nanos to the nearest millisecond and sleeps for the calculated amount in milliseconds (at which it is very inaccurate). It's a big joke, really. What?!? Well this makes me mad, since I just starting using this for my games  Seriously? That's disgusting.
Bahaha agreed 
|
|
|
|
philfrei
|
 |
«
Reply #19 - Posted
2011-03-10 09:59:46 » |
|
Might be interesting to try testing the two methods. But I'm not at all sure as to the best way to go about that. Perhaps, at the given time, print to file or system.out the trigger time and the current time by either method? Hmmm. There is probably something very wrong with that. It's always nice to get some sort of statement as to the expected tolerances, rather than just "it's inaccurate" which could mean a lot of different things. 
|
"Greetings my friends! We are all interested in the future, for that is where you and I are going to spend the rest of our lives!" -- The Amazing Criswell
|
|
|
gimbal
|
 |
«
Reply #20 - Posted
2011-03-10 10:48:31 » |
|
Seriously? That's disgusting.
Blame the OS / hardware. No mainstream OS can provide nano second precision timing, so adding support for it in Java was basically a useless exercise. The best you can get is microsecond precision from it. But perhaps in the future such things will become possible, who knows.
|
|
|
|
|
Riven
|
 |
«
Reply #21 - Posted
2011-03-10 11:41:11 » |
|
With a bit of C++ (and a JNI wrapper), we can do accurate milli/micro sleeps: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
| #include <time.h> #include <errno.h>
void millisleep(const int ms) { struct timespec sleeptime; struct timespec remaining; int result; remaining.tv_sec = (ms/1000); remaining.tv_nsec = (ms%1000)*1000000; do { sleeptime.tv_sec = remaining.tv_sec; sleeptime.tv_nsec = remaining.tv_nsec; result = nanosleep(&sleeptime,&remaining); } while(0>result && EINTR==errno); }
void microsleep(const int us) { struct timespec sleeptime; struct timespec remaining; int result; remaining.tv_sec = (us/1000000); remaining.tv_nsec = (us%1000000)*1000; do { sleeptime.tv_sec = remaining.tv_sec; sleeptime.tv_nsec = remaining.tv_nsec; result = nanosleep(&sleeptime,&remaining); } while(0>result && EINTR==errno); } |
|
|
|
|
gwoptics
Senior Newbie  Projects: 1
|
 |
«
Reply #22 - Posted
2011-03-10 19:18:36 » |
|
I am obviously ignorant and naive, but where would you need nanosecond accuracy anyway? I already find it hard to believe that a few microseconds here or there matter. It's an honest question, in what scenarios do you need accurate microseconds or better?
Andreas
|
|
|
|
IronclawsBt
|
 |
«
Reply #23 - Posted
2011-03-10 19:27:25 » |
|
I am obviously ignorant and naive, but where would you need nanosecond accuracy anyway? I already find it hard to believe that a few microseconds here or there matter. It's an honest question, in what scenarios do you need accurate microseconds or better?
Andreas
For almost everything there is no use. There are, however, some real-time versions of Java where this may be useful. A 2 GHz processor is 0.5 nanoseconds / cycle so the unit is not meaning-less.
|
|
|
|
philfrei
|
 |
«
Reply #24 - Posted
2011-03-10 20:03:32 » |
|
If there are musical parts that are off by a couple milliseconds, they definitely start to feel "out of the pocket". At a single millisecond or less, I'd think I'd have trouble identifying parts that lead/lag. Maybe a world-class professional percussionist could sense offsets at the micro-second level, but I think even so, it would be a matter of hundreds of microseconds, not a matter of just a few.
But even with micro-second timing, it's moot if the GC interruptions take milliseconds to complete, yes? I mean, there are issues relevent to timing besides the degree of granularity of the "getCurrentTime" function.
Concerns at the nano-level can be left to the folks at CERN.
|
"Greetings my friends! We are all interested in the future, for that is where you and I are going to spend the rest of our lives!" -- The Amazing Criswell
|
|
|
|